Last active
March 12, 2019 20:55
-
-
Save reggi/2e51bc3c63249aa4800b78b35895dc92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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