wesm87
11/15/2017 - 7:16 AM

evolveCustom

Custom R.evolve that supports arrays as well as objects

import { always, inc, dec } from 'ramda'

evolveCustom(
  [inc, dec, [dec], always('constant-value')],
  [0, 3, [4], 10, 'unchanged'],
)
// => [1, 2, [3], 'constant-value', 'unchanged']

evolveCustom(
  { a: inc, b: { c: dec }, d: always('constant-value') },
  { a: 0, b: { c: 3 }, d: 10, e: 'unchanged' },
)
// => { a: 1, b: { c: 2 }, d: 'constant-value', e: 'unchanged' }
import {
  __, T, always, pipe, curry, curryN, apply, cond, is, prop, map, addIndex,
  mapObjIndexed,
} from 'ramda'

const evolveCustom = curry((spec, values) => {
  const mapWithIndex = is(Array, values) ? addIndex(map) : mapObjIndexed
  const applyTransform = (value, key) => pipe(
    prop(key),
    cond([
      [is(Function), apply(__, [value])],
      [is(Object), evolveCustom(__, value)],
      [T, always(value)],
    ]),
  )(spec)

  return mapWithIndex(applyTransform, values)
})