@rimbu/deep
Version:
Tools to use handle plain JS objects as immutable objects
117 lines • 4.5 kB
JavaScript
import { isPlainObj, } from '@rimbu/base';
/**
* Returns an immutably updated version of the given `value` where the given `patchItems` have been
* applied to the result.
* The Rimbu patch notation is as follows:
* - if the target is a simple value or array, the patch can be the same type or a function returning the same type
* - if the target is a tuple (array of fixed length), the patch be the same type or an object containing numeric keys with patches indicating the tuple index to patch
* - if the target is an object, the patch can be the same type, or an array containing partial keys with their patches for the object
* @typeparam T - the type of the value to patch
* @typeparam TE - a utility type
* @typeparam TT - a utility type
* @param value - the input value to patch
* @param patchItem - the `Patch` value to apply to the input value
* @example
* ```ts
* const input = { a: 1, b: { c: true, d: 'a' } }
* patch(input, [{ a: 2 }]) // => { a: 2, b: { c: true, d: 'a' } }
* patch(input, [{ b: [{ c: (v) => !v }] }] )
* // => { a: 1, b: { c: false, d: 'a' } }
* patch(input: [{ a: (v) => v + 1, b: [{ d: 'q' }] }] )
* // => { a: 2, b: { c: true, d: 'q' } }
* ```
*/
export function patch(value, patchItem) {
return patchEntry(value, value, value, patchItem);
}
function patchEntry(value, parent, root, patchItem) {
if (Object.is(value, patchItem)) {
// patching a value with itself never changes the value
return value;
}
if (typeof value === 'function') {
// function input, directly return patch
return patchItem;
}
if (typeof patchItem === 'function') {
// function patch always needs to be resolved first
const item = patchItem(value, parent, root);
return patchEntry(value, parent, root, item);
}
if (isPlainObj(value)) {
// value is plain object
return patchPlainObj(value, root, patchItem);
}
if (Array.isArray(value)) {
// value is tuple or array
return patchArr(value, root, patchItem);
}
// value is primitive type or complex object
return patchItem;
}
function patchPlainObj(value, root, patchItem) {
if (!Array.isArray(patchItem)) {
// the patch is a complete replacement of the current value
return patchItem;
}
// patch is an array of partial updates
// copy the input value
const result = { ...value };
let anyChange = false;
// loop over patches in array
for (const entry of patchItem) {
// update root if needed
const currentRoot = value === root ? { ...result } : root;
// keep current updated result as parent
const parent = { ...result };
// loop over all the patch keys
for (const key in entry) {
// patch the value at the given key with the patch at that key
const currentValue = result[key];
const newValue = patchEntry(currentValue, parent, currentRoot, entry[key]);
if (!Object.is(currentValue, newValue)) {
// if value changed, set it in result and mark change
anyChange = true;
result[key] = newValue;
}
}
}
if (anyChange) {
// something changed, return new value
return result;
}
// nothing changed, return old value
return value;
}
function patchArr(value, root, patchItem) {
if (Array.isArray(patchItem)) {
// value is a normal array
// patch is a complete replacement of current array
return patchItem;
}
// value is a tuple
// patch is an object containing numeric keys with function values
// that update the tuple at the given indices
// copy the tuple
const result = [...value];
let anyChange = false;
// loop over all index keys in object
for (const index in patchItem) {
const numIndex = index;
// patch the tuple at the given index
const currentValue = result[numIndex];
const newValue = patchEntry(currentValue, value, root, patchItem[index]);
if (!Object.is(newValue, currentValue)) {
// if value changed, set it in result and mark change
anyChange = true;
result[numIndex] = newValue;
}
}
if (anyChange) {
// something changed, return new value
return result;
}
// nothing changed, return old value
return value;
}
//# sourceMappingURL=patch.mjs.map