diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2018-05-07 09:06:04 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2018-05-07 09:06:04 +0200 |
| commit | 5caf4e48174533710243e8eeaf52567795ec9d01 (patch) | |
| tree | 1396f09bd14c3c4e1af3164ef924f2190b3f949f /backend/utils.js | |
| parent | ded191658af266a764332b9a1b72d628de3d5b6e (diff) | |
| download | brokentalents-5caf4e48174533710243e8eeaf52567795ec9d01.tar.gz brokentalents-5caf4e48174533710243e8eeaf52567795ec9d01.zip | |
move files to backend subdir, push data in submodule
Diffstat (limited to 'backend/utils.js')
| -rw-r--r-- | backend/utils.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/backend/utils.js b/backend/utils.js new file mode 100644 index 0000000..6cff356 --- /dev/null +++ b/backend/utils.js @@ -0,0 +1,72 @@ +#!/usr/bin/node + +const R = require('ramda'); + +const lengthEq = (n) => R.compose(R.equals(n), R.length); +const list = R.unapply(R.identity); +const unlist = R.apply(R.identity); +const mergeAllArgs = R.compose(R.mergeAll, list); +const objOfPath = R.curry((objOf, path) => R.compose(R.objOf(objOf), R.path(path))); +const andAll = R.all(R.equals(R.T())); +const andAllArgs = R.compose(andAll, list); + +const projectProps = R.curry( + (proj, d) => R.compose(R.prop(0), R.project(proj), R.of)(d) +); + +const joinRight = R.curry((mapper1, mapper2, t1, t2) => { + let indexed = R.indexBy(mapper1, t1); + return t2.map((t2row) => R.merge(t2row, indexed[mapper2(t2row)])); +}); + +const commonKeys = R.useWith(R.intersection, [ + R.keys, + R.keys, +]); + +const naturalJoinRight = R.curry((t1, t2) => joinRight( + R.props(commonKeys(t1[0], t2[0])), + R.props(commonKeys(t1[0], t2[0])), + t2, + t1, +)); + +const eqPropses = R.curry((props) => // TODO should create function with arity 3 but doesn't because it's nested + R.converge(andAllArgs, R.map(R.eqProps, props)) +); + +const mergeAllWith = (merger) => R.until(lengthEq(1), R.pipe( // TODO same issue + R.splitEvery(2), + R.map( + R.ifElse(lengthEq(2), // TODO refactor + R.apply(R.mergeWith(merger)), + R.apply(R.identity)) + ) +)); + +const divPropsByProp = R.curry((prop, props, data) => { // TODO refactor + return R.fromPairs( + R.map((aProp) => + R.contains(aProp, props) ? + [aProp, R.prop(aProp, data) / R.prop(prop, data)] + : [aProp, R.prop(aProp, data)], + R.keys(data), + ) + ); +}); + +module.exports = { + list, + unlist, + mergeAllArgs, + objOfPath, + andAll, + andAllArgs, + joinRight, + naturalJoinRight, + eqPropses, + lengthEq, + mergeAllWith, + projectProps, + divPropsByProp, +}; |
