maraj 2.3.0 → 2.4.1

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,54 +1,91 @@
1
1
  /**
2
- * Paths implementation
2
+ * --------------------------------------------
3
+ * UTILITIES
4
+ * --------------------------------------------
3
5
  */
6
+ /** Primitives and browser native objects. */
4
7
  type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
8
+ /** Index literal type to improve DX at managing updates. */
9
+ type LiteralIndex = '${number}';
10
+ /** Array indexes type. */
11
+ type ArrayIndexes = `${number}` | LiteralIndex;
12
+ /** To check if a ReadonlyArray is a tuple or not. */
5
13
  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]>}`;
14
+ /**
15
+ * --------------------------------------------
16
+ * DOT PATH IMPLEMENTATION
17
+ * --------------------------------------------
18
+ */
19
+ /** Dot-paths of an array. */
20
+ type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
21
+ /** Dot-paths of an object. */
22
+ type ObjectPaths<T, TAllowedTypes> = {
23
+ [Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPaths<T[Key], TAllowedTypes>}` : `${Key}.${DotPaths<T[Key], TAllowedTypes>}`;
11
24
  }[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>;
25
+ /** Dot-paths of a tuple. */
26
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
27
+ /** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
28
+ type DotPaths<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>;
15
29
  /**
16
- * Type to know when a path is indexing an array
30
+ * --------------------------------------------
31
+ * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
32
+ * --------------------------------------------
17
33
  */
18
- type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` | `${string}.${T}` | `${string}.${T}.${string}`;
34
+ /** Nested type implementation. */
35
+ 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;
36
+ /** Value of a nested property of a given dot-path. */
37
+ type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
19
38
  /**
20
- * Nested Property Type implementation
39
+ * --------------------------------------------
40
+ * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
41
+ * --------------------------------------------
21
42
  */
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>);
43
+ /** Returned nested type implementation. */
44
+ 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;
45
+ /** Value of a RETURNED nested property of a given dot-path (so when accessing arrays can be undefined). */
46
+ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
25
47
  /**
26
- * Update Object Type Implementation
48
+ * --------------------------------------------
49
+ * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
50
+ * --------------------------------------------
27
51
  */
52
+ /** Value of a dot-path update object, can be the value or a function. */
53
+ type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
54
+ /** Dot-path update object, used to give updates to the updateImmutably function. */
28
55
  type DotPathUpdateObject<TObject> = {
29
- [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
56
+ [TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
30
57
  };
31
58
  /**
32
- * Optional and Required keys
59
+ * --------------------------------------------
60
+ * REQUIRED OR OPTIONAL KEYS
61
+ * --------------------------------------------
33
62
  */
34
- type NonPartialKeys = string | number | boolean | symbol | bigint | null | object;
35
- type OptionalKeys<T> = {
63
+ /** All types instead of undefined. */
64
+ type NonPartialKeys = null | object;
65
+ /** Returns only the optional keys of an object. */
66
+ type RetrieveOptionalKeys<T> = {
36
67
  [K in keyof T & string]: T[K] extends NonPartialKeys ? never : K;
37
68
  }[keyof T & string];
38
- type RequiredKeys<T> = {
69
+ /** Returns only the required keys of an object. */
70
+ type RetrieveRequiredKeys<T> = {
39
71
  [K in keyof T & string]: T[K] extends NonPartialKeys ? K : never;
40
72
  }[keyof T & string];
41
73
 
42
- type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (OptionalKeys<T> | (OptionalKeys<T>)[])) => T;
74
+ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (RetrieveOptionalKeys<T> | (RetrieveOptionalKeys<T>)[])) => T;
43
75
  /**
44
76
  * Removes properties or indexes, order doesn't matter.
77
+ * @param value Object/array from where to remove keys/indexes shallowly.
78
+ * @param keys Keys to remove.
79
+ * @returns New object/array without the provided keys/indexes.
45
80
  */
46
81
  declare const remove: Remove;
47
82
 
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>;
83
+ type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
49
84
  /**
50
- * Returns the value from a given dot path of an object/array.
51
- * Returns undefined if the provided path doesnt exist.
85
+ * Returns the value from a given dot path of an object/array or undefined if doesn't exist.
86
+ * @param state State from which retrieve the value.
87
+ * @param path Path indicating the value to extract.
88
+ * @returns Value of the provided path or undefined.
52
89
  */
53
90
  declare const select: Select;
54
91
 
@@ -56,22 +93,28 @@ type PathWithoutLastKey = string;
56
93
  type LastKey = string;
57
94
  type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
58
95
  /**
59
- * Splits string dotPath into ID and RemainingPath
60
- */
96
+ * Splits a dot path string into its last key and its remaining keys ('' if none).
97
+ * @param dotPath A dot path.
98
+ * @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
99
+ */
61
100
  declare const splitPathAtLastKey: SplitDotPathAtLastKey;
62
101
 
63
102
  type Set = <T>(newValue: T) => (value: T) => T;
64
103
  /**
65
- * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays
104
+ * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
105
+ * @param newValue New value.
106
+ * @returns The same value.
66
107
  */
67
108
  declare const set: Set;
68
109
 
69
110
  type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T>, addNonExistentPropsAndIndexes?: true) => T;
70
111
  /**
71
- * Creates a new updated object/array applying the changes provided in the update object.
72
- * It doesnt modify the original input.
73
- * If any of the paths doesnt exists it returns
112
+ * Creates a new object applying immutable updates using dot-path syntax.
113
+ * @param state Original State Object.
114
+ * @param updates Dot Path Update Object.
115
+ * @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
116
+ * @returns New Updated Object.
74
117
  */
75
118
  declare const updateImmutably: UpdateImmutably;
76
119
 
77
- export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type RequiredKeys, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
120
+ 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,54 +1,91 @@
1
1
  /**
2
- * Paths implementation
2
+ * --------------------------------------------
3
+ * UTILITIES
4
+ * --------------------------------------------
3
5
  */
6
+ /** Primitives and browser native objects. */
4
7
  type PrimitivesAndNativeObjects = null | undefined | string | number | boolean | symbol | bigint | Date | FileList | File;
8
+ /** Index literal type to improve DX at managing updates. */
9
+ type LiteralIndex = '${number}';
10
+ /** Array indexes type. */
11
+ type ArrayIndexes = `${number}` | LiteralIndex;
12
+ /** To check if a ReadonlyArray is a tuple or not. */
5
13
  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]>}`;
14
+ /**
15
+ * --------------------------------------------
16
+ * DOT PATH IMPLEMENTATION
17
+ * --------------------------------------------
18
+ */
19
+ /** Dot-paths of an array. */
20
+ type ArrayPaths<TInnerType, TAllowedTypes> = TInnerType extends TAllowedTypes ? TInnerType extends PrimitivesAndNativeObjects ? `${ArrayIndexes}` : `${ArrayIndexes}` | `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}` : `${ArrayIndexes}.${DotPaths<TInnerType, TAllowedTypes>}`;
21
+ /** Dot-paths of an object. */
22
+ type ObjectPaths<T, TAllowedTypes> = {
23
+ [Key in keyof T & string]: T[Key] extends TAllowedTypes ? T[Key] extends PrimitivesAndNativeObjects ? `${Key}` : `${Key}` | `${Key}.${DotPaths<T[Key], TAllowedTypes>}` : `${Key}.${DotPaths<T[Key], TAllowedTypes>}`;
11
24
  }[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>;
25
+ /** Dot-paths of a tuple. */
26
+ type TuplePaths<T, TAllowedTypes> = ObjectPaths<Omit<T, keyof any[]>, TAllowedTypes>;
27
+ /** Literal union of all posible dot-paths which satisfy provided types as second argument (default: any). */
28
+ type DotPaths<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>;
15
29
  /**
16
- * Type to know when a path is indexing an array
30
+ * --------------------------------------------
31
+ * VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
32
+ * --------------------------------------------
17
33
  */
18
- type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` | `${string}.${T}` | `${string}.${T}.${string}`;
34
+ /** Nested type implementation. */
35
+ 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;
36
+ /** Value of a nested property of a given dot-path. */
37
+ type ValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = NestedType<TObject, TPath>;
19
38
  /**
20
- * Nested Property Type implementation
39
+ * --------------------------------------------
40
+ * RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
41
+ * --------------------------------------------
21
42
  */
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>);
43
+ /** Returned nested type implementation. */
44
+ 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;
45
+ /** Value of a RETURNED nested property of a given dot-path (so when accessing arrays can be undefined). */
46
+ type ReturnedValueInDotPath<TObject, TPath extends DotPaths<TObject> | ''> = ReturnedNestedValue<TObject, TPath>;
25
47
  /**
26
- * Update Object Type Implementation
48
+ * --------------------------------------------
49
+ * UPDATE OBJECT WITH DOT PATH IMPLEMENTATION
50
+ * --------------------------------------------
27
51
  */
52
+ /** Value of a dot-path update object, can be the value or a function. */
53
+ type DotPathUpdateValue<TObject, TPath extends DotPaths<TObject>> = ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
54
+ /** Dot-path update object, used to give updates to the updateImmutably function. */
28
55
  type DotPathUpdateObject<TObject> = {
29
- [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
56
+ [TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
30
57
  };
31
58
  /**
32
- * Optional and Required keys
59
+ * --------------------------------------------
60
+ * REQUIRED OR OPTIONAL KEYS
61
+ * --------------------------------------------
33
62
  */
34
- type NonPartialKeys = string | number | boolean | symbol | bigint | null | object;
35
- type OptionalKeys<T> = {
63
+ /** All types instead of undefined. */
64
+ type NonPartialKeys = null | object;
65
+ /** Returns only the optional keys of an object. */
66
+ type RetrieveOptionalKeys<T> = {
36
67
  [K in keyof T & string]: T[K] extends NonPartialKeys ? never : K;
37
68
  }[keyof T & string];
38
- type RequiredKeys<T> = {
69
+ /** Returns only the required keys of an object. */
70
+ type RetrieveRequiredKeys<T> = {
39
71
  [K in keyof T & string]: T[K] extends NonPartialKeys ? K : never;
40
72
  }[keyof T & string];
41
73
 
42
- type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (OptionalKeys<T> | (OptionalKeys<T>)[])) => T;
74
+ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${number}` | (number | `${number}`)[]) : (RetrieveOptionalKeys<T> | (RetrieveOptionalKeys<T>)[])) => T;
43
75
  /**
44
76
  * Removes properties or indexes, order doesn't matter.
77
+ * @param value Object/array from where to remove keys/indexes shallowly.
78
+ * @param keys Keys to remove.
79
+ * @returns New object/array without the provided keys/indexes.
45
80
  */
46
81
  declare const remove: Remove;
47
82
 
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>;
83
+ type Select = <TObject, TPath extends DotPaths<TObject> | ''>(state: TObject, path: TPath) => ReturnedValueInDotPath<TObject, TPath>;
49
84
  /**
50
- * Returns the value from a given dot path of an object/array.
51
- * Returns undefined if the provided path doesnt exist.
85
+ * Returns the value from a given dot path of an object/array or undefined if doesn't exist.
86
+ * @param state State from which retrieve the value.
87
+ * @param path Path indicating the value to extract.
88
+ * @returns Value of the provided path or undefined.
52
89
  */
53
90
  declare const select: Select;
54
91
 
@@ -56,22 +93,28 @@ type PathWithoutLastKey = string;
56
93
  type LastKey = string;
57
94
  type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
58
95
  /**
59
- * Splits string dotPath into ID and RemainingPath
60
- */
96
+ * Splits a dot path string into its last key and its remaining keys ('' if none).
97
+ * @param dotPath A dot path.
98
+ * @returns [pathWithoutLastKey, lastKey] or ['', lastKey].
99
+ */
61
100
  declare const splitPathAtLastKey: SplitDotPathAtLastKey;
62
101
 
63
102
  type Set = <T>(newValue: T) => (value: T) => T;
64
103
  /**
65
- * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays
104
+ * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
105
+ * @param newValue New value.
106
+ * @returns The same value.
66
107
  */
67
108
  declare const set: Set;
68
109
 
69
110
  type UpdateImmutably = <T extends object>(state: T, updates: DotPathUpdateObject<T>, addNonExistentPropsAndIndexes?: true) => T;
70
111
  /**
71
- * Creates a new updated object/array applying the changes provided in the update object.
72
- * It doesnt modify the original input.
73
- * If any of the paths doesnt exists it returns
112
+ * Creates a new object applying immutable updates using dot-path syntax.
113
+ * @param state Original State Object.
114
+ * @param updates Dot Path Update Object.
115
+ * @param addNonExistentPropsAndIndexes If it should or not create non-existent fields.
116
+ * @returns New Updated Object.
74
117
  */
75
118
  declare const updateImmutably: UpdateImmutably;
76
119
 
77
- export { type DotPathUpdateObject, type DotPathUpdateValue, type DotPaths, type OptionalKeys, type RequiredKeys, type ValueInDotPath, remove, select, set, splitPathAtLastKey, updateImmutably };
120
+ 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.1",
4
4
  "description": "A lightweight 500 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",