Skip to content

Instantly share code, notes, and snippets.

@reggi
Last active March 12, 2019 20:55
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 reggi/2e51bc3c63249aa4800b78b35895dc92 to your computer and use it in GitHub Desktop.
Save reggi/2e51bc3c63249aa4800b78b35895dc92 to your computer and use it in GitHub Desktop.
import {journey} from '@reggi/journey'
import * as lodash from 'lodash'
const getPairs = (obj) => lodash.entries(obj).map(([key, method], index) => ({key, method, index}));
const nestedReduce = (pairs, cb) => {
return pairs.reduce((acq, fromPair) => {
acq[fromPair.key] = pairs.reduce((acq, toPair) => {
const r = cb({ fromPair, toPair });
if (r) acq[toPair.key] = r;
return acq;
}, {});
return acq;
}, {})
}
const prophecy = (po) => nestedReduce(getPairs(po), ({ fromPair, toPair }) => {
if (fromPair.index > toPair.index) return;
const localizedPairs = lodash.slice(getPairs(po), fromPair.index, toPair.index + 1)
const localizedMethods = localizedPairs.map(p => p.method);
return (args) => journey(() => [
() => (args),
...localizedMethods
])();
})
const p = prophecy({
init: ({name}) => ({name}),
addAlpha: ({name}) => ({alpha: `alpha${name}`}),
addBeta: ({alpha}) => ({beta: `beta${alpha}`}),
addGamma: ({beta}) => ({gamma: `gamma${beta}`}),
addDelta: ({gamma}) => ({delta: `delta${gamma}`}),
})
console.log(p.init.addDelta({ name: 'Thomas'}))
console.log(p.addBeta.addDelta({ alpha: 'alphaThomas'}))
// different kind of prophecy has ability to convert types within chain and overload
{
datePeriod: () =>
datePeriods: ()
date: ({ datePeriod }) =>
dates:
dateUnit: () =>
dateUnits:
sort: ({ date, dateUnit, datePeriods})
}
p.datePeriods.to.dateUnits()
const P = { overload: (a: any) => (a: any) => {} };
///
const x = {
ruleOptions: ({ customer, serviceRecurrence }) => P.overload([
({ customer, serviceRecurrence }) => customer && serviceRecurrence ? getRuleOptionsFromServiceRecurrence({ customer, serviceRecurrence }) : P.NULL,
({ customer }) => customer ? getRuleOptionsFromCustomer({ customer }) : P.NULL,
])({ customer, serviceRecurrence }),
};
x.ruleOptions({ customer });
ruleUtils.ruleOptions.to.dateUnits({});
from: {
ruleOptions: (ruleOptions) => ruleOptions;
},
to: {
dateSpan: {
({ ruleOptions }) =>
},
dateUnits: {
({ dateSpan }) =>
}
}
feat.recurrenceEngine
.inject({ mongo, pgdb, now: new Date() })
.evaluate(({ from }) => from.express.to.createCustomerServiceRecurrence({ req })
.then(jsonResponse => res.json(jsonResponse))
feat.recurrenceEngine
.inject({ mongo, pgdb })
.evaluate(({ chain }) => chain.pgdbCustomer.mongoCustomer.customer({ req })
.then(jsonResponse => res.json(jsonResponse))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment