maraj 2.1.0 → 2.2.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
@@ -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 extends object> = Flatten<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 extends string> = 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 extends object, TPath extends DotPaths<TObject> | ''> = NestedProperty<TObject, TPath>;
24
- type DotPathUpdateValue<TObject extends object, 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>);
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 extends object> = {
28
+ type DotPathUpdateObject<TObject> = {
29
29
  [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
30
30
  };
31
31
  /**
@@ -52,7 +52,9 @@ type Select = <T extends object, TPath extends (DotPaths<T> | '')>(state: T, pat
52
52
  */
53
53
  declare const select: Select;
54
54
 
55
- type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [string, string];
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 extends object> = Flatten<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 extends string> = 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 extends object, TPath extends DotPaths<TObject> | ''> = NestedProperty<TObject, TPath>;
24
- type DotPathUpdateValue<TObject extends object, 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>);
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 extends object> = {
28
+ type DotPathUpdateObject<TObject> = {
29
29
  [TKey in DotPaths<TObject>]?: DotPathUpdateValue<TObject, TKey>;
30
30
  };
31
31
  /**
@@ -52,7 +52,9 @@ type Select = <T extends object, TPath extends (DotPaths<T> | '')>(state: T, pat
52
52
  */
53
53
  declare const select: Select;
54
54
 
55
- type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [string, string];
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 K=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},O=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of D(e))!w.call(t,a)&&a!==r&&f(t,a,{get:()=>e[a],enumerable:!(o=K(e,a))||o.enumerable});return t};var g=t=>O(f({},"__esModule",{value:!0}),t);var v={};j(v,{remove:()=>P,select:()=>T,set:()=>b,splitPathAtLastKey:()=>m,updateImmutably:()=>A});module.exports=g(v);var P=(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 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):"",o=t.slice(e);return[r,o]};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 o=Array.isArray(t)?[...t]:{...t},a={};for(let[y,h]of Object.entries(e)){if(!y)continue;let n=y.split("."),s=n.pop(),l=a,d=o;for(let u=0;u<n.length;u++){let i=n[u],p=d[i],I=typeof p=="object"&&(p.constructor===Object||Array.isArray(p)),x=r&&p===void 0;if(!I&&!x)throw new Error(`Invalid path provided: "${i}" is not an object/array in "${y}"`);l[i]||(x?d[i]=isNaN(n[u+1]??s)?{}:[]:d[i]=Array.isArray(p)?[...p]:{...p},l[i]={}),d=d[i],l=l[i]}let c=d;if(!r){if(Array.isArray(c)&&!(s<c.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(!c.hasOwnProperty(s))throw new Error(`Property ${s} doesnt exist in the path ${n.join(".")} because its value is ${c}`)}c[s]=typeof h=="function"?h(c[s],o):h,delete l[s]}return o};0&&(module.exports={remove,select,set,splitPathAtLastKey,updateImmutably});
1
+ "use strict";var f=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var j=(t,e)=>{for(var r in e)f(t,r,{get:e[r],enumerable:!0})},O=(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=>O(f({},"__esModule",{value:!0}),t);var v={};j(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,l]of Object.entries(e)){if(!y)continue;let n=y.split("."),s=n.pop(),c=o,d=a;for(let u=0;u<n.length;u++){let i=n[u],p=d[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}"`);c[i]||(P?d[i]=isNaN(n[u+1]??s)?{}:[]:d[i]=Array.isArray(p)?[...p]:{...p},c[i]={}),d=d[i],c=c[i]}let h=d;if(!r){if(Array.isArray(h)&&!(s<h.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(!h.hasOwnProperty(s))throw new Error(`Property ${s} doesnt exist in the path ${n.join(".")} because its value is ${h}`)}h[s]=typeof l=="function"?l(h[s],a):l,delete c[s]}return a};0&&(module.exports={remove,select,set,splitPathAtLastKey,updateImmutably});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var P=(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 m=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 b=t=>()=>t;var A=(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,h]of Object.entries(e)){if(!y)continue;let a=y.split("."),n=a.pop(),l=p,d=r;for(let u=0;u<a.length;u++){let s=a[u],i=d[s],x=typeof i=="object"&&(i.constructor===Object||Array.isArray(i)),f=o&&i===void 0;if(!x&&!f)throw new Error(`Invalid path provided: "${s}" is not an object/array in "${y}"`);l[s]||(f?d[s]=isNaN(a[u+1]??n)?{}:[]:d[s]=Array.isArray(i)?[...i]:{...i},l[s]={}),d=d[s],l=l[s]}let c=d;if(!o){if(Array.isArray(c)&&!(n<c.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(!c.hasOwnProperty(n))throw new Error(`Property ${n} doesnt exist in the path ${a.join(".")} because its value is ${c}`)}c[n]=typeof h=="function"?h(c[n],r):h,delete l[n]}return r};export{P as remove,T as select,b as set,m as splitPathAtLastKey,A as updateImmutably};
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,l]of Object.entries(e)){if(!y)continue;let o=y.split("."),n=o.pop(),c=p,d=r;for(let u=0;u<o.length;u++){let s=o[u],i=d[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}"`);c[s]||(f?d[s]=isNaN(o[u+1]??n)?{}:[]:d[s]=Array.isArray(i)?[...i]:{...i},c[s]={}),d=d[s],c=c[s]}let h=d;if(!a){if(Array.isArray(h)&&!(n<h.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(!h.hasOwnProperty(n))throw new Error(`Property ${n} doesnt exist in the path ${o.join(".")} because its value is ${h}`)}h[n]=typeof l=="function"?l(h[n],r):l,delete c[n]}return r};export{x as remove,T as select,b as set,m as splitPathAtLastKey,A as updateImmutably};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.1.0",
3
+ "version": "2.2.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",