maraj 2.7.2 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -8
- package/dist/index.d.mts +23 -12
- package/dist/index.d.ts +23 -12
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,8 @@ originalValue.files[1] === newValue.files[1] // true
|
|
|
41
41
|
- { a: { b: {"c.d": ... } } }
|
|
42
42
|
- { a: { b: {"c": { d: ... } } } }
|
|
43
43
|
|
|
44
|
+
It can still work with dot-paths keys containing an object or array, however the paths targeting nested properties in this key are forbiden. For example having the following object { a: { "b.c": { d: ... } } } you can use the path "a" and "a.b.c", but you cant use "a.b.c.d", this path will create a new property "b" inside "a", then "c" and lastly "d" resulting in the following object { a: { "b.c": { d: ... } }, b: { c: { d: ... } } }
|
|
45
|
+
|
|
44
46
|
- In "extendableUpdate", **numbers** inside path will **always** create a mutable **array** if the array/object doesnt exist beforehand. If you want an object/tuple/readonlyArray instead of an array, add it beforehand as type.
|
|
45
47
|
|
|
46
48
|
```ts
|
|
@@ -57,22 +59,27 @@ const updatedValue2 = extendableUpdate({}, {
|
|
|
57
59
|
// updatedValue2 = { files: { '1': { name: 'new name' } } }
|
|
58
60
|
```
|
|
59
61
|
|
|
60
|
-
- The type "
|
|
62
|
+
- The type "UpdateObject" (the type of the object provided containing the updates, used as second parameter in the update function) if used to create new update objects using computed properties in a inline syntax { [key]: value } will always throw a ts error even if the typing is correct, use the second syntax.
|
|
61
63
|
|
|
62
64
|
```ts
|
|
63
|
-
const updateFieldStore = <TPath extends DotPaths<
|
|
65
|
+
const updateFieldStore = <T, TPath extends DotPaths<T>>(
|
|
66
|
+
state: T,
|
|
64
67
|
path: TPath,
|
|
65
|
-
newValue:
|
|
68
|
+
newValue: UpdateValue<T, TPath>
|
|
66
69
|
) => {
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
// ts error even if the typing is correct (typescript issue)
|
|
71
|
+
const updateObject1: UpdateObject<T> = {
|
|
69
72
|
[path]: newValue
|
|
70
|
-
}
|
|
71
|
-
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// All good, no ts error
|
|
76
|
+
const updateObject2: UpdateObject<T> = {}
|
|
77
|
+
updateObject[path] = newValue
|
|
78
|
+
|
|
72
79
|
}
|
|
73
80
|
```
|
|
74
81
|
|
|
75
|
-
- Use the "set" utility if typescript doesn't infer properly
|
|
82
|
+
- Use the "set" utility if typescript doesn't infer properly *"an optional field"* vs a *"field which can have 'undefined' as type"*. Usually happens in nested props inside arrays.
|
|
76
83
|
|
|
77
84
|
```ts
|
|
78
85
|
const updatedValue = update(data, {
|
package/dist/index.d.mts
CHANGED
|
@@ -21,7 +21,7 @@ type Options = {
|
|
|
21
21
|
onNewIndexes?: OptionValues;
|
|
22
22
|
onPathsIntersectedByPrimitiveValues?: OptionValues;
|
|
23
23
|
};
|
|
24
|
-
type ImmutableUpdate = <TObject
|
|
24
|
+
type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends PropertyKey = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
|
|
25
25
|
/**
|
|
26
26
|
* Creates a new object applying immutable updates using dot-path syntax.
|
|
27
27
|
* Can add or skip new props or indexes depending on options.
|
|
@@ -41,7 +41,7 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
|
|
|
41
41
|
* @param dotPathUpdateObject Dot Path Update Object.
|
|
42
42
|
* @returns New Updated Object.
|
|
43
43
|
*/
|
|
44
|
-
declare const
|
|
44
|
+
declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* --------------------------------------------
|
|
@@ -51,7 +51,7 @@ declare const extendableUpdate: ExtendableImmutableUpdate;
|
|
|
51
51
|
/** Primitives and browser native objects. */
|
|
52
52
|
type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
|
|
53
53
|
/** Array indexes type. */
|
|
54
|
-
type ArrayIndexes = number | LiteralIndex;
|
|
54
|
+
type ArrayIndexes = number | `${number}` | LiteralIndex;
|
|
55
55
|
/** To check if a ReadonlyArray is a tuple or not. */
|
|
56
56
|
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
57
57
|
/**
|
|
@@ -60,15 +60,21 @@ type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false
|
|
|
60
60
|
* --------------------------------------------
|
|
61
61
|
*/
|
|
62
62
|
/** Dot-paths of an array. */
|
|
63
|
-
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ?
|
|
63
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? ArrayIndexes : ArrayIndexes | `${ArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}`;
|
|
64
64
|
/** Dot-paths of an object. */
|
|
65
65
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
66
|
-
[Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${
|
|
67
|
-
}[keyof T & (string | number)];
|
|
66
|
+
[Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`;
|
|
67
|
+
}[keyof T & (string | number)] & unknown;
|
|
68
68
|
/** Dot-paths of a tuple. */
|
|
69
|
-
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes
|
|
69
|
+
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes> & unknown;
|
|
70
|
+
/** Dot-paths implementation */
|
|
71
|
+
type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
|
|
72
|
+
/** Extract Keys from any kind of type, including objects, arrays, tuples. */
|
|
73
|
+
type KeyOf<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? U extends TAllowedTypes ? IsTuple<T> extends true ? keyof Omit<T, keyof any[]> : ArrayIndexes : never : T extends PrimitivesAndNativeObjects ? never : {
|
|
74
|
+
[Key in keyof T]: T[Key] extends TAllowedTypes ? Key : never;
|
|
75
|
+
}[keyof T];
|
|
70
76
|
/** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
|
|
71
|
-
type DotPaths<T, TAllowedTypes = any> =
|
|
77
|
+
type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplementation<T, TAllowedTypes>;
|
|
72
78
|
/**
|
|
73
79
|
* --------------------------------------------
|
|
74
80
|
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
@@ -89,10 +95,10 @@ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath ex
|
|
|
89
95
|
* --------------------------------------------
|
|
90
96
|
*/
|
|
91
97
|
/** Value of a dot-path update object, can be the value or a function. */
|
|
92
|
-
type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => ValueInPath<TObject, TPath>);
|
|
98
|
+
type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> extends never ? never : ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => ValueInPath<TObject, TPath>);
|
|
93
99
|
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
94
100
|
type UpdateObject<TObject, TPaths extends PropertyKey = DotPaths<TObject>> = {
|
|
95
|
-
[
|
|
101
|
+
[Path in TPaths]?: UpdateValue<TObject, Path>;
|
|
96
102
|
};
|
|
97
103
|
/**
|
|
98
104
|
* --------------------------------------------
|
|
@@ -133,7 +139,7 @@ type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T exten
|
|
|
133
139
|
}>;
|
|
134
140
|
} & unknown;
|
|
135
141
|
/** New expanded value after aplying updates including new properties */
|
|
136
|
-
type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> :
|
|
142
|
+
type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject>;
|
|
137
143
|
|
|
138
144
|
type Select = {
|
|
139
145
|
<T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
|
|
@@ -171,4 +177,9 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
|
171
177
|
declare const parseStringIndex: (v: string) => number | null;
|
|
172
178
|
declare const isStringIndex: (v: string) => boolean;
|
|
173
179
|
|
|
174
|
-
|
|
180
|
+
type StrictCallback<T> = (v: T) => [T, 'STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function'];
|
|
181
|
+
type StrictCallbackReturn<T> = [T, 'STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function'] & unknown;
|
|
182
|
+
/** Parameter allowing to strictly check types for callback functions as arguments. It returns a tuple where the first index is the value you want as return type. */
|
|
183
|
+
declare const strictCallback: <T>(v: T) => [T, "STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function"];
|
|
184
|
+
|
|
185
|
+
export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type StrictCallback, type StrictCallbackReturn, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, strictCallback, update };
|
package/dist/index.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ type Options = {
|
|
|
21
21
|
onNewIndexes?: OptionValues;
|
|
22
22
|
onPathsIntersectedByPrimitiveValues?: OptionValues;
|
|
23
23
|
};
|
|
24
|
-
type ImmutableUpdate = <TObject
|
|
24
|
+
type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends PropertyKey = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
|
|
25
25
|
/**
|
|
26
26
|
* Creates a new object applying immutable updates using dot-path syntax.
|
|
27
27
|
* Can add or skip new props or indexes depending on options.
|
|
@@ -41,7 +41,7 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
|
|
|
41
41
|
* @param dotPathUpdateObject Dot Path Update Object.
|
|
42
42
|
* @returns New Updated Object.
|
|
43
43
|
*/
|
|
44
|
-
declare const
|
|
44
|
+
declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* --------------------------------------------
|
|
@@ -51,7 +51,7 @@ declare const extendableUpdate: ExtendableImmutableUpdate;
|
|
|
51
51
|
/** Primitives and browser native objects. */
|
|
52
52
|
type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
|
|
53
53
|
/** Array indexes type. */
|
|
54
|
-
type ArrayIndexes = number | LiteralIndex;
|
|
54
|
+
type ArrayIndexes = number | `${number}` | LiteralIndex;
|
|
55
55
|
/** To check if a ReadonlyArray is a tuple or not. */
|
|
56
56
|
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
|
|
57
57
|
/**
|
|
@@ -60,15 +60,21 @@ type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false
|
|
|
60
60
|
* --------------------------------------------
|
|
61
61
|
*/
|
|
62
62
|
/** Dot-paths of an array. */
|
|
63
|
-
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ?
|
|
63
|
+
type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? ArrayIndexes : ArrayIndexes | `${ArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPathsImplementation<TInnerType, TAllowedTypes>}`;
|
|
64
64
|
/** Dot-paths of an object. */
|
|
65
65
|
type ObjectPaths<T, TAllowedTypes> = {
|
|
66
|
-
[Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${
|
|
67
|
-
}[keyof T & (string | number)];
|
|
66
|
+
[Key in keyof T & (string | number)]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}` : `${Key}.${DotPathsImplementation<T[Key], TAllowedTypes>}`;
|
|
67
|
+
}[keyof T & (string | number)] & unknown;
|
|
68
68
|
/** Dot-paths of a tuple. */
|
|
69
|
-
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes
|
|
69
|
+
type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes> & unknown;
|
|
70
|
+
/** Dot-paths implementation */
|
|
71
|
+
type DotPathsImplementation<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
|
|
72
|
+
/** Extract Keys from any kind of type, including objects, arrays, tuples. */
|
|
73
|
+
type KeyOf<T, TAllowedTypes = any> = T extends ReadonlyArray<infer U> ? U extends TAllowedTypes ? IsTuple<T> extends true ? keyof Omit<T, keyof any[]> : ArrayIndexes : never : T extends PrimitivesAndNativeObjects ? never : {
|
|
74
|
+
[Key in keyof T]: T[Key] extends TAllowedTypes ? Key : never;
|
|
75
|
+
}[keyof T];
|
|
70
76
|
/** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
|
|
71
|
-
type DotPaths<T, TAllowedTypes = any> =
|
|
77
|
+
type DotPaths<T, TAllowedTypes = any> = KeyOf<T, TAllowedTypes> | DotPathsImplementation<T, TAllowedTypes>;
|
|
72
78
|
/**
|
|
73
79
|
* --------------------------------------------
|
|
74
80
|
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
@@ -89,10 +95,10 @@ type ReturnedValueInPath<TObject, TPath> = TPath extends '' ? TObject : TPath ex
|
|
|
89
95
|
* --------------------------------------------
|
|
90
96
|
*/
|
|
91
97
|
/** Value of a dot-path update object, can be the value or a function. */
|
|
92
|
-
type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => ValueInPath<TObject, TPath>);
|
|
98
|
+
type UpdateValue<TObject, TPath> = ValueInPath<TObject, TPath> extends never ? never : ValueInPath<TObject, TPath> | ((fieldValue: ValueInPath<TObject, TPath>) => ValueInPath<TObject, TPath>);
|
|
93
99
|
/** Dot-path update object, used to give updates to the updateImmutably function. */
|
|
94
100
|
type UpdateObject<TObject, TPaths extends PropertyKey = DotPaths<TObject>> = {
|
|
95
|
-
[
|
|
101
|
+
[Path in TPaths]?: UpdateValue<TObject, Path>;
|
|
96
102
|
};
|
|
97
103
|
/**
|
|
98
104
|
* --------------------------------------------
|
|
@@ -133,7 +139,7 @@ type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T exten
|
|
|
133
139
|
}>;
|
|
134
140
|
} & unknown;
|
|
135
141
|
/** New expanded value after aplying updates including new properties */
|
|
136
|
-
type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> :
|
|
142
|
+
type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject>;
|
|
137
143
|
|
|
138
144
|
type Select = {
|
|
139
145
|
<T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
|
|
@@ -171,4 +177,9 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
|
|
|
171
177
|
declare const parseStringIndex: (v: string) => number | null;
|
|
172
178
|
declare const isStringIndex: (v: string) => boolean;
|
|
173
179
|
|
|
174
|
-
|
|
180
|
+
type StrictCallback<T> = (v: T) => [T, 'STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function'];
|
|
181
|
+
type StrictCallbackReturn<T> = [T, 'STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function'] & unknown;
|
|
182
|
+
/** Parameter allowing to strictly check types for callback functions as arguments. It returns a tuple where the first index is the value you want as return type. */
|
|
183
|
+
declare const strictCallback: <T>(v: T) => [T, "STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function"];
|
|
184
|
+
|
|
185
|
+
export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type StrictCallback, type StrictCallbackReturn, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, strictCallback, update };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var x=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var D=(t,e)=>{for(var r in e)x(t,r,{get:e[r],enumerable:!0})},N=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of V(e))!v.call(t,o)&&o!==r&&x(t,o,{get:()=>e[o],enumerable:!(n=L(e,o))||n.enumerable});return t};var $=t=>N(x({},"__esModule",{value:!0}),t);var B={};D(B,{experimental_extendableUpdate:()=>A,isObjectLiteral:()=>l,isStringIndex:()=>d,parseStringIndex:()=>S,remove:()=>I,select:()=>w,set:()=>K,splitPathAtLastKey:()=>j,strictCallback:()=>k,update:()=>R});module.exports=$(B);var I=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(e)?e:[e];if(Array.isArray(t)){let o=Symbol(),i=[...t];return r.forEach(s=>{i[s]=o}),i.filter(s=>s!==o)}let n={...t};return r.forEach(o=>delete n[o]),n};var w=(t,e)=>{if(typeof e!="string")throw new Error("path is not a string");if(!e)return t;let r=e.split("."),n=t;for(;r.length;){let o=r.join(".");if(n?.hasOwnProperty(o))return n[o];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var j=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",n=t.slice(e);return[r,n]};var K=t=>()=>t;var l=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var S=t=>{let e=parseInt(t);return e.toString()===t?e:null},d=t=>parseInt(t).toString()===t;var U=t=>JSON.stringify(t),T=t=>Array.isArray(t),E=(t,e,r={})=>{if(!l(t)&&!T(t))throw new Error(`"${U(t)}" is not an literalObject/array`);if(!l(e))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(t)?[...t]:{...t},o={};for(let[i,f]of Object.entries(e)){if(i==="")continue;let s=i.split("."),y=o,p=n;for(;s.length>1;){let b=s.join(".");if(Object.hasOwn(p,b)){s=[b];break}let a=s[0],h=p[a],O=l(h),m=T(h);if(!O&&!m&&!(h===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${a}" in "${i}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!y[a]){if(!O&&!m){let g=T(p)&&d(a),C=d(s[1]),P=g?r.onNewIndexes:r.onNewProps;if(P==="error")throw new Error(`${a} doesn't exist in "${i}"`);if(P==="skip")continue;p[a]=C?[]:{}}else p[a]=m?[...h]:{...h};y[a]={}}p=p[a],y=y[a],s.shift()}let c=s[0],u=p;if(!Object.hasOwn(u,c)){let a=T(u)&&d(c)?r.onNewIndexes:r.onNewProps;if(a==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${i}". Its value is: "${U(u)}"`);if(a==="skip")continue}u[c]=typeof f=="function"?f(u[c]):f,delete y[c]}return n},R=(t,e,r)=>E(t,e,r),A=(t,e)=>E(t,e);var k=t=>[t,"STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function"];0&&(module.exports={experimental_extendableUpdate,isObjectLiteral,isStringIndex,parseStringIndex,remove,select,set,splitPathAtLastKey,strictCallback,update});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var K=(e
|
|
1
|
+
var K=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(e)?e:[e];if(Array.isArray(t)){let s=Symbol(),i=[...t];return r.forEach(a=>{i[a]=s}),i.filter(a=>a!==s)}let n={...t};return r.forEach(s=>delete n[s]),n};var S=(t,e)=>{if(typeof e!="string")throw new Error("path is not a string");if(!e)return t;let r=e.split("."),n=t;for(;r.length;){let s=r.join(".");if(n?.hasOwnProperty(s))return n[s];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var U=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",n=t.slice(e);return[r,n]};var E=t=>()=>t;var u=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var R=t=>{let e=parseInt(t);return e.toString()===t?e:null},h=t=>parseInt(t).toString()===t;var P=t=>JSON.stringify(t),T=t=>Array.isArray(t),I=(t,e,r={})=>{if(!u(t)&&!T(t))throw new Error(`"${P(t)}" is not an literalObject/array`);if(!u(e))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(t)?[...t]:{...t},s={};for(let[i,f]of Object.entries(e)){if(i==="")continue;let a=i.split("."),l=s,p=n;for(;a.length>1;){let b=a.join(".");if(Object.hasOwn(p,b)){a=[b];break}let o=a[0],y=p[o],x=u(y),m=T(y);if(!x&&!m&&!(y===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${o}" in "${i}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!l[o]){if(!x&&!m){let w=T(p)&&h(o),j=h(a[1]),O=w?r.onNewIndexes:r.onNewProps;if(O==="error")throw new Error(`${o} doesn't exist in "${i}"`);if(O==="skip")continue;p[o]=j?[]:{}}else p[o]=m?[...y]:{...y};l[o]={}}p=p[o],l=l[o],a.shift()}let c=a[0],d=p;if(!Object.hasOwn(d,c)){let o=T(d)&&h(c)?r.onNewIndexes:r.onNewProps;if(o==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${i}". Its value is: "${P(d)}"`);if(o==="skip")continue}d[c]=typeof f=="function"?f(d[c]):f,delete l[c]}return n},A=(t,e,r)=>I(t,e,r),k=(t,e)=>I(t,e);var g=t=>[t,"STRICTNESS RETURN ERROR: wrap the return value with the first parameter of the callback function"];export{k as experimental_extendableUpdate,u as isObjectLiteral,h as isStringIndex,R as parseStringIndex,K as remove,S as select,E as set,U as splitPathAtLastKey,g as strictCallback,A as update};
|