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 +45 -24
- package/dist/index.d.ts +45 -24
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,51 +1,72 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
*
|
|
28
|
+
* --------------------------------------------
|
|
29
|
+
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
30
|
+
* --------------------------------------------
|
|
17
31
|
*/
|
|
18
|
-
type
|
|
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
|
-
*
|
|
35
|
+
* --------------------------------------------
|
|
36
|
+
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
37
|
+
* --------------------------------------------
|
|
21
38
|
*/
|
|
22
|
-
type
|
|
23
|
-
type
|
|
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
|
-
*
|
|
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
|
-
[
|
|
48
|
+
[TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
30
49
|
};
|
|
31
50
|
/**
|
|
32
|
-
*
|
|
51
|
+
* --------------------------------------------
|
|
52
|
+
* REQUIRED OR OPTIONAL KEYS
|
|
53
|
+
* --------------------------------------------
|
|
33
54
|
*/
|
|
34
|
-
type NonPartialKeys =
|
|
35
|
-
type
|
|
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
|
|
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}`)[]) : (
|
|
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 = <
|
|
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
|
|
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
|
-
*
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
*
|
|
28
|
+
* --------------------------------------------
|
|
29
|
+
* VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
30
|
+
* --------------------------------------------
|
|
17
31
|
*/
|
|
18
|
-
type
|
|
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
|
-
*
|
|
35
|
+
* --------------------------------------------
|
|
36
|
+
* RETURNED VALUE OF A NESTED PROPERTY PROVIDING A DOT PATH
|
|
37
|
+
* --------------------------------------------
|
|
21
38
|
*/
|
|
22
|
-
type
|
|
23
|
-
type
|
|
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
|
-
*
|
|
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
|
-
[
|
|
48
|
+
[TPath in DotPaths<TObject>]?: ValueInDotPath<TObject, TPath> | ((fieldValue: ValueInDotPath<TObject, TPath>, fullObject: TObject) => ValueInDotPath<TObject, TPath>);
|
|
30
49
|
};
|
|
31
50
|
/**
|
|
32
|
-
*
|
|
51
|
+
* --------------------------------------------
|
|
52
|
+
* REQUIRED OR OPTIONAL KEYS
|
|
53
|
+
* --------------------------------------------
|
|
33
54
|
*/
|
|
34
|
-
type NonPartialKeys =
|
|
35
|
-
type
|
|
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
|
|
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}`)[]) : (
|
|
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 = <
|
|
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
|
|
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
|
|
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
|
|
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};
|