Last active
April 22, 2020 05:04
-
-
Save renatorib/f48e916eed7ce61e812624a13d3a005d to your computer and use it in GitHub Desktop.
mini-ramda
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
export const uncurry = (fn) => (...args) => { | |
let result = fn | |
for(i in args){ | |
result = typeof result === 'function' | |
? result(args[i]) | |
: result | |
} | |
return result | |
} | |
export const add = uncurry(a => b => Number(a + b)) | |
export const subtract = uncurry(a => b => Number(a - b)) | |
export const complement = fn => (...args) => !fn(...args) | |
export const keys = obj => Object.keys(obj) | |
export const map = uncurry(fn => array => array.map(fn)) | |
export const filter = uncurry(fn => array => array.filter(fn)) | |
export const reject = uncurry(fn => array => array.filter(complement(fn))) | |
export const sort = uncurry(fn => array => [...array].sort(fn)) | |
export const head = array => array[0] | |
export const gte = uncurry(a => b => a >= b) | |
export const lte = uncurry(a => b => a <= b) | |
export const prop = uncurry(name => obj => obj[name]) | |
export const equals = uncurry(a => b => a === b) | |
export const all = uncurry(fn => arr => reject(fn, arr).length === 0) | |
export const none = uncurry(fn => arr => filter(fn, arr).length === 0) | |
export const assoc = uncurry(str => value => obj => ({ ...obj, [str]: value })) | |
export const dissoc = uncurry(str => obj => (n = {...obj}, delete n[str], n)) | |
export const tail = arr => typeof arr === 'string' | |
? [...arr].slice(1, arr.length).join('') | |
: [...arr].slice(1, arr.length) | |
export const take = uncurry(n => arr => typeof arr === 'string' | |
? [...arr].slice(0, n).join('') | |
: [...arr].slice(0, n)) | |
export const takeLast = uncurry(n => arr => typeof arr === 'string' | |
? [...arr].slice(Math.max(arr.length - n, 0)).join('') | |
: [...arr].slice(Math.max(arr.length - n, 0))) | |
export const asc = (a, b) => a - b | |
export const desc = (a, b) => b - a | |
export const pipe = (...fns) => (...props) => fns.reduce((res, fn) => fn(res), ...props) | |
export const compose = (...fns) => pipe(...(fns.reverse())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment