@rimbu/deep
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -48,2 +48,3 @@ "use strict"; | ||
(function (Patch) { | ||
Patch.MAP = Symbol('Patch.ALL'); | ||
/** | ||
@@ -96,2 +97,11 @@ * Returns a function that patches a given object of type `T` with the given `patches` | ||
return patcher; | ||
if (valueIsArray && Patch.MAP in patcher) { | ||
var arr = value; | ||
var itemPatch = patcher[Patch.MAP]; | ||
var result = arr.slice(); | ||
for (var i = 0; i < arr.length; i++) { | ||
result[i] = patchSingle(arr[i], itemPatch, value, root); | ||
} | ||
return result; | ||
} | ||
var clone = valueIsArray ? tslib_1.__spreadArray([], tslib_1.__read(value)) : tslib_1.__assign({}, value); | ||
@@ -98,0 +108,0 @@ var changed = false; |
@@ -28,2 +28,3 @@ import { RimbuError } from '@rimbu/base'; | ||
(function (Patch) { | ||
Patch.MAP = Symbol('Patch.ALL'); | ||
/** | ||
@@ -70,2 +71,11 @@ * Returns a function that patches a given object of type `T` with the given `patches` | ||
return patcher; | ||
if (valueIsArray && Patch.MAP in patcher) { | ||
const arr = value; | ||
const itemPatch = patcher[Patch.MAP]; | ||
const result = arr.slice(); | ||
for (let i = 0; i < arr.length; i++) { | ||
result[i] = patchSingle(arr[i], itemPatch, value, root); | ||
} | ||
return result; | ||
} | ||
const clone = valueIsArray ? [...value] : Object.assign({}, value); | ||
@@ -72,0 +82,0 @@ let changed = false; |
import type { ArrayNonEmpty } from '@rimbu/common'; | ||
import { Immutable, Literal } from './internal'; | ||
import { Literal } from './internal'; | ||
/** | ||
@@ -24,4 +24,5 @@ * Type to determine the allowed input type for the `patch` function. | ||
*/ | ||
export declare function patch<T>(value: T): (...patches: Patch.Multi<T>) => Immutable<T>; | ||
export declare function patch<T>(value: T): (...patches: Patch.Multi<T>) => T; | ||
export declare namespace Patch { | ||
const MAP: unique symbol; | ||
/** | ||
@@ -47,3 +48,3 @@ * Type to determine the allowed input type for the `patch` function given an object type T. | ||
*/ | ||
type Update<T, P, R> = Literal.Value<T> | ((value: Immutable<T>, parent: Immutable<P>, root: Immutable<R>) => T extends boolean ? boolean : T); | ||
type Update<T, P, R> = Literal.Value<T> | ((value: T, parent: P, root: R) => T extends boolean ? boolean : T); | ||
/** | ||
@@ -55,3 +56,5 @@ * Type representing allowed patches for arrays | ||
*/ | ||
type PatchArray<T extends readonly unknown[], P, R> = (Patch.Update<T, P, R> | { | ||
type PatchArray<T extends readonly unknown[], P, R> = (Patch.Update<T, P, R> | (T extends readonly (infer E)[] ? { | ||
[Patch.MAP]: Patch<E, T, R>; | ||
} : never) | { | ||
[K in { | ||
@@ -72,3 +75,3 @@ [K2 in keyof T]: K2; | ||
*/ | ||
function create<T, T2 extends T = T>(...patches: Patch.Multi<T2>): (value: T) => Immutable<T>; | ||
function create<T, T2 extends T = T>(...patches: Patch.Multi<T2>): (value: T) => T; | ||
} |
{ | ||
"name": "@rimbu/deep", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "Tools to use handle plain JS objects as immutable objects", | ||
@@ -68,3 +68,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "ebcf6f5f730b10fd78f14d8886e0b50eb186d5ad" | ||
"gitHead": "4990d9a30716d3a5e7117545e5a6ced2d36ae3ad" | ||
} |
import { RimbuError } from '@rimbu/base'; | ||
import type { ArrayNonEmpty } from '@rimbu/common'; | ||
import { Immutable, Literal } from './internal'; | ||
import { Literal } from './internal'; | ||
@@ -31,15 +31,8 @@ /** | ||
*/ | ||
export function patch<T>( | ||
value: T | ||
): (...patches: Patch.Multi<T>) => Immutable<T> { | ||
export function patch<T>(value: T): (...patches: Patch.Multi<T>) => T { | ||
return function (...patches) { | ||
let result = value as Immutable<T>; | ||
let result = value; | ||
for (const p of patches) { | ||
result = patchSingle( | ||
result, | ||
p as Patch<T>, | ||
result, | ||
result | ||
) as Immutable<T>; | ||
result = patchSingle(result, p as Patch<T>, result, result); | ||
} | ||
@@ -52,2 +45,4 @@ | ||
export namespace Patch { | ||
export const MAP = Symbol('Patch.ALL'); | ||
/** | ||
@@ -77,7 +72,3 @@ * Type to determine the allowed input type for the `patch` function given an object type T. | ||
| Literal.Value<T> | ||
| (( | ||
value: Immutable<T>, | ||
parent: Immutable<P>, | ||
root: Immutable<R> | ||
) => T extends boolean ? boolean : T); | ||
| ((value: T, parent: P, root: R) => T extends boolean ? boolean : T); | ||
@@ -92,2 +83,3 @@ /** | ||
| Patch.Update<T, P, R> | ||
| (T extends readonly (infer E)[] ? { [Patch.MAP]: Patch<E, T, R> } : never) | ||
| { | ||
@@ -112,3 +104,3 @@ [K in { [K2 in keyof T]: K2 }[keyof T]]?: Patch<T[K], T, R>; | ||
...patches: Patch.Multi<T2> | ||
): (value: T) => Immutable<T> { | ||
): (value: T) => T { | ||
return (value) => | ||
@@ -120,7 +112,7 @@ patch<T>(value)(...(patches as unknown as Patch.Multi<T>)); | ||
function patchSingle<T, P = T, R = T>( | ||
value: Immutable<T>, | ||
value: T, | ||
patcher: Patch<T, P, R>, | ||
parent: Immutable<P>, | ||
root: Immutable<R> | ||
): Immutable<T> { | ||
parent: P, | ||
root: R | ||
): T { | ||
if (typeof patcher === 'function') { | ||
@@ -132,3 +124,3 @@ return patcher(value, parent, root) as any; | ||
if (typeof patcher !== 'object' || null === patcher) return patcher as any; | ||
if (Literal.isLiteral<Immutable<T>>(patcher)) { | ||
if (Literal.isLiteral<T>(patcher)) { | ||
return Literal.getValue(patcher); | ||
@@ -147,3 +139,3 @@ } | ||
if (Literal.isLiteral<Immutable<T>>(patcher)) { | ||
if (Literal.isLiteral<T>(patcher)) { | ||
return Literal.getValue(patcher); | ||
@@ -156,2 +148,15 @@ } | ||
if (valueIsArray && Patch.MAP in patcher) { | ||
const arr = value as unknown as any[]; | ||
const itemPatch = (patcher as any)[Patch.MAP]; | ||
const result = arr.slice(); | ||
for (let i = 0; i < arr.length; i++) { | ||
result[i] = patchSingle(arr[i], itemPatch, value, root); | ||
} | ||
return result as any; | ||
} | ||
const clone: any = valueIsArray ? ([...(value as any)] as any) : { ...value }; | ||
@@ -158,0 +163,0 @@ let changed = false; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100656
2159