maraj 2.7.1 → 2.7.2

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
@@ -19,8 +19,9 @@ type OptionValues = 'error' | 'skip' | 'add';
19
19
  type Options = {
20
20
  onNewProps?: OptionValues;
21
21
  onNewIndexes?: OptionValues;
22
+ onPathsIntersectedByPrimitiveValues?: OptionValues;
22
23
  };
23
- type ImmutableUpdate = <TObject extends Record<PropertyKey, any>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
+ type ImmutableUpdate = <TObject>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
25
  /**
25
26
  * Creates a new object applying immutable updates using dot-path syntax.
26
27
  * Can add or skip new props or indexes depending on options.
@@ -31,7 +32,7 @@ type ImmutableUpdate = <TObject extends Record<PropertyKey, any>>(state: TObject
31
32
  * @returns New Updated Object.
32
33
  */
33
34
  declare const update: ImmutableUpdate;
34
- type ExtendableImmutableUpdate = <TObject extends Record<PropertyKey, any>, TUpdateObject extends UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
35
+ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
35
36
  /**
36
37
  * Creates a new object applying immutable updates using dot-path syntax.
37
38
  * It will always add non-existent props or indexes if provided.
@@ -118,7 +119,7 @@ type NestedPathKeys<T, K extends string | number> = {
118
119
  /** Extract value if its a function */
119
120
  type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
120
121
  /** To check if an empty object is provided */
121
- type EmptyObject = Record<string | number | symbol, never>;
122
+ type EmptyObject = Record<PropertyKey, never>;
122
123
  /** Used to extract the type from a dot-path update object. */
123
124
  type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T extends EmptyObject ? {} : keyof T extends LiteralIndex | number | `${number}` | `${number}.${infer U}` ? Value<T[KeyPathsStartingWithAnIndex<T>]> extends never ? Array<Value<T[KeysRepresentingAnIndex<T>]>> : Array<Value<T[KeysRepresentingAnIndex<T>]> | {
124
125
  [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
@@ -132,7 +133,7 @@ type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T exten
132
133
  }>;
133
134
  } & unknown;
134
135
  /** New expanded value after aplying updates including new properties */
135
- type ExtendedUpdate<TObject extends Record<string | number | symbol, any>, TDotPathUpdateObject extends Record<string | number | symbol, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
136
+ type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
136
137
 
137
138
  type Select = {
138
139
  <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
@@ -156,7 +157,7 @@ type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastK
156
157
  */
157
158
  declare const splitPathAtLastKey: SplitDotPathAtLastKey;
158
159
 
159
- type Set = <T>(newValue: T) => (value: T) => T;
160
+ type Set = <T>(newValue: T) => (v: unknown) => T;
160
161
  /**
161
162
  * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
162
163
  * @param newValue New value.
package/dist/index.d.ts CHANGED
@@ -19,8 +19,9 @@ type OptionValues = 'error' | 'skip' | 'add';
19
19
  type Options = {
20
20
  onNewProps?: OptionValues;
21
21
  onNewIndexes?: OptionValues;
22
+ onPathsIntersectedByPrimitiveValues?: OptionValues;
22
23
  };
23
- type ImmutableUpdate = <TObject extends Record<PropertyKey, any>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
+ type ImmutableUpdate = <TObject>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
25
  /**
25
26
  * Creates a new object applying immutable updates using dot-path syntax.
26
27
  * Can add or skip new props or indexes depending on options.
@@ -31,7 +32,7 @@ type ImmutableUpdate = <TObject extends Record<PropertyKey, any>>(state: TObject
31
32
  * @returns New Updated Object.
32
33
  */
33
34
  declare const update: ImmutableUpdate;
34
- type ExtendableImmutableUpdate = <TObject extends Record<PropertyKey, any>, TUpdateObject extends UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
35
+ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TObject>>(state: TObject, dotPathUpdateObject: TUpdateObject) => ExtendedUpdate<TObject, TUpdateObject>;
35
36
  /**
36
37
  * Creates a new object applying immutable updates using dot-path syntax.
37
38
  * It will always add non-existent props or indexes if provided.
@@ -118,7 +119,7 @@ type NestedPathKeys<T, K extends string | number> = {
118
119
  /** Extract value if its a function */
119
120
  type Value<T> = T extends (...args: any) => any ? ReturnType<T> : T;
120
121
  /** To check if an empty object is provided */
121
- type EmptyObject = Record<string | number | symbol, never>;
122
+ type EmptyObject = Record<PropertyKey, never>;
122
123
  /** Used to extract the type from a dot-path update object. */
123
124
  type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T extends EmptyObject ? {} : keyof T extends LiteralIndex | number | `${number}` | `${number}.${infer U}` ? Value<T[KeyPathsStartingWithAnIndex<T>]> extends never ? Array<Value<T[KeysRepresentingAnIndex<T>]>> : Array<Value<T[KeysRepresentingAnIndex<T>]> | {
124
125
  [Key2 in U]: Value<T[KeyPathsStartingWithAnIndex<T>]>;
@@ -132,7 +133,7 @@ type ExtractTypeFromDotPathUpdateObject<T extends Record<string, any>> = T exten
132
133
  }>;
133
134
  } & unknown;
134
135
  /** New expanded value after aplying updates including new properties */
135
- type ExtendedUpdate<TObject extends Record<string | number | symbol, any>, TDotPathUpdateObject extends Record<string | number | symbol, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
136
+ type ExtendedUpdate<TObject, TDotPathUpdateObject extends Record<PropertyKey, any>> = TObject extends Array<infer U> ? ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> extends Array<infer W> ? Array<U | W> : TObject & ExtractTypeFromDotPathUpdateObject<TDotPathUpdateObject> : ExtractTypeFromDotPathUpdateObject<TObject & TDotPathUpdateObject>;
136
137
 
137
138
  type Select = {
138
139
  <T, TPath extends DotPaths<T>>(state: T, path: TPath): ReturnedValueInPath<T, TPath>;
@@ -156,7 +157,7 @@ type SplitDotPathAtLastKey = <T extends string>(dotPath: T) => [PathWithoutLastK
156
157
  */
157
158
  declare const splitPathAtLastKey: SplitDotPathAtLastKey;
158
159
 
159
- type Set = <T>(newValue: T) => (value: T) => T;
160
+ type Set = <T>(newValue: T) => (v: unknown) => T;
160
161
  /**
161
162
  * Allows to set strongly typed values (removing inacurate "undefined" inference) in nested values inside arrays.
162
163
  * @param newValue New value.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var m=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},C=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of $(t))!k.call(e,o)&&o!==r&&m(e,o,{get:()=>t[o],enumerable:!(n=R(t,o))||n.enumerable});return e};var N=e=>C(m({},"__esModule",{value:!0}),e);var W={};v(W,{extendableUpdate:()=>L,isObjectLiteral:()=>d,isStringIndex:()=>y,parseStringIndex:()=>U,remove:()=>I,select:()=>j,set:()=>K,splitPathAtLastKey:()=>w,update:()=>E});module.exports=N(W);var I=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let o=Symbol(),p=[...e];return r.forEach(s=>{p[s]=o}),p.filter(s=>s!==o)}let n={...e};return r.forEach(o=>delete n[o]),n};var j=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let o=r.join(".");if(n?.hasOwnProperty(o))return n[o];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var w=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",n=e.slice(t);return[r,n]};var K=e=>()=>e;var d=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var U=e=>{let t=parseInt(e);return t.toString()===e?t:null},y=e=>parseInt(e).toString()===e;var A=e=>JSON.stringify(e),h=e=>Array.isArray(e),g=(e,t,r={})=>{if(!d(e)&&!h(e))throw new Error(`"${A(e)}" is not an literalObject/array`);if(!d(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},o={};for(let[p,x]of Object.entries(t)){if(p==="")continue;let s=p.split("."),l=o,i=n;for(;s.length>1;){let b=s.join(".");if(Object.hasOwn(i,b)){s=[b];break}let a=s[0],f=i[a],S=d(f),T=h(f),O=f===void 0;if(!S&&!T&&!O)throw new Error(`Property "${a}" in "${p}" is a primitive`);if(!l[a]){if(O){let V=h(i)&&y(a),D=y(s[1]),P=V?r.onNewIndexes:r.onNewProps;if(P==="error")throw new Error(`${a} doesn't exist in "${p}"`);if(P==="skip")continue;i[a]=D?[]:{}}else i[a]=T?[...f]:{...f};l[a]={}}i=i[a],l=l[a],s.shift()}let c=s[0],u=i;if(!Object.hasOwn(u,c)){let a=h(u)&&y(c)?r.onNewIndexes:r.onNewProps;if(a==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${p}". Its value is: "${A(u)}"`);if(a==="skip")continue}u[c]=typeof x=="function"?x(u[c]):x,delete l[c]}return n},E=(e,t,r)=>g(e,t,r),L=(e,t)=>g(e,t);0&&(module.exports={extendableUpdate,isObjectLiteral,isStringIndex,parseStringIndex,remove,select,set,splitPathAtLastKey,update});
1
+ "use strict";var T=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var $=(e,t)=>{for(var r in t)T(e,r,{get:t[r],enumerable:!0})},C=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of v(t))!D.call(e,o)&&o!==r&&T(e,o,{get:()=>t[o],enumerable:!(n=k(t,o))||n.enumerable});return e};var N=e=>C(T({},"__esModule",{value:!0}),e);var R={};$(R,{extendableUpdate:()=>L,isObjectLiteral:()=>d,isStringIndex:()=>y,parseStringIndex:()=>U,remove:()=>I,select:()=>j,set:()=>K,splitPathAtLastKey:()=>w,update:()=>E});module.exports=N(R);var I=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let o=Symbol(),i=[...e];return r.forEach(s=>{i[s]=o}),i.filter(s=>s!==o)}let n={...e};return r.forEach(o=>delete n[o]),n};var j=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let o=r.join(".");if(n?.hasOwnProperty(o))return n[o];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var w=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",n=e.slice(t);return[r,n]};var K=e=>()=>e;var d=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var U=e=>{let t=parseInt(e);return t.toString()===e?t:null},y=e=>parseInt(e).toString()===e;var A=e=>JSON.stringify(e),m=e=>Array.isArray(e),g=(e,t,r={})=>{if(!d(e)&&!m(e))throw new Error(`"${A(e)}" is not an literalObject/array`);if(!d(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},o={};for(let[i,h]of Object.entries(t)){if(i==="")continue;let s=i.split("."),l=o,p=n;for(;s.length>1;){let x=s.join(".");if(Object.hasOwn(p,x)){s=[x];break}let a=s[0],f=p[a],O=d(f),b=m(f);if(!O&&!b&&!(f===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${a}" in "${i}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!l[a]){if(!O&&!b){let V=m(p)&&y(a),S=y(s[1]),P=V?r.onNewIndexes:r.onNewProps;if(P==="error")throw new Error(`${a} doesn't exist in "${i}"`);if(P==="skip")continue;p[a]=S?[]:{}}else p[a]=b?[...f]:{...f};l[a]={}}p=p[a],l=l[a],s.shift()}let c=s[0],u=p;if(!Object.hasOwn(u,c)){let a=m(u)&&y(c)?r.onNewIndexes:r.onNewProps;if(a==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${i}". Its value is: "${A(u)}"`);if(a==="skip")continue}u[c]=typeof h=="function"?h(u[c]):h,delete l[c]}return n},E=(e,t,r)=>g(e,t,r),L=(e,t)=>g(e,t);0&&(module.exports={extendableUpdate,isObjectLiteral,isStringIndex,parseStringIndex,remove,select,set,splitPathAtLastKey,update});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var U=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let s=Symbol(),p=[...e];return r.forEach(a=>{p[a]=s}),p.filter(a=>a!==s)}let n={...e};return r.forEach(s=>delete n[s]),n};var A=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let s=r.join(".");if(n?.hasOwnProperty(s))return n[s];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var g=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",n=e.slice(t);return[r,n]};var E=e=>()=>e;var u=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var L=e=>{let t=parseInt(e);return t.toString()===e?t:null},f=e=>parseInt(e).toString()===e;var P=e=>JSON.stringify(e),h=e=>Array.isArray(e),I=(e,t,r={})=>{if(!u(e)&&!h(e))throw new Error(`"${P(e)}" is not an literalObject/array`);if(!u(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},s={};for(let[p,x]of Object.entries(t)){if(p==="")continue;let a=p.split("."),d=s,i=n;for(;a.length>1;){let b=a.join(".");if(Object.hasOwn(i,b)){a=[b];break}let o=a[0],l=i[o],j=u(l),m=h(l),T=l===void 0;if(!j&&!m&&!T)throw new Error(`Property "${o}" in "${p}" is a primitive`);if(!d[o]){if(T){let w=h(i)&&f(o),K=f(a[1]),O=w?r.onNewIndexes:r.onNewProps;if(O==="error")throw new Error(`${o} doesn't exist in "${p}"`);if(O==="skip")continue;i[o]=K?[]:{}}else i[o]=m?[...l]:{...l};d[o]={}}i=i[o],d=d[o],a.shift()}let c=a[0],y=i;if(!Object.hasOwn(y,c)){let o=h(y)&&f(c)?r.onNewIndexes:r.onNewProps;if(o==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${p}". Its value is: "${P(y)}"`);if(o==="skip")continue}y[c]=typeof x=="function"?x(y[c]):x,delete d[c]}return n},S=(e,t,r)=>I(e,t,r),V=(e,t)=>I(e,t);export{V as extendableUpdate,u as isObjectLiteral,f as isStringIndex,L as parseStringIndex,U as remove,A as select,E as set,g as splitPathAtLastKey,S as update};
1
+ var K=(e,t)=>{if(!Array.isArray(e)&&e?.constructor!==Object)throw'Wrong type provided in "remove" function';let r=Array.isArray(t)?t:[t];if(Array.isArray(e)){let s=Symbol(),i=[...e];return r.forEach(a=>{i[a]=s}),i.filter(a=>a!==s)}let n={...e};return r.forEach(s=>delete n[s]),n};var U=(e,t)=>{if(typeof t!="string")throw new Error("path is not a string");if(!t)return e;let r=t.split("."),n=e;for(;r.length;){let s=r.join(".");if(n?.hasOwnProperty(s))return n[s];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var A=e=>{if(typeof e!="string")throw new Error(`Wrong dotPath type: ${e}`);let t=e.lastIndexOf(".")+1,r=t?e.slice(0,t-1):"",n=e.slice(t);return[r,n]};var g=e=>()=>e;var u=e=>!!e&&Object.getPrototypeOf(e)===Object.prototype;var E=e=>{let t=parseInt(e);return t.toString()===e?t:null},f=e=>parseInt(e).toString()===e;var P=e=>JSON.stringify(e),m=e=>Array.isArray(e),I=(e,t,r={})=>{if(!u(e)&&!m(e))throw new Error(`"${P(e)}" is not an literalObject/array`);if(!u(t))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(e)?[...e]:{...e},s={};for(let[i,h]of Object.entries(t)){if(i==="")continue;let a=i.split("."),d=s,p=n;for(;a.length>1;){let x=a.join(".");if(Object.hasOwn(p,x)){a=[x];break}let o=a[0],l=p[o],T=u(l),b=m(l);if(!T&&!b&&!(l===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${o}" in "${i}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!d[o]){if(!T&&!b){let j=m(p)&&f(o),w=f(a[1]),O=j?r.onNewIndexes:r.onNewProps;if(O==="error")throw new Error(`${o} doesn't exist in "${i}"`);if(O==="skip")continue;p[o]=w?[]:{}}else p[o]=b?[...l]:{...l};d[o]={}}p=p[o],d=d[o],a.shift()}let c=a[0],y=p;if(!Object.hasOwn(y,c)){let o=m(y)&&f(c)?r.onNewIndexes:r.onNewProps;if(o==="error")throw new Error(`Property/Index "${c}" doesnt exist in the path "${i}". Its value is: "${P(y)}"`);if(o==="skip")continue}y[c]=typeof h=="function"?h(y[c]):h,delete d[c]}return n},L=(e,t,r)=>I(e,t,r),V=(e,t)=>I(e,t);export{V as extendableUpdate,u as isObjectLiteral,f as isStringIndex,E as parseStringIndex,K as remove,U as select,g as set,A as splitPathAtLastKey,L as update};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
4
4
  "description": "A lightweight 600 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",