maraj 2.3.0 → 2.4.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/dist/index.d.mts CHANGED
@@ -1,51 +1,72 @@
1
1
  /**
2
- * Paths implementation
2
+ * --------------------------------------------
3
+ * UTILITIES
4
+ * --------------------------------------------
3
5
  */
4
6
  type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
7
+ type LiteralIndex = '${number}';
8
+ type ArrayIndexes = `${number}` | LiteralIndex;
5
9
  type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
6
- type Index = '_INDEX_';
7
- type ArrayIndexPaths = `${number}` | Index;
8
- type ArrayPaths<TInnerType> = `${ArrayIndexPaths}` | `${ArrayIndexPaths}.${Flatten<TInnerType>}`;
9
- type ObjectPaths<T> = {
10
- [Key in keyof T & string]: `${Key}` | `${Key}.${Flatten<T[Key]>}`;
10
+ /**
11
+ * --------------------------------------------
12
+ * DOT PATH IMPLEMENTATION
13
+ * --------------------------------------------
14
+ */
15
+ type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${Flatten<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${Flatten<TInnerType, TAllowedTypes>}`;
16
+ type ObjectPaths<T, TAllowedTypes> = {
17
+ [Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${Flatten<T[Key], TAllowedTypes>}` : `${Key}.${Flatten<T[Key], TAllowedTypes>}`;
11
18
  }[keyof T & string];
12
- type TuplePaths<T> = ObjectPaths<Omit<T, keyof any[]>>;
13
- type Flatten<T> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T> : ArrayPaths<U> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T>;
14
- type DotPaths<T> = Flatten<T>;
19
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
20
+ type Flatten<T, TAllowedTypes> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
21
+ type DotPaths<T, TAllowedTypes = any> = Flatten<T, TAllowedTypes>;
22
+ /**
23
+ * --------------------------------------------
24
+ * TO KNOW IF A PATH IS INDEXING ARRAY OR NOT
25
+ * --------------------------------------------
26
+ */
15
27
  /**
16
- * Type to know when a path is indexing an array
28
+ * --------------------------------------------
29
+ * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
30
+ * --------------------------------------------
17
31
  */
18
- type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` | `${string}.${T}` | `${string}.${T}.${string}`;
32
+ type NestedType<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedType<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? NestedType<U, R> : never : never : never;
33
+ type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
19
34
  /**
20
- * Nested Property Type implementation
35
+ * --------------------------------------------
36
+ * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
37
+ * --------------------------------------------
21
38
  */
22
- type NestedProperty<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends (`${number}` | Index) ? TObject extends Array<infer U> ? TPath extends `${number}.${infer K}` ? K extends keyof U ? NestedProperty<U, K> : never : U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedProperty<Required<TObject>[K], R> : K extends `${number}` | Index ? TObject extends Array<infer U> ? NestedProperty<U, R> : never : never : never;
23
- type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedProperty<TObject, TPath>;
24
- type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = TPath extends PathIndexingArray<Index> ? [`PATH ERROR: replace _INDEX_ with a NUMBER`, ValueInDotPath<TObject, TPath>, ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>)] : ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
39
+ type ReturnedNestedValue<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedNestedValue<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedNestedValue<U, R> | undefined) : never : never : never;
40
+ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
25
41
  /**
26
- * Update Object Type Implementation
42
+ * --------------------------------------------
43
+ * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
44
+ * --------------------------------------------
27
45
  */
46
+ type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
28
47
  type DotPathUpdateObject<TObject> = {
29
- [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
48
+ [TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
30
49
  };
31
50
  /**
32
- * Optional and Required keys
51
+ * --------------------------------------------
52
+ * REQUIRED OR OPTIONAL KEYS
53
+ * --------------------------------------------
33
54
  */
34
- type NonPartialKeys = string | number | boolean | symbol | bigint | null | object;
35
- type OptionalKeys<T> = {
55
+ type NonPartialKeys = null | object;
56
+ type RetrieveOptionalKeys<T> = {
36
57
  [K in keyof T & string]: T[K] extends NonPartialKeys ? never : K;
37
58
  }[keyof T & string];
38
- type RequiredKeys<T> = {
59
+ type RetrieveRequiredKeys<T> = {
39
60
  [K in keyof T & string]: T[K] extends NonPartialKeys ? K : never;
40
61
  }[keyof T & string];
41
62
 
42
- type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (OptionalKeys<T> | (OptionalKeys<T>)[])) => T;
63
+ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (RetrieveOptionalKeys<T> | (RetrieveOptionalKeys<T>)[])) => T;
43
64
  /**
44
65
  * Removes properties or indexes, order doesn't matter.
45
66
  */
46
67
  declare const remove: Remove;
47
68
 
48
- type Select = <T, TPath extends (DotPaths<T> | '')>(state: T, path: TPath) => TPath extends PathIndexingArray<Index> ? never : TPath extends PathIndexingArray<number> ? ValueInDotPath<T, TPath> | undefined : ValueInDotPath<T, TPath>;
69
+ type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
49
70
  /**
50
71
  * Returns the value from a given dot path of an object/array.
51
72
  * Returns undefined if the provided path doesnt exist.
@@ -74,4 +95,4 @@ type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject
74
95
  */
75
96
  declare const updateImmutably: UpdateImmutably;
76
97
 
77
- export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type RequiredKeys, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
98
+ export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type PrimitivesAndNativeObjects, type RetrieveOptionalKeys, type RetrieveRequiredKeys, type ReturnedValueInDotPath, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
package/dist/index.d.ts CHANGED
@@ -1,51 +1,72 @@
1
1
  /**
2
- * Paths implementation
2
+ * --------------------------------------------
3
+ * UTILITIES
4
+ * --------------------------------------------
3
5
  */
4
6
  type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
7
+ type LiteralIndex = '${number}';
8
+ type ArrayIndexes = `${number}` | LiteralIndex;
5
9
  type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
6
- type Index = '_INDEX_';
7
- type ArrayIndexPaths = `${number}` | Index;
8
- type ArrayPaths<TInnerType> = `${ArrayIndexPaths}` | `${ArrayIndexPaths}.${Flatten<TInnerType>}`;
9
- type ObjectPaths<T> = {
10
- [Key in keyof T & string]: `${Key}` | `${Key}.${Flatten<T[Key]>}`;
10
+ /**
11
+ * --------------------------------------------
12
+ * DOT PATH IMPLEMENTATION
13
+ * --------------------------------------------
14
+ */
15
+ type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${Flatten<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${Flatten<TInnerType, TAllowedTypes>}`;
16
+ type ObjectPaths<T, TAllowedTypes> = {
17
+ [Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${Flatten<T[Key], TAllowedTypes>}` : `${Key}.${Flatten<T[Key], TAllowedTypes>}`;
11
18
  }[keyof T & string];
12
- type TuplePaths<T> = ObjectPaths<Omit<T, keyof any[]>>;
13
- type Flatten<T> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T> : ArrayPaths<U> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T>;
14
- type DotPaths<T> = Flatten<T>;
19
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
20
+ type Flatten<T, TAllowedTypes> = T extends ReadonlyArray<infer U> ? IsTuple<T> extends true ? TuplePaths<T, TAllowedTypes> : ArrayPaths<U, TAllowedTypes> : T extends PrimitivesAndNativeObjects ? never : ObjectPaths<T, TAllowedTypes>;
21
+ type DotPaths<T, TAllowedTypes = any> = Flatten<T, TAllowedTypes>;
22
+ /**
23
+ * --------------------------------------------
24
+ * TO KNOW IF A PATH IS INDEXING ARRAY OR NOT
25
+ * --------------------------------------------
26
+ */
15
27
  /**
16
- * Type to know when a path is indexing an array
28
+ * --------------------------------------------
29
+ * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
30
+ * --------------------------------------------
17
31
  */
18
- type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` | `${string}.${T}` | `${string}.${T}.${string}`;
32
+ type NestedType<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedType<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? NestedType<U, R> : never : never : never;
33
+ type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
19
34
  /**
20
- * Nested Property Type implementation
35
+ * --------------------------------------------
36
+ * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
37
+ * --------------------------------------------
21
38
  */
22
- type NestedProperty<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends (`${number}` | Index) ? TObject extends Array<infer U> ? TPath extends `${number}.${infer K}` ? K extends keyof U ? NestedProperty<U, K> : never : U : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? NestedProperty<Required<TObject>[K], R> : K extends `${number}` | Index ? TObject extends Array<infer U> ? NestedProperty<U, R> : never : never : never;
23
- type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedProperty<TObject, TPath>;
24
- type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = TPath extends PathIndexingArray<Index> ? [`PATH ERROR: replace _INDEX_ with a NUMBER`, ValueInDotPath<TObject, TPath>, ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>)] : ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
39
+ type ReturnedNestedValue<TObject, TPath> = TPath extends '' ? TObject : TPath extends keyof TObject ? TObject[TPath] : TPath extends LiteralIndex ? never : TPath extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (U | undefined) : never : TPath extends `${infer K}.${infer R}` ? K extends keyof TObject ? ReturnedNestedValue<TObject[K], R> : K extends LiteralIndex ? never : K extends `${number}` ? TObject extends ReadonlyArray<infer U> ? (ReturnedNestedValue<U, R> | undefined) : never : never : never;
40
+ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
25
41
  /**
26
- * Update Object Type Implementation
42
+ * --------------------------------------------
43
+ * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
44
+ * --------------------------------------------
27
45
  */
46
+ type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
28
47
  type DotPathUpdateObject<TObject> = {
29
- [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
48
+ [TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
30
49
  };
31
50
  /**
32
- * Optional and Required keys
51
+ * --------------------------------------------
52
+ * REQUIRED OR OPTIONAL KEYS
53
+ * --------------------------------------------
33
54
  */
34
- type NonPartialKeys = string | number | boolean | symbol | bigint | null | object;
35
- type OptionalKeys<T> = {
55
+ type NonPartialKeys = null | object;
56
+ type RetrieveOptionalKeys<T> = {
36
57
  [K in keyof T & string]: T[K] extends NonPartialKeys ? never : K;
37
58
  }[keyof T & string];
38
- type RequiredKeys<T> = {
59
+ type RetrieveRequiredKeys<T> = {
39
60
  [K in keyof T & string]: T[K] extends NonPartialKeys ? K : never;
40
61
  }[keyof T & string];
41
62
 
42
- type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (OptionalKeys<T> | (OptionalKeys<T>)[])) => T;
63
+ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (RetrieveOptionalKeys<T> | (RetrieveOptionalKeys<T>)[])) => T;
43
64
  /**
44
65
  * Removes properties or indexes, order doesn't matter.
45
66
  */
46
67
  declare const remove: Remove;
47
68
 
48
- type Select = <T, TPath extends (DotPaths<T> | '')>(state: T, path: TPath) => TPath extends PathIndexingArray<Index> ? never : TPath extends PathIndexingArray<number> ? ValueInDotPath<T, TPath> | undefined : ValueInDotPath<T, TPath>;
69
+ type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
49
70
  /**
50
71
  * Returns the value from a given dot path of an object/array.
51
72
  * Returns undefined if the provided path doesnt exist.
@@ -74,4 +95,4 @@ type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject
74
95
  */
75
96
  declare const updateImmutably: UpdateImmutably;
76
97
 
77
- export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type RequiredKeys, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
98
+ export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type PrimitivesAndNativeObjects, type RetrieveOptionalKeys, type RetrieveRequiredKeys, type ReturnedValueInDotPath, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var O=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of D(e))!w.call(t,o)&&o!==r&&f(t,o,{get:()=>e[o],enumerable:!(a=I(e,o))||a.enumerable});return t};var g=t=>j(f({},"__esModule",{value:!0}),t);var v={};O(v,{remove:()=>x,select:()=>T,set:()=>b,splitPathAtLastKey:()=>m,updateImmutably:()=>A});module.exports=g(v);var x=(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(),y=[...t];return r.forEach(n=>{y[n]=o}),y.filter(n=>n!==o)}let a={...t};return r.forEach(o=>delete a[o]),a};var T=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((r,a)=>{if(r?.hasOwnProperty(a))return r[a]},t);var m=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",a=t.slice(e);return[r,a]};var b=t=>()=>t;var A=(t,e,r)=>{if(t?.constructor!==Object&&!Array.isArray(t))throw new Error(`${t} is not an object/array`);if(!e)return t;let a=Array.isArray(t)?[...t]:{...t},o={};for(let[y,u]of Object.entries(e)){if(!y)continue;let n=y.split("."),s=n.pop(),l=o,h=a;for(let c=0;c<n.length;c++){let i=n[c],p=h[i],K=typeof p=="object"&&(p.constructor===Object||Array.isArray(p)),P=r&&p===void 0;if(!K&&!P)throw new Error(`Invalid path provided: "${i}" is not an object/array in "${y}"`);l[i]||(P?h[i]=isNaN(n[c+1]??s)?{}:[]:h[i]=Array.isArray(p)?[...p]:{...p},l[i]={}),h=h[i],l=l[i]}let d=h;if(!r){if(Array.isArray(d)&&!(s<d.length))throw new Error(`Index ${s} doesnt exist in the path ${n.join(".")} because its value is ${s}, use spread operator for adding new indexes`);if(!d.hasOwnProperty(s))throw new Error(`Property ${s} doesnt exist in the path ${n.join(".")} because its value is ${d}`)}d[s]=typeof u=="function"?u(d[s],a):u,delete l[s]}return a};0&&(module.exports={remove,select,set,splitPathAtLastKey,updateImmutably});
1
+ "use strict";var f=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var D=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},v=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of j(e))!w.call(t,a)&&a!==r&&f(t,a,{get:()=>e[a],enumerable:!(o=O(e,a))||o.enumerable});return t};var I=t=>v(f({},"__esModule",{value:!0}),t);var R={};D(R,{remove:()=>b,select:()=>T,set:()=>K,splitPathAtLastKey:()=>P,updateImmutably:()=>x});module.exports=I(R);var b=(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 a=Symbol(),y=[...t];return r.forEach(n=>{y[n]=a}),y.filter(n=>n!==a)}let o={...t};return r.forEach(a=>delete o[a]),o};var T=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((r,o)=>{if(r?.hasOwnProperty(o))return r[o]},t);var P=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",o=t.slice(e);return[r,o]};var K=t=>()=>t;var x=(t,e,r)=>{if(t?.constructor!==Object&&!Array.isArray(t))throw new Error(`${t} is not an object/array`);if(!e)return t;let o=Array.isArray(t)?[...t]:{...t},a={};for(let[y,d]of Object.entries(e)){if(!y)continue;let n=y.split("."),s=n.pop(),u=a,c=o;for(let h=0;h<n.length;h++){let i=n[h],p=c[i],A=typeof p=="object"&&(p.constructor===Object||Array.isArray(p)),m=r&&p===void 0;if(!A&&!m)throw new Error(`Invalid path provided: "${i}" is not an object/array in "${y}"`);u[i]||(m?c[i]=isNaN(n[h+1]??s)?{}:[]:c[i]=Array.isArray(p)?[...p]:{...p},u[i]={}),c=c[i],u=u[i]}let l=c;if(!r){if(Array.isArray(l)&&!(s<l.length))throw new Error(`Index ${s} doesnt exist in the path ${n.join(".")} because its value is ${s}, use spread operator for adding new indexes`);if(!l.hasOwnProperty(s))throw new Error(`Property ${s} doesnt exist in the path ${n.join(".")} because its value is ${l}`)}l[s]=typeof d=="function"?d(l[s],o):d,delete u[s]}return o};0&&(module.exports={remove,select,set,splitPathAtLastKey,updateImmutably});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var x=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let a=Array.isArray(e)?e:[e];if(Array.isArray(t)){let p=Symbol(),y=[...t];return a.forEach(o=>{y[o]=p}),y.filter(o=>o!==p)}let r={...t};return a.forEach(p=>delete r[p]),r};var T=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((a,r)=>{if(a?.hasOwnProperty(r))return a[r]},t);var m=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,a=e?t.slice(0,e-1):"",r=t.slice(e);return[a,r]};var b=t=>()=>t;var A=(t,e,a)=>{if(t?.constructor!==Object&&!Array.isArray(t))throw new Error(`${t} is not an object/array`);if(!e)return t;let r=Array.isArray(t)?[...t]:{...t},p={};for(let[y,u]of Object.entries(e)){if(!y)continue;let o=y.split("."),n=o.pop(),l=p,h=r;for(let c=0;c<o.length;c++){let s=o[c],i=h[s],P=typeof i=="object"&&(i.constructor===Object||Array.isArray(i)),f=a&&i===void 0;if(!P&&!f)throw new Error(`Invalid path provided: "${s}" is not an object/array in "${y}"`);l[s]||(f?h[s]=isNaN(o[c+1]??n)?{}:[]:h[s]=Array.isArray(i)?[...i]:{...i},l[s]={}),h=h[s],l=l[s]}let d=h;if(!a){if(Array.isArray(d)&&!(n<d.length))throw new Error(`Index ${n} doesnt exist in the path ${o.join(".")} because its value is ${n}, use spread operator for adding new indexes`);if(!d.hasOwnProperty(n))throw new Error(`Property ${n} doesnt exist in the path ${o.join(".")} because its value is ${d}`)}d[n]=typeof u=="function"?u(d[n],r):u,delete l[n]}return r};export{x as remove,T as select,b as set,m as splitPathAtLastKey,A as updateImmutably};
1
+ var b=(t,e)=>{if(!Array.isArray(t)&&t?.constructor!==Object)throw'Wrong type provided in "remove" function';let o=Array.isArray(e)?e:[e];if(Array.isArray(t)){let p=Symbol(),y=[...t];return o.forEach(a=>{y[a]=p}),y.filter(a=>a!==p)}let r={...t};return o.forEach(p=>delete r[p]),r};var T=(t,e)=>!e||typeof e!="string"?t:e.split(".").reduce((o,r)=>{if(o?.hasOwnProperty(r))return o[r]},t);var P=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,o=e?t.slice(0,e-1):"",r=t.slice(e);return[o,r]};var K=t=>()=>t;var x=(t,e,o)=>{if(t?.constructor!==Object&&!Array.isArray(t))throw new Error(`${t} is not an object/array`);if(!e)return t;let r=Array.isArray(t)?[...t]:{...t},p={};for(let[y,d]of Object.entries(e)){if(!y)continue;let a=y.split("."),n=a.pop(),u=p,c=r;for(let h=0;h<a.length;h++){let s=a[h],i=c[s],m=typeof i=="object"&&(i.constructor===Object||Array.isArray(i)),f=o&&i===void 0;if(!m&&!f)throw new Error(`Invalid path provided: "${s}" is not an object/array in "${y}"`);u[s]||(f?c[s]=isNaN(a[h+1]??n)?{}:[]:c[s]=Array.isArray(i)?[...i]:{...i},u[s]={}),c=c[s],u=u[s]}let l=c;if(!o){if(Array.isArray(l)&&!(n<l.length))throw new Error(`Index ${n} doesnt exist in the path ${a.join(".")} because its value is ${n}, use spread operator for adding new indexes`);if(!l.hasOwnProperty(n))throw new Error(`Property ${n} doesnt exist in the path ${a.join(".")} because its value is ${l}`)}l[n]=typeof d=="function"?d(l[n],r):d,delete u[n]}return r};export{b as remove,T as select,K as set,P as splitPathAtLastKey,x as updateImmutably};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "A lightweight 500 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",