New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

object-property-assigner

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-property-assigner - npm Package Compare versions

Comparing version 1.2.4 to 1.3.0

2

build/index.cjs.js

@@ -1,1 +0,1 @@

"use strict";const r=(r,e,t,n,o)=>{if(!n)throw new Error(null!=o?o:`Invalid property path: ${e}\nCouldn't access "${t}" in ${JSON.stringify(r)}`)},e=r=>"object"==typeof r&&null!==r&&!Array.isArray(r),t=r=>Array.isArray(r),n=(r,e)=>r.filter(((r,t)=>t!==e)),o=(l,s,u,f)=>{const c=e(l)?Object.assign({},l):null,y=t(l)?[...l]:null;if(0===s.length)return l;if(!c&&!y)throw new Error("Can't assign property -- invalid input object");const{createNew:p,remove:m,noError:h,fullData:g,fullPath:b}=f,d=s[0];if(y&&"string"==typeof d)return y.map((r=>o(r,s,u,f)));if(1===s.length)return c&&"string"==typeof d?(a(c,d,u,f),c):y&&"number"==typeof d?(i(y,d,u,f),y):(r(g,b,d,h),l);const A=c||y||[];if(m&&2===s.length&&"number"==typeof s[1]){const e=A[d],o=s[1];return t(e)?A[d]=n(e,o):r(g,b,d,h,"Trying to remove an indexed item from an object that is not an array"),A}const v=s.slice(1);if(d in l){if(w=A[d],!t(w)&&!e(w)){if(!p)return r(g,b,d,h),A;A[d]={}}return A[d]=o(A[d],v,u,f),A}var w;if(p){const r="number"==typeof v[0]?[]:{};if(c)return A[d]=r,A[d]=o(A[d],v,u,f),A;if(y&&Array.isArray(A)){A.push(r);const e=A.length-1;return A[e]=o(A[e],v,u,f),A}}return r(g,b,d,h),A},a=(e,t,n,o)=>{const{remove:a,createNew:i,noError:l,fullData:s,fullPath:u}=o,f=t in e;a?f?delete e[t]:r(s,u,t,l):i||f?e[t]=n:r(s,u,t,l)},i=(e,t,n,o)=>{const{noError:a,fullData:i,fullPath:l,createNew:s}=o;t in e?e[t]=n:s?e.push(n):r(i,l,t,a)};module.exports=(r,e,a,i={})=>{const{remove:l=!1,createNew:s=!0,noError:u=!1}=i;var f;const c={remove:l,createNew:s,noError:u,fullData:r,fullPath:"string"==typeof(f=e)?f:"number"==typeof f?String(f):f.reduce(((r,e)=>"number"==typeof e?`${r}[${e}]`:""===r?e:`${r}.${e}`),"")},y=Array.isArray(e)?e:(r=>Array.isArray(r)?r:r.split(/(\.|\[\d+\])/).filter((r=>"."!==r&&""!==r)).map((r=>{const e=/\[(\d+)\]/.exec(r);return e?Number(e[1]):r})).flat())(e).filter((r=>""!==r));return t(r)&&l&&1===y.length?n(r,y[0]):o(r,y,a,c)};
"use strict";const r=(r,e,t,n,o)=>{if(!n)throw new Error(null!=o?o:`Invalid property path: ${e}\nCouldn't access "${t}" in ${JSON.stringify(r)}`)},e=r=>"object"==typeof r&&null!==r&&!Array.isArray(r),t=r=>Array.isArray(r),n=(r,e)=>r.filter(((r,t)=>t!==e)),o=(a,l,f,u)=>{const c=e(a)?Object.assign({},a):null,y=t(a)?[...a]:null;if(0===l.length)return a;if(!c&&!y)throw new Error("Can't assign property -- invalid input object");const{createNew:p,remove:m,noError:g,fullData:h,fullPath:b}=u,d=l[0];if(y&&"string"==typeof d)return y.map((r=>o(r,l,f,u)));if(1===l.length){if(c&&"string"==typeof d){const r=i(c,d,f,u);return null!=r?r:c}return y&&"number"==typeof d?(s(y,d,f,u),y):(r(h,b,d,g),a)}const A=c||y||[];if(m&&2===l.length&&"number"==typeof l[1]){const e=A[d],o=l[1];return t(e)?A[d]=n(e,o):r(h,b,d,g,"Trying to remove an indexed item from an object that is not an array"),A}const v=l.slice(1);if(d in a){if(w=A[d],!t(w)&&!e(w)){if(!p)return r(h,b,d,g),A;A[d]={}}return A[d]=o(A[d],v,f,u),A}var w;if(p){const r="number"==typeof v[0]?[]:{};if(c)return A[d]=r,A[d]=o(A[d],v,f,u),A;if(y&&Array.isArray(A)){A.push(r);const e=A.length-1;return A[e]=o(A[e],v,f,u),A}}return r(h,b,d,g),A},i=(e,t,n,o)=>{const{remove:i,createNew:s,noError:a,insertAfter:l,insertBefore:f,fullData:u,fullPath:c}=o;if(f||l){const r=Object.entries(e);let o=r.findIndex((([r,e])=>r===(null!=f?f:l)));return l&&o++,r.splice(o,0,[t,n]),Object.fromEntries(r)}const y=t in e;i?y?delete e[t]:r(u,c,t,a):s||y?e[t]=n:r(u,c,t,a)},s=(e,t,n,o)=>{const{noError:i,fullData:s,fullPath:a,createNew:l,insert:f}=o;f&&e.splice(t,0,n),t in e?e[t]=n:l?e.push(n):r(s,a,t,i)};module.exports=(r,e,i,s={})=>{const{remove:a=!1,createNew:l=!0,noError:f=!1}=s,u=r,c="string"==typeof(y=e)?y:"number"==typeof y?String(y):y.reduce(((r,e)=>"number"==typeof e?`${r}[${e}]`:""===r?e:`${r}.${e}`),"");var y;const p=Object.assign(Object.assign({},s),{remove:a,createNew:l,noError:f,fullData:u,fullPath:c}),m=Array.isArray(e)?e:(r=>Array.isArray(r)?r:r.split(/(\.|\[\d+\])/).filter((r=>"."!==r&&""!==r)).map((r=>{const e=/\[(\d+)\]/.exec(r);return e?Number(e[1]):r})).flat())(e).filter((r=>""!==r));return t(r)&&a&&1===m.length?n(r,m[0]):o(r,m,i,p)};

@@ -12,2 +12,5 @@ type BasicType = string | number | boolean | undefined | null | Function;

createNew?: boolean;
insert?: boolean;
insertBefore?: string;
insertAfter?: string;
}

@@ -14,0 +17,0 @@ type Path = (string | number)[];

@@ -1,1 +0,1 @@

const r=(r,e,t,n,o)=>{if(!n)throw new Error(null!=o?o:`Invalid property path: ${e}\nCouldn't access "${t}" in ${JSON.stringify(r)}`)},e=r=>"object"==typeof r&&null!==r&&!Array.isArray(r),t=r=>Array.isArray(r),n=(r,e)=>r.filter(((r,t)=>t!==e)),o=(r,e,o,i={})=>{const{remove:l=!1,createNew:f=!0,noError:u=!1}=i;var s;const c={remove:l,createNew:f,noError:u,fullData:r,fullPath:"string"==typeof(s=e)?s:"number"==typeof s?String(s):s.reduce(((r,e)=>"number"==typeof e?`${r}[${e}]`:""===r?e:`${r}.${e}`),"")},y=Array.isArray(e)?e:(r=>Array.isArray(r)?r:r.split(/(\.|\[\d+\])/).filter((r=>"."!==r&&""!==r)).map((r=>{const e=/\[(\d+)\]/.exec(r);return e?Number(e[1]):r})).flat())(e).filter((r=>""!==r));return t(r)&&l&&1===y.length?n(r,y[0]):a(r,y,o,c)},a=(o,f,u,s)=>{const c=e(o)?Object.assign({},o):null,y=t(o)?[...o]:null;if(0===f.length)return o;if(!c&&!y)throw new Error("Can't assign property -- invalid input object");const{createNew:p,remove:h,noError:m,fullData:g,fullPath:b}=s,d=f[0];if(y&&"string"==typeof d)return y.map((r=>a(r,f,u,s)));if(1===f.length)return c&&"string"==typeof d?(i(c,d,u,s),c):y&&"number"==typeof d?(l(y,d,u,s),y):(r(g,b,d,m),o);const A=c||y||[];if(h&&2===f.length&&"number"==typeof f[1]){const e=A[d],o=f[1];return t(e)?A[d]=n(e,o):r(g,b,d,m,"Trying to remove an indexed item from an object that is not an array"),A}const v=f.slice(1);if(d in o){if(w=A[d],!t(w)&&!e(w)){if(!p)return r(g,b,d,m),A;A[d]={}}return A[d]=a(A[d],v,u,s),A}var w;if(p){const r="number"==typeof v[0]?[]:{};if(c)return A[d]=r,A[d]=a(A[d],v,u,s),A;if(y&&Array.isArray(A)){A.push(r);const e=A.length-1;return A[e]=a(A[e],v,u,s),A}}return r(g,b,d,m),A},i=(e,t,n,o)=>{const{remove:a,createNew:i,noError:l,fullData:f,fullPath:u}=o,s=t in e;a?s?delete e[t]:r(f,u,t,l):i||s?e[t]=n:r(f,u,t,l)},l=(e,t,n,o)=>{const{noError:a,fullData:i,fullPath:l,createNew:f}=o;t in e?e[t]=n:f?e.push(n):r(i,l,t,a)};export{o as default};
const r=(r,e,t,n,o)=>{if(!n)throw new Error(null!=o?o:`Invalid property path: ${e}\nCouldn't access "${t}" in ${JSON.stringify(r)}`)},e=r=>"object"==typeof r&&null!==r&&!Array.isArray(r),t=r=>Array.isArray(r),n=(r,e)=>r.filter(((r,t)=>t!==e)),o=(r,e,o,a={})=>{const{remove:s=!1,createNew:l=!0,noError:f=!1}=a,u=r,c="string"==typeof(y=e)?y:"number"==typeof y?String(y):y.reduce(((r,e)=>"number"==typeof e?`${r}[${e}]`:""===r?e:`${r}.${e}`),"");var y;const p=Object.assign(Object.assign({},a),{remove:s,createNew:l,noError:f,fullData:u,fullPath:c}),m=Array.isArray(e)?e:(r=>Array.isArray(r)?r:r.split(/(\.|\[\d+\])/).filter((r=>"."!==r&&""!==r)).map((r=>{const e=/\[(\d+)\]/.exec(r);return e?Number(e[1]):r})).flat())(e).filter((r=>""!==r));return t(r)&&s&&1===m.length?n(r,m[0]):i(r,m,o,p)},i=(o,l,f,u)=>{const c=e(o)?Object.assign({},o):null,y=t(o)?[...o]:null;if(0===l.length)return o;if(!c&&!y)throw new Error("Can't assign property -- invalid input object");const{createNew:p,remove:m,noError:g,fullData:h,fullPath:b}=u,d=l[0];if(y&&"string"==typeof d)return y.map((r=>i(r,l,f,u)));if(1===l.length){if(c&&"string"==typeof d){const r=a(c,d,f,u);return null!=r?r:c}return y&&"number"==typeof d?(s(y,d,f,u),y):(r(h,b,d,g),o)}const A=c||y||[];if(m&&2===l.length&&"number"==typeof l[1]){const e=A[d],o=l[1];return t(e)?A[d]=n(e,o):r(h,b,d,g,"Trying to remove an indexed item from an object that is not an array"),A}const v=l.slice(1);if(d in o){if(w=A[d],!t(w)&&!e(w)){if(!p)return r(h,b,d,g),A;A[d]={}}return A[d]=i(A[d],v,f,u),A}var w;if(p){const r="number"==typeof v[0]?[]:{};if(c)return A[d]=r,A[d]=i(A[d],v,f,u),A;if(y&&Array.isArray(A)){A.push(r);const e=A.length-1;return A[e]=i(A[e],v,f,u),A}}return r(h,b,d,g),A},a=(e,t,n,o)=>{const{remove:i,createNew:a,noError:s,insertAfter:l,insertBefore:f,fullData:u,fullPath:c}=o;if(f||l){const r=Object.entries(e);let o=r.findIndex((([r,e])=>r===(null!=f?f:l)));return l&&o++,r.splice(o,0,[t,n]),Object.fromEntries(r)}const y=t in e;i?y?delete e[t]:r(u,c,t,s):a||y?e[t]=n:r(u,c,t,s)},s=(e,t,n,o)=>{const{noError:i,fullData:a,fullPath:s,createNew:l,insert:f}=o;f&&e.splice(t,0,n),t in e?e[t]=n:l?e.push(n):r(a,s,t,i)};export{o as default};
{
"name": "object-property-assigner",
"version": "1.2.4",
"version": "1.3.0",
"description": "A lightweight (no dependencies) tool to assign deeply nested properties in JS Objects (incl. Arrays)",

@@ -5,0 +5,0 @@ "main": "build/index.cjs.js",

@@ -95,2 +95,14 @@ # Object Property Assigner

```
- `insert` (`number`) -- (only for arrays). If this property is set, then the new value will be *inserted* into the array at this position and all other elements shuffled along. e.g.
```js
assign( {myArray: [1, 2, 3]}, "myArray[1]", "New Value")
// --> {myArray: [1, "New Value", 2, 3]}
```
- `insertBefore` (`string`) -- (only for objects). If this property is set, then the new value will be inserted *before* the key specified in `insertBefore`. e.g.
```js
assign( {one: 1, two: 2, three: 3}, "myNewValue", 69, {insertBefore: "three"} )
// --> {one: 1, two: 2, myNewValue: 69, three: 3}
```
**Please note**: Javascript does *not* guarantee object property order, so this option may yield unpredictable results, especially if using non-standard object keys (e.g. numbers). See [here](https://dev.to/frehner/the-order-of-js-object-keys-458d) for an explanation of how key ordering is handled.
- `insertAfter` (`string`) -- (only for objects). Same as `insertBefore`, but inserts *after* a particular object key. Same caveats apply.
- `noError` -- (default: `false`). If a property doesn't exist *and* `createNew == false`, then an error will be thrown. If you'd rather it just silently ignored the missing property, then set this parameter to `true`. Note that this is only for errors due to invalid property strings -- other errors might still be thrown.

@@ -97,0 +109,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc