Skip to main content
Module

x/rambda/evolve.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { _isArray } from './_internals/_isArray'import { mapArray, mapObject } from './map'import { type } from './type'
export function evolveArray(rules, list){ return mapArray( (x, i) => { if (type(rules[ i ]) === 'Function'){ return rules[ i ](x) }
return x }, list, true )}
export function evolveObject(rules, iterable){ return mapObject((x, prop) => { if (type(x) === 'Object'){ const typeRule = type(rules[ prop ]) if (typeRule === 'Function'){ return rules[ prop ](x) } if (typeRule === 'Object'){ return evolve(rules[ prop ], x) }
return x } if (type(rules[ prop ]) === 'Function'){ return rules[ prop ](x) }
return x }, iterable)}
export function evolve(rules, iterable){ if (arguments.length === 1){ return _iterable => evolve(rules, _iterable) } const rulesType = type(rules) const iterableType = type(iterable)
if (iterableType !== rulesType){ throw new Error('iterableType !== rulesType') }
if (![ 'Object', 'Array' ].includes(rulesType)){ throw new Error(`'iterable' and 'rules' are from wrong type ${ rulesType }`) }
if (iterableType === 'Object'){ return evolveObject(rules, iterable) }
return evolveArray(rules, iterable)}