Last active
September 24, 2020 18:50
-
-
Save dinocarl/309ae7cc1f7f48de6511d9c7f110e3fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Utils | |
const curry = (fn) => (...xs) => { | |
if (xs.length >= fn.length) { | |
return fn(...xs); | |
} | |
return curry(fn.bind(null, ...xs)); | |
}; | |
const compose = (...fnList) => data => | |
fnList.reduceRight((val, fn) => fn(val), data) | |
const checkType = (obj) => Object.prototype.toString.call(obj).slice(8, -1); | |
const is = curry((type, obj) => typeof obj !== 'undefined' && obj !== null && checkType(obj) === type); | |
const map = curry((fn, list) => list.map(fn)); | |
const filter = curry((fn, list) => list.filter(fn)); | |
const reduce = curry((fn, acc, data) => data.reduce(fn, acc)); | |
const concat = curry((arr1, arr2) => arr1.concat(arr2)); | |
// Arrays | |
const join = curry((glue, list) => list.join(glue)); | |
const strConcat = join(''); | |
const head = list => list[0]; | |
const last = list => list[list.length - 1]; | |
const contains = curry((item, list) => list.indexOf(item) > -1); | |
const length = list => list.length; | |
const flatten = reduce( | |
(acc, item) => is('Array', item) | |
? concat(acc, flatten(item)) | |
: concat(acc, item) | |
, | |
[] | |
); | |
const times = curry((num, val) => Array(num).fill(val)); | |
const sort = curry((fn, list) => list.sort(fn)); | |
const asc = (a, b) => a - b | |
const desc = (a, b) => b - a | |
// Objects | |
const assignApply = curry((context, arr) => Object.assign.apply(context, [{}].concat(arr))); | |
const nullContextAssignApply = assignApply(null); | |
const reduceAssign = nullContextAssignApply; | |
const propOr = curry((fallback, key, obj) => is('Object', obj) && obj.hasOwnProperty(key) ? obj[key] : fallback); | |
const prop = curry((key, obj) => propOr(undefined, key, obj)); | |
const pathOr = curry((fallback, keys, obj) => { | |
if (is('Array', keys) && is('Object', obj)) { | |
let currentVal = reduceAssign(obj); | |
const vals = map(function(key){ | |
currentVal = prop(key, currentVal); | |
return currentVal; | |
}, keys); | |
return contains(undefined, vals) ? fallback : last(vals); | |
} | |
else { | |
throw Error('pathOr: Incorrect usage with ' + keys + ' and ' + obj); | |
} | |
}); | |
const path = curry((keys, obj) => pathOr(undefined, keys, obj)); | |
// zip :: [a] -> [b] -> [a, b] | |
const zip = curry((a, b) => a.map( | |
(key, index) => [ | |
key, | |
b[index] | |
] | |
)); | |
// objAttrs :: Object -> [[Keys], [Attrs]] | |
const objAttrs = (obj) => { | |
const keys = Object.keys(obj); | |
return [ | |
keys, | |
keys.map(function (key) { | |
return obj[key]; | |
}) | |
]; | |
}; | |
const toPairs = obj => zip.apply(null, objAttrs(obj)); | |
// Strings | |
const split = curry((separator, str) => str.split(separator)); | |
const appendStr = curry((a, b) => `${a}${b}`); | |
const prependStr = curry((a, b) => `${b}${a}`); | |
const toString = val => `${val}` | |
const replace = curry((searchStr, replaceStr, str) => str.replace(searchStr, replaceStr)); | |
// Math | |
const fpParseInt = radix => str => parseInt(str, radix) | |
const add = curry((a, b) => a + b); | |
const subtract = curry((b, a) => a - b); | |
const inc = add(1); | |
const dec = subtract(1); | |
const sum = reduce(add, 0); | |
// Logic | |
const equals = curry((a, b) => a === b); | |
const gt = curry((a, b) => a < b); | |
const lt = curry((a, b) => a > b); | |
const gte = curry((a, b) => a <= b); | |
const lte = curry((a, b) => a >= b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤️