Skip to main content

evolve

evolve() function

Creates a new object by recursively evolving a shallow copy of object, according to the transformation functions.

Signature:
declare function evolve<O extends Record<string, any>, T extends Transformation<O>>(transformation: T, obj: O): EvolveReturnType<O, T>;

declare function evolve<O extends Record<string, any>, T extends Transformation<O>>(transformation: T): (obj: O) => EvolveReturnType<O, T>;

Example

const add1String = (a: number) => String(a + 1);
const obj = { a: 1, b: 2, c: { d: 3, e: 4 }, f: true };
const transformation = {
a: add1String,
b: add1String,
c: (obj: { d: number; e: number }) =>
evolve({ d: add1String, e: add1String }, obj),
};
evolve(transformation, obj);
// { a: "2", b: "3", c: { d: "4", e: "5" }, f: true }

Open Source Code