Skip to content

Instantly share code, notes, and snippets.

@LucasBadico
Created September 12, 2022 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucasBadico/091f8e7b30be90702cb1ea849110eaf1 to your computer and use it in GitHub Desktop.
Save LucasBadico/091f8e7b30be90702cb1ea849110eaf1 to your computer and use it in GitHub Desktop.
Serviços funcionais
/*
tem rodas
anda de ponto a ponto b
tem peso
*/
class Vehicle {
// dado
constructor({
model,
color,
brand,
potency,
weight,
capacity,
total_seats,
start_point: {
x: start_point_x,
y: start_point_y,
},
}) {
this.model = model;
this.color = color;
this.brand = brand;
this.potency = potency;
this.weight = weight;
this.capacity = capacity;
this.total_seats = total_seats;
this.position = {
x: start_point_x,
y: start_point_y
}
}
// comportamento
move(destination_point, aceleration_rate) {
return new Promise((resolve, reject) => {
const deltaX = this.position.x - destination_point.x;
const deltaY = this.position.y - destination_point.y;
const distance = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
const distancepersecond = (this.potency/this.capacity * aceleration_rate/100)/this.weight;
const self = this;
const time_spent = distance/distancepersecond;
return setTimeout(() => {
// MUTATION
self.position = {
x: destination_point.x,
y: destination_point.y,
}
return resolve({ time_spent, actual_position: destination_point, vehicle: self })
}, time_spent*1000)
})
}
}
class Motocycle extends Vehicle {
constructor(args) {
super(args);
this.total_seats = args.total_seats < 2 ? args.total_seats : 2;
}
}
/*
const duke = new Motocycle({
model: 'duke 200',
color: 'white',
brand: 'ktm',
potency: 0.2,
weight: 100,
capacity: 90,
total_seats: 2,
start_point: { x: 0, y:0 },
});
const celta = new Car({
model: 'celta 1.0',
color: 'white',
brand: 'gm',
potency: 1,
weight: 2000,
capacity: 60,
total_seats: 4,
start_point: { x: 0, y:0 },
})
celta.move({x:0, y:1000}, 80).then(console.log);
celta.move({x:100, y:200}, 100).then(console.log);
*/
// A clareza com que os dados se revelam, é tao gostoso.
class Car extends Vehicle {
}
class ToyCar extends Car/*or Vehicle*/ {
// nao tem move
// tem brand, tem model
}
const axios = require('axios');
const { Parser } = require('json2csv');
const parser = (fields) => (list) => {
const opts = { fields };
try {
const p = new Parser(opts);
return p.parse(list);
} catch (err) {
console.error(err);
}
}
function pipe(...fns) {
return (arg) => fns.reduce((lastResultOrPromise, fn) => {
if (lastResultOrPromise && typeof lastResultOrPromise.then === typeof function() {}) {
return lastResultOrPromise.then((result) => fn(result))
}
return fn(lastResultOrPromise);
}, arg)
}
const API_URL = 'https://eopewfmm0j92zl5.m.pipedream.net'; //'http://localhost:4000/data';
const selectedFields = [
'value',
'id',
'bank.account',
'bank.agency'
];
const getPage = async (page=1, list=[]) => {
const {data} = await axios.get(
`${PAGADORIA_API_URL}?limit=12&page=${page}&initial_date=${intialDate}&final_date=${finalDate}`
);
let extractData;
if (typeof data === typeof "-") {
extractData = JSON.parse(data);
} else {
extractData = data;
}
if (page < extractData.total_pages) {
return getPage(page+1, [...list, ...extractData.data]);
}
return [...list, ...extractData.data];
}
const myFuncs = pipe(
getPage,
parser(selectedFields),
(content) => services.fs.write(content, taskId,'csv'),
);
module.exports = async (services, {from, to, company, taskId }) => {
const [intialDate] = from.toISOString().split('T');
const [finalDate] = to.toISOString().split('T');
return myFuncs({ intialDate, finalDate })
}
/*
tem rodas
anda de ponto a ponto b
tem peso
*/
//
const MixinMovable = (
Base// mototocycle
) => class Movable extends Base {
// dado
constructor(args) {
const {
potency,
weight,
capacity,
start_point: {
x: start_point_x,
y: start_point_y,
},
} = args
super(args);
this.potency = potency;
this.weight = weight;
this.capacity = capacity;
this.position = {
x: start_point_x,
y: start_point_y
}
}
// comportamento
move(destination_point, aceleration_rate) {
return new Promise((resolve, reject) => {
const deltaX = this.position.x - destination_point.x;
const deltaY = this.position.y - destination_point.y;
const distance = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
const distancepersecond = (this.potency/this.capacity * aceleration_rate/100)/this.weight;
const self = this;
const time_spent = distance/distancepersecond;
return setTimeout(() => {
// MUTATION
self.position = {
x: destination_point.x,
y: destination_point.y,
}
return resolve({ time_spent, actual_position: destination_point, vehicle: self })
}, time_spent*1000)
})
}
}
class Motocycle {
constructor(args) {
this.total_seats = args.total_seats < 2 ? args.total_seats : 2;
}
}
const MyMovableMotorcycle = MixinMovable(Motocycle);
/*
const duke = new Motocycle({
model: 'duke 200',
color: 'white',
brand: 'ktm',
potency: 0.2,
weight: 100,
capacity: 90,
total_seats: 2,
start_point: { x: 0, y:0 },
});
const celta = new Car({
model: 'celta 1.0',
color: 'white',
brand: 'gm',
potency: 1,
weight: 2000,
capacity: 60,
total_seats: 4,
start_point: { x: 0, y:0 },
})
celta.move({x:0, y:1000}, 80).then(console.log);
celta.move({x:100, y:200}, 100).then(console.log);
*/
// A clareza com que os dados se revelam, é tao gostoso.
class Car extends Vehicle {
}
class ToyCar extends Car/*or Vehicle*/ {
// nao tem move
// tem brand, tem model
}
class Vehicle {
// dado
constructor({
model,
color,
brand,
potency,
weight,
capacity,
total_seats,
start_point: {
x: start_point_x,
y: start_point_y,
},
}) {
this.model = model;
this.color = color;
this.brand = brand;
this.potency = potency;
this.weight = weight;
this.capacity = capacity;
this.total_seats = total_seats;
this.position = {
x: start_point_x,
y: start_point_y
}
}
// comportamento
move(destination_point, aceleration_rate) {
return new Promise((resolve, reject) => {
const deltaX = this.position.x - destination_point.x;
const deltaY = this.position.y - destination_point.y;
const distance = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
const distancepersecond = (this.potency/this.capacity * aceleration_rate/100)/this.weight;
const self = this;
const time_spent = distance/distancepersecond;
return setTimeout(() => {
// MUTATION
self.position = {
x: destination_point.x,
y: destination_point.y,
}
return resolve({ time_spent, actual_position: destination_point, vehicle: self })
}, time_spent*1000)
})
}
}
const MixinVehicleInfo = (Base) => class VehicleInfo extends Base {
constructor(args) {
const {
model,
color,
brand,
total_seats,
} = args;
super(args);
this.model = model;
this.color = color;
this.brand = brand;
this.total_seats = total_seats;
}
}
const MixinPositionable = (Base) => class Positionable extends Base {
constructor(args) {
const {
start_point: {
x: start_point_x,
y: start_point_y,
},
} = args;
super(args);
this.position = {
x: start_point_x,
y: start_point_y
}
}
}
class Car {
}
const MyCarClass = MixinVehicleInfo(MixinPositionable(Car))
// ou
const MyCarClass2 = pipe(
MixinPositionable,
MixinVehicleInfo
)(Car)
const MixinMovableBehavior = (Base) => class Movable extends Base {
move(self, destination_point, aceleration_rate) {
return new Promise((resolve, reject) => {
const deltaX = self.position.x - destination_point.x;
const deltaY = self.position.y - destination_point.y;
const distance = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
const distancepersecond = (self.potency/self.capacity * aceleration_rate/100)/this.weight;
const time_spent = distance/distancepersecond;
return setTimeout(() => {
// MUTATION
const newSelf = Object.assign({}, self, {
position: {
x: destination_point.x,
y: destination_point.y,
}
})
return resolve(newSelf)
}, time_spent*1000)
})
}
}
const MixinMovableData = (Base) => class Movable extends Base {
// dado
constructor(args) {
const {
potency,
weight,
capacity,
start_point: {
x: start_point_x,
y: start_point_y,
},
} = args
super(args);
this.potency = potency;
this.weight = weight;
this.capacity = capacity;
this.position = {
x: start_point_x,
y: start_point_y
}
}
}
class CarData {
}
class CarService {
}
const MyCarServiceBehavior = MixinMovableBehavior(CarService);
const MyCarInfo = MixinMovableData(MixinVehicleInfo(MixinPositionable(CarData)));
const celtaData1 = new MyCarInfo({
model: 'celta 1.0',
color: 'white',
brand: 'gm',
potency: 1,
weight: 2000,
capacity: 60,
total_seats: 4,
start_point: { x: 0, y:0 },
})
const myCarService = new MyCarServiceBehavior({});// cano
const celtaData2 = await myCarService.move({x:0, y:1000}, 80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment