maraj 2.1.0 → 2.3.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 +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -11,7 +11,7 @@ type ObjectPaths<T> = {
|
|
|
11
11
|
}[keyof T & string];
|
|
12
12
|
type TuplePaths<T> = ObjectPaths<Omit<T, keyof any[]>>;
|
|
13
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
|
|
14
|
+
type DotPaths<T> = Flatten<T>;
|
|
15
15
|
/**
|
|
16
16
|
* Type to know when a path is indexing an array
|
|
17
17
|
*/
|
|
@@ -19,13 +19,13 @@ type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` |
|
|
|
19
19
|
/**
|
|
20
20
|
* Nested Property Type implementation
|
|
21
21
|
*/
|
|
22
|
-
type NestedProperty<TObject, TPath
|
|
23
|
-
type ValueInDotPath<TObject
|
|
24
|
-
type DotPathUpdateValue<TObject
|
|
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>);
|
|
25
25
|
/**
|
|
26
26
|
* Update Object Type Implementation
|
|
27
27
|
*/
|
|
28
|
-
type DotPathUpdateObject<TObject
|
|
28
|
+
type DotPathUpdateObject<TObject> = {
|
|
29
29
|
[TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
|
|
30
30
|
};
|
|
31
31
|
/**
|
|
@@ -45,14 +45,16 @@ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${
|
|
|
45
45
|
*/
|
|
46
46
|
declare const remove: Remove;
|
|
47
47
|
|
|
48
|
-
type Select = <T
|
|
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>;
|
|
49
49
|
/**
|
|
50
50
|
* Returns the value from a given dot path of an object/array.
|
|
51
51
|
* Returns undefined if the provided path doesnt exist.
|
|
52
52
|
*/
|
|
53
53
|
declare const select: Select;
|
|
54
54
|
|
|
55
|
-
type
|
|
55
|
+
type PathWithoutLastKey = string;
|
|
56
|
+
type LastKey = string;
|
|
57
|
+
type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
|
|
56
58
|
/**
|
|
57
59
|
* Splits string dotPath into ID and RemainingPath
|
|
58
60
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ type ObjectPaths<T> = {
|
|
|
11
11
|
}[keyof T & string];
|
|
12
12
|
type TuplePaths<T> = ObjectPaths<Omit<T, keyof any[]>>;
|
|
13
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
|
|
14
|
+
type DotPaths<T> = Flatten<T>;
|
|
15
15
|
/**
|
|
16
16
|
* Type to know when a path is indexing an array
|
|
17
17
|
*/
|
|
@@ -19,13 +19,13 @@ type PathIndexingArray<T extends string | number> = `${T}` | `${T}.${string}` |
|
|
|
19
19
|
/**
|
|
20
20
|
* Nested Property Type implementation
|
|
21
21
|
*/
|
|
22
|
-
type NestedProperty<TObject, TPath
|
|
23
|
-
type ValueInDotPath<TObject
|
|
24
|
-
type DotPathUpdateValue<TObject
|
|
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>);
|
|
25
25
|
/**
|
|
26
26
|
* Update Object Type Implementation
|
|
27
27
|
*/
|
|
28
|
-
type DotPathUpdateObject<TObject
|
|
28
|
+
type DotPathUpdateObject<TObject> = {
|
|
29
29
|
[TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
|
|
30
30
|
};
|
|
31
31
|
/**
|
|
@@ -45,14 +45,16 @@ type Remove = <T extends object>(value: T, keys: T extends any[] ? (number | `${
|
|
|
45
45
|
*/
|
|
46
46
|
declare const remove: Remove;
|
|
47
47
|
|
|
48
|
-
type Select = <T
|
|
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>;
|
|
49
49
|
/**
|
|
50
50
|
* Returns the value from a given dot path of an object/array.
|
|
51
51
|
* Returns undefined if the provided path doesnt exist.
|
|
52
52
|
*/
|
|
53
53
|
declare const select: Select;
|
|
54
54
|
|
|
55
|
-
type
|
|
55
|
+
type PathWithoutLastKey = string;
|
|
56
|
+
type LastKey = string;
|
|
57
|
+
type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastKey, LastKey];
|
|
56
58
|
/**
|
|
57
59
|
* Splits string dotPath into ID and RemainingPath
|
|
58
60
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var
|
|
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});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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};
|