Last active
April 25, 2018 22:55
-
-
Save stukennedy/2bbbbd71c50f6cd637c33c9c3f77baed to your computer and use it in GitHub Desktop.
FP Box - functional lodash style tools written in ES6
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
// jshint esnext: true | |
const curry = (f) => (...a) => (a.length < f.length) ? curry(f.bind(this, ...a)) : f(...a) | |
const clone = (obj) => Object.assign({}, obj) | |
const chain = (...fs) => (a) => fs.reduce((o, f) => f(o), a) | |
const isEmpty = curry((a) => !a || a === null || a === '' || Object.values(a).length === 0) | |
const isString = curry((a) => (typeof a === 'string')) | |
const isNumber = (x) => !isNaN(Number(x)) | |
const isArray = curry((a) => Array.isArray(a)) | |
const range = curry((size) => Array(size).fill(undefined).map((x, i) => i)) | |
const keys = curry((o) => Object.keys(o)) | |
const values = curry((o) => Object.values(o)) | |
const fromPairs = (arr) => arr.reduce((o, el) => { o[el[0]] = el[1]; return o }, {}) | |
const mapValues = curry((fn, a) => Object.keys(a).reduce((o, k) => { o[k] = fn(a[k], k); return o }, {})) | |
const pickBy = curry((fn, a) => Object.keys(a).reduce((o, k) => { if (fn(a[k], k)) o[k] = a[k]; return o }, {})) | |
const reduce = curry((fn, k, a) => a.length ? a.reduce(fn, k) : Object.keys(a).reduce((o, i) => fn(o, a[i], i), k)) | |
const filter = curry((fn, args) => args.length ? args.filter(fn) : pickBy(fn, args)) | |
const map = curry((fn, args) => args.length ? args.map(fn) : mapValues(fn, args)) | |
const fromPath = curry((path) => path.replace('[', '.').replace(']', '').split('.')) | |
const get = curry((p, a) => (isArray(p) ? p : fromPath(p)).reduce((acc, o) => acc ? acc[o] : acc, a)) | |
const set = curry((p, value, obj) => { | |
const path = isArray(p) ? p : fromPath(p) | |
const next = obj[path[0]] || (isNumber(path[1]) ? [] : {}) | |
obj[path[0]] = path.length > 1 ? set(path.slice(1), value, next) : value | |
return obj | |
}) | |
console.log( | |
// fromPath('the.many[0].ways'), | |
chain( | |
set('the.many[5].ways', 'YO'), | |
get(['the', 'many', 5, 'ways']) | |
// get('the.many[5].ways') | |
)({}) | |
) | |
// Example | |
// const addAll = curry((a, b) => a + b) | |
// const square = curry((n) => n * n) | |
// const a = chain( | |
// addAll(2), | |
// square | |
// )(2) | |
// console.log(a) | |
// const output = chain( | |
// map((x, i) => i), | |
// filter(x => x % 2) | |
// )(range(100)) | |
// console.log(output) | |
// const obj = { a: 'foo', b: 'bar' } | |
// const arr = [ 'foo', 'bar' ] | |
// console.log( | |
// map(x => x + '-', obj), | |
// map(x => x + '-', arr) | |
// ); | |
// console.log( | |
// chain( | |
// range, | |
// map((x) => ['_' + x + '_', 100 + x]), | |
// fromPairs, | |
// map((x, i) => i + 'what'), | |
// filter((x, i) => i !== '_5_'), | |
// reduce((acc, x, k) => acc + k, '') | |
// )(10) | |
// ) | |
// console.log(isEmpty([0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment