maraj 2.7.3 → 2.8.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/README.md CHANGED
@@ -41,6 +41,8 @@ originalValue.files[1] === newValue.files[1] // true
41
41
  - { a: { b: {"c.d": ... } } }
42
42
  - { a: { b: {"c": { d: ... } } } }
43
43
 
44
+ It can still work with dot-paths keys containing an object or array, however the paths targeting nested properties in this key are forbiden. For example having the following object { a: { "b.c": { d: ... } } } you can use the path "a" and "a.b.c", but you cant use "a.b.c.d", this path will create a new property "b" inside "a", then "c" and lastly "d" resulting in the following object { a: { "b.c": { d: ... } }, b: { c: { d: ... } } }
45
+
44
46
  - In "extendableUpdate", **numbers** inside path will **always** create a mutable **array** if the array/object doesnt exist beforehand. If you want an object/tuple/readonlyArray instead of an array, add it beforehand as type.
45
47
 
46
48
  ```ts
@@ -57,22 +59,27 @@ const updatedValue2 = extendableUpdate({}, {
57
59
  // updatedValue2 = { files: { '1': { name: 'new name' } } }
58
60
  ```
59
61
 
60
- - The type "UpdateDotPathObject" (the type of the object provided containing the updates, the one used as second parameter in the update function) if used to create new update objects using computed properties { [key]: value } (middlewares for example) will always throw a ts error even if the typing is correct. [Playground simple explanation](https://www.typescriptlang.org/play?ts=5.3.2#code/LAKALgngDgpgBAGQJYGcxwLxwOQENtwA+cABgCQDeAdgK4C2ARjAE4C+JooksiqYASjADGAe2YATTHApwA2gGk4SKrzQBdAPwAuOItacQoqmjhDmMXGBiDREqQB4AKnBgAPK1XEpVYAHwAKAGsYCB1HAEpMX2lQODijE3MhHWQ0GzFJLApYuNyAejy4AD0wbxZmMR0UZSF4ADcAFgA6AGYmgFYldAB3JAAbPrgwAAsK7rhcFXKxCfQhYeFA5QBzUxE6KBorSWCIb0nJOtw+mhhvGmqqVasNvst4PqQrZmOyupgVJAAzIYWh6BWSm8tiSYByuTkuzUOl24P0IFy5jANGYKiSoFYQA)
62
+ - The type "UpdateObject" (the type of the object provided containing the updates, used as second parameter in the update function) if used to create new update objects using computed properties in a inline syntax { [key]: value } will always throw a ts error even if the typing is correct, use the second syntax.
61
63
 
62
64
  ```ts
63
- const updateFieldStore = <TPath extends DotPaths<Store>>(
65
+ const updateFieldStore = <T, TPath extends DotPaths<T>>(
66
+ state: T,
64
67
  path: TPath,
65
- newValue: UpdateDotPathValue<Store, TPath>
68
+ newValue: UpdateValue<T, TPath>
66
69
  ) => {
67
- const update: UpdateDotPathObject<Store> = {
68
- // ^throws ts error even if the typing is correct
70
+ // ts error even if the typing is correct (typescript issue)
71
+ const updateObject1: UpdateObject<T> = {
69
72
  [path]: newValue
70
- } // fix: coherce it with " as UpdateDotPathObject<Store>"
71
- return update
73
+ }
74
+
75
+ // All good, no ts error
76
+ const updateObject2: UpdateObject<T> = {}
77
+ updateObject[path] = newValue
78
+
72
79
  }
73
80
  ```
74
81
 
75
- - Use the "set" utility if typescript doesn't infer properly _"an optional field"_ vs a _"field which can have 'undefined' as type"_. Usually happens in optional nested props inside arrays.
82
+ - Use the "set" utility if typescript doesn't infer properly *"an optional field"* vs a *"field which can have 'undefined' as type"*. Usually happens in nested props inside arrays.
76
83
 
77
84
  ```ts
78
85
  const updatedValue = update(data, {
package/dist/index.d.mts CHANGED
@@ -21,7 +21,7 @@ type Options = {
21
21
  onNewIndexes?: OptionValues;
22
22
  onPathsIntersectedByPrimitiveValues?: OptionValues;
23
23
  };
24
- type ImmutableUpdate = <TObject>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
+ type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends PropertyKey = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
25
25
  /**
26
26
  * Creates a new object applying immutable updates using dot-path syntax.
27
27
  * Can add or skip new props or indexes depending on options.
@@ -41,7 +41,7 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
41
41
  * @param dotPathUpdateObject Dot Path Update Object.
42
42
  * @returns New Updated Object.
43
43
  */
44
- declare const extendableUpdate: ExtendableImmutableUpdate;
44
+ declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
45
45
 
46
46
  /**
47
47
  * --------------------------------------------
@@ -177,4 +177,13 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
177
177
  declare const parseStringIndex: (v: string) => number | null;
178
178
  declare const isStringIndex: (v: string) => boolean;
179
179
 
180
- export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type UpdateObject, type UpdateValue, type ValueInPath, extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, update };
180
+ type StrictCallbackError = "CALLBACK STRICTNESS RETURN ERROR: return the value using the first parameter";
181
+ type StrictCallbackValue<T> = [StrictCallbackError, T];
182
+ type StrictCallbackFunction<T> = (v: T) => StrictCallbackValue<T>;
183
+ /** Apply strictness to a callback function */
184
+ type StrictCallback<T extends (...args: any[]) => any> = (r: StrictCallbackFunction<ReturnType<T>>, ...args: Parameters<T>) => [StrictCallbackError, ReturnType<T>];
185
+ type UnsealStrictCallback = <TCallback extends (r: StrictCallbackFunction<any>, ...args: any[]) => any>(callback: TCallback) => (...args: Parameters<TCallback> extends [infer _, ...infer RestParams] ? RestParams : never) => ReturnType<TCallback>['1'];
186
+ /** Extract the callback from */
187
+ declare const unsealStrictCallback: UnsealStrictCallback;
188
+
189
+ export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type StrictCallback, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, unsealStrictCallback, update };
package/dist/index.d.ts CHANGED
@@ -21,7 +21,7 @@ type Options = {
21
21
  onNewIndexes?: OptionValues;
22
22
  onPathsIntersectedByPrimitiveValues?: OptionValues;
23
23
  };
24
- type ImmutableUpdate = <TObject>(state: TObject, dotPathUpdateObject: UpdateObject<TObject>, options?: Options) => TObject;
24
+ type ImmutableUpdate = <TObject, TAllowedDotPathKeys extends PropertyKey = DotPaths<TObject>>(state: TObject, dotPathUpdateObject: UpdateObject<TObject, TAllowedDotPathKeys>, options?: Options) => TObject;
25
25
  /**
26
26
  * Creates a new object applying immutable updates using dot-path syntax.
27
27
  * Can add or skip new props or indexes depending on options.
@@ -41,7 +41,7 @@ type ExtendableImmutableUpdate = <TObject, TUpdateObject extends UpdateObject<TO
41
41
  * @param dotPathUpdateObject Dot Path Update Object.
42
42
  * @returns New Updated Object.
43
43
  */
44
- declare const extendableUpdate: ExtendableImmutableUpdate;
44
+ declare const experimental_extendableUpdate: ExtendableImmutableUpdate;
45
45
 
46
46
  /**
47
47
  * --------------------------------------------
@@ -177,4 +177,13 @@ declare const isObjectLiteral: (valueToTest: any) => boolean;
177
177
  declare const parseStringIndex: (v: string) => number | null;
178
178
  declare const isStringIndex: (v: string) => boolean;
179
179
 
180
- export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type UpdateObject, type UpdateValue, type ValueInPath, extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, update };
180
+ type StrictCallbackError = "CALLBACK STRICTNESS RETURN ERROR: return the value using the first parameter";
181
+ type StrictCallbackValue<T> = [StrictCallbackError, T];
182
+ type StrictCallbackFunction<T> = (v: T) => StrictCallbackValue<T>;
183
+ /** Apply strictness to a callback function */
184
+ type StrictCallback<T extends (...args: any[]) => any> = (r: StrictCallbackFunction<ReturnType<T>>, ...args: Parameters<T>) => [StrictCallbackError, ReturnType<T>];
185
+ type UnsealStrictCallback = <TCallback extends (r: StrictCallbackFunction<any>, ...args: any[]) => any>(callback: TCallback) => (...args: Parameters<TCallback> extends [infer _, ...infer RestParams] ? RestParams : never) => ReturnType<TCallback>['1'];
186
+ /** Extract the callback from */
187
+ declare const unsealStrictCallback: UnsealStrictCallback;
188
+
189
+ export { type DotPaths, type KeyOf, type LiteralIndex, type ReturnedValueInPath, type StrictCallback, type UpdateObject, type UpdateValue, type ValueInPath, experimental_extendableUpdate, isObjectLiteral, isStringIndex, parseStringIndex, remove, select, set, splitPathAtLastKey, unsealStrictCallback, update };
package/dist/index.js CHANGED
@@ -1 +1 @@
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});
1
+ "use strict";var x=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var D=(t,e)=>{for(var r in e)x(t,r,{get:e[r],enumerable:!0})},N=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of R(e))!v.call(t,a)&&a!==r&&x(t,a,{get:()=>e[a],enumerable:!(n=V(e,a))||n.enumerable});return t};var $=t=>N(x({},"__esModule",{value:!0}),t);var B={};D(B,{experimental_extendableUpdate:()=>A,isObjectLiteral:()=>c,isStringIndex:()=>y,parseStringIndex:()=>K,remove:()=>I,select:()=>j,set:()=>S,splitPathAtLastKey:()=>w,unsealStrictCallback:()=>g,update:()=>U});module.exports=$(B);var I=(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(),i=[...t];return r.forEach(s=>{i[s]=a}),i.filter(s=>s!==a)}let n={...t};return r.forEach(a=>delete n[a]),n};var j=(t,e)=>{if(typeof e!="string")throw new Error("path is not a string");if(!e)return t;let r=e.split("."),n=t;for(;r.length;){let a=r.join(".");if(n?.hasOwnProperty(a))return n[a];if(!n?.hasOwnProperty(r[0]))return;n=n[r[0]],r.shift()}return n};var w=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",n=t.slice(e);return[r,n]};var S=t=>()=>t;var c=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var K=t=>{let e=parseInt(t);return e.toString()===t?e:null},y=t=>parseInt(t).toString()===t;var k=t=>JSON.stringify(t),f=t=>Array.isArray(t),C=(t,e,r={})=>{if(!c(t)&&!f(t))throw new Error(`"${k(t)}" is not an literalObject/array`);if(!c(e))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(t)?[...t]:{...t},a={};for(let[i,T]of Object.entries(e)){if(i==="")continue;let s=i.split("."),d=a,p=n;for(;s.length>1;){let h=s.join(".");if(Object.hasOwn(p,h)){s=[h];break}let o=s[0],b=p[o],O=c(b),m=f(b);if(!O&&!m&&!(b===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(!O&&!m){let E=f(p)&&y(o),L=y(s[1]),P=E?r.onNewIndexes:r.onNewProps;if(P==="error")throw new Error(`${o} doesn't exist in "${i}"`);if(P==="skip")continue;p[o]=L?[]:{}}else p[o]=m?[...b]:{...b};d[o]={}}p=p[o],d=d[o],s.shift()}let l=s[0],u=p;if(!Object.hasOwn(u,l)){let o=f(u)&&y(l)?r.onNewIndexes:r.onNewProps;if(o==="error")throw new Error(`Property/Index "${l}" doesnt exist in the path "${i}". Its value is: "${k(u)}"`);if(o==="skip")continue}u[l]=typeof T=="function"?T(u[l]):T,delete d[l]}return n},U=(t,e,r)=>C(t,e,r),A=(t,e)=>C(t,e);var g=t=>(...e)=>t(r=>r,...e);0&&(module.exports={experimental_extendableUpdate,isObjectLiteral,isStringIndex,parseStringIndex,remove,select,set,splitPathAtLastKey,unsealStrictCallback,update});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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};
1
+ var S=(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 s=Symbol(),i=[...t];return r.forEach(o=>{i[o]=s}),i.filter(o=>o!==s)}let n={...t};return r.forEach(s=>delete n[s]),n};var K=(t,e)=>{if(typeof e!="string")throw new Error("path is not a string");if(!e)return t;let r=e.split("."),n=t;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 k=t=>{if(typeof t!="string")throw new Error(`Wrong dotPath type: ${t}`);let e=t.lastIndexOf(".")+1,r=e?t.slice(0,e-1):"",n=t.slice(e);return[r,n]};var C=t=>()=>t;var u=t=>!!t&&Object.getPrototypeOf(t)===Object.prototype;var U=t=>{let e=parseInt(t);return e.toString()===t?e:null},b=t=>parseInt(t).toString()===t;var P=t=>JSON.stringify(t),f=t=>Array.isArray(t),I=(t,e,r={})=>{if(!u(t)&&!f(t))throw new Error(`"${P(t)}" is not an literalObject/array`);if(!u(e))throw new Error('wrong "updateObject" type provided');let n=Array.isArray(t)?[...t]:{...t},s={};for(let[i,T]of Object.entries(e)){if(i==="")continue;let o=i.split("."),c=s,p=n;for(;o.length>1;){let h=o.join(".");if(Object.hasOwn(p,h)){o=[h];break}let a=o[0],d=p[a],x=u(d),m=f(d);if(!x&&!m&&!(d===void 0)){if(r.onPathsIntersectedByPrimitiveValues==="error")throw new Error(`Property "${a}" in "${i}" is a primitive`);if(r.onPathsIntersectedByPrimitiveValues==="skip")continue}if(!c[a]){if(!x&&!m){let j=f(p)&&b(a),w=b(o[1]),O=j?r.onNewIndexes:r.onNewProps;if(O==="error")throw new Error(`${a} doesn't exist in "${i}"`);if(O==="skip")continue;p[a]=w?[]:{}}else p[a]=m?[...d]:{...d};c[a]={}}p=p[a],c=c[a],o.shift()}let l=o[0],y=p;if(!Object.hasOwn(y,l)){let a=f(y)&&b(l)?r.onNewIndexes:r.onNewProps;if(a==="error")throw new Error(`Property/Index "${l}" doesnt exist in the path "${i}". Its value is: "${P(y)}"`);if(a==="skip")continue}y[l]=typeof T=="function"?T(y[l]):T,delete c[l]}return n},A=(t,e,r)=>I(t,e,r),g=(t,e)=>I(t,e);var E=t=>(...e)=>t(r=>r,...e);export{g as experimental_extendableUpdate,u as isObjectLiteral,b as isStringIndex,U as parseStringIndex,S as remove,K as select,C as set,k as splitPathAtLastKey,E as unsealStrictCallback,A as update};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.7.3",
3
+ "version": "2.8.1",
4
4
  "description": "A lightweight 600 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",