@sagold/json-pointer
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("jsonPointer",[],t):"object"==typeof exports?exports.jsonPointer=t():e.jsonPointer=t()}("undefined"!=typeof self?self:this,(()=>(()=>{"use strict";var e={d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function n(e){return"#"===e||""===e||Array.isArray(e)&&0===e.length||!1}e.r(t),e.d(t,{default:()=>_,get:()=>s,isRoot:()=>n,join:()=>P,remove:()=>v,removeUndefinedItems:()=>m,set:()=>g,split:()=>f,splitLast:()=>O});const r=/~1/g,o=/~0/g,i=/(^#?\/?)/g;function l(e){return e.replace(r,"/").replace(o,"~")}function u(e){return l(decodeURIComponent(e))}function f(e){if(null==e||"string"!=typeof e||n(e))return Array.isArray(e)?e:[];const t=e.indexOf("#")>=0?u:l,r=(e=e.replace(i,"")).split("/");for(let e=0,n=r.length;e<n;e+=1)r[e]=t(r[e]);return r}function s(e,t,r=void 0){if(null==t||null==e)return r;if(n(t))return e;const o=c(e,f(t));return void 0===o?r:o}function c(e,t){const n=t.shift();if(void 0!==e)return void 0!==n?c(e[n],t):e}const p=/^\[.*\]$/,d=/^\[(.+)\]$/;function a(e,t){return"__proto__"===e||"constructor"==e&&t.length>0&&"prototype"==t[0]}function g(e,t,n){if(null==t)return e;const r=f(t);if(0===r.length)return e;null==e&&(e=p.test(r[0])?[]:{});let o,i,l=e;for(;r.length>1;)o=r.shift(),i=p.test(r[0]),a(o,r)||(l=h(l,o,i));return o=r.pop(),y(l,o,n),e}function y(e,t,n){let r;const o=t.match(d);"[]"===t&&Array.isArray(e)?e.push(n):o?(r=o.pop(),e[r]=n):e[t]=n}function h(e,t,n){if(null!=e[t])return e[t];const r=n?[]:{};return y(e,t,r),r}function m(e){let t=0,n=0;for(;t+n<e.length;)void 0===e[t+n]&&(n+=1),e[t]=e[t+n],t+=1;return e.length=e.length-n,e}function v(e,t,n){const r=f(t),o=r.pop(),i=s(e,r);return i&&delete i[o],Array.isArray(i)&&!0!==n&&m(i),e}const j=/~/g,b=/\//g;function A(e,t){if(0===e.length)return t?"#":"";for(let n=0,r=e.length;n<r;n+=1)e[n]=e[n].replace(j,"~0").replace(b,"~1"),t&&(e[n]=encodeURIComponent(e[n]));return(t?"#/":"/")+e.join("/")}function P(e,...t){const n=[];if(Array.isArray(e))return A(e,!0===arguments[1]);const r=arguments[arguments.length-1],o="boolean"==typeof r?r:e&&"#"===e[0];for(let e=0,t=arguments.length;e<t;e+=1)n.push.apply(n,f(arguments[e]));const i=[];for(let e=0,t=n.length;e<t;e+=1)if(".."===n[e]){if(0===i.length)return o?"#":"";i.pop()}else i.push(n[e]);return A(i,o)}function O(e){const t=f(e);if(0===t.length)return"string"==typeof e&&"#"===e[0]?["#",t[0]]:["",void 0];if(1===t.length)return"#"===e[0]?["#",t[0]]:["",t[0]];const n=t.pop();return[P(t,"#"===e[0]),n]}const _={get:s,set:g,remove:v,join:P,split:f,splitLast:O,isRoot:n,removeUndefinedItems:m};return t})())); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("jsonPointer",[],e):"object"==typeof exports?exports.jsonPointer=e():t.jsonPointer=e()}("undefined"!=typeof self?self:this,(()=>(()=>{"use strict";var t={d:(e,n)=>{for(var o in n)t.o(n,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:n[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function n(t){return"#"===t||""===t||Array.isArray(t)&&0===t.length||!1}t.r(e),t.d(e,{default:()=>_,get:()=>s,isRoot:()=>n,join:()=>P,remove:()=>m,removeUndefinedItems:()=>v,set:()=>g,split:()=>f,splitLast:()=>O});const o=/~1/g,r=/~0/g,i=/(^#?\/?)/g;function l(t){return t.replace(o,"/").replace(r,"~")}function u(t){return l(decodeURIComponent(t))}function f(t){if(null==t||"string"!=typeof t||n(t))return Array.isArray(t)?t:[];const e=t.indexOf("#")>=0?u:l,o=(t=t.replace(i,"")).split("/");for(let t=0,n=o.length;t<n;t+=1)o[t]=e(o[t]);return o}function s(t,e,o=void 0){if(null==e||null==t)return o;if(n(e))return t;const r=c(t,f(e));return void 0===r?o:r}function c(t,e){const n=e.shift();if(void 0!==t)return void 0!==n?c(t[n],e):t}const p=/^\[.*\]$/,d=/^\[(.+)\]$/;function a(t,e){return"__proto__"===t||"constructor"==t&&e.length>0&&"prototype"==e[0]}function g(t,e,n){if(null==e)return t;const o=f(e);if(0===o.length)return t;null==t&&(t=p.test(o[0])?[]:{});let r,i,l=t;for(;o.length>1;)r=o.shift(),i=p.test(o[0]),a(r,o)||(l=h(l,r,i));return r=o.pop(),y(l,r,n),t}function y(t,e,n){let o;const r=e.match(d);"[]"===e&&Array.isArray(t)?t.push(n):r?(o=r.pop(),t[o]=n):t[e]=n}function h(t,e,n){var o,r;const i=null!==(r=null===(o=e.match(d))||void 0===o?void 0:o.pop())&&void 0!==r?r:e;if(null!=t[i])return t[i];const l=n?[]:{};return y(t,e,l),l}function v(t){let e=0,n=0;for(;e+n<t.length;)void 0===t[e+n]&&(n+=1),t[e]=t[e+n],e+=1;return t.length=t.length-n,t}function m(t,e,n){const o=f(e),r=o.pop(),i=s(t,o);return i&&delete i[r],Array.isArray(i)&&!0!==n&&v(i),t}const j=/~/g,b=/\//g;function A(t,e){if(0===t.length)return e?"#":"";for(let n=0,o=t.length;n<o;n+=1)t[n]=t[n].replace(j,"~0").replace(b,"~1"),e&&(t[n]=encodeURIComponent(t[n]));return(e?"#/":"/")+t.join("/")}function P(t,...e){const n=[];if(Array.isArray(t))return A(t,!0===arguments[1]);const o=arguments[arguments.length-1],r="boolean"==typeof o?o:t&&"#"===t[0];for(let t=0,e=arguments.length;t<e;t+=1)n.push.apply(n,f(arguments[t]));const i=[];for(let t=0,e=n.length;t<e;t+=1)if(".."===n[t]){if(0===i.length)return r?"#":"";i.pop()}else i.push(n[t]);return A(i,r)}function O(t){const e=f(t);if(0===e.length)return"string"==typeof t&&"#"===t[0]?["#",e[0]]:["",void 0];if(1===e.length)return"#"===t[0]?["#",e[0]]:["",e[0]];const n=e.pop();return[P(e,"#"===t[0]),n]}const _={get:s,set:g,remove:m,join:P,split:f,splitLast:O,isRoot:n,removeUndefinedItems:v};return e})())); |
@@ -64,4 +64,5 @@ import { split } from "./split"; | ||
function create(data, key, isArray) { | ||
if (data[key] != null) { | ||
return data[key]; | ||
const property = key.match(arrayIndex)?.pop() ?? key; | ||
if (data[property] != null) { | ||
return data[property]; | ||
} | ||
@@ -68,0 +69,0 @@ const value = isArray ? [] : {}; |
{ | ||
"name": "@sagold/json-pointer", | ||
"version": "6.0.1", | ||
"version": "6.0.2", | ||
"description": "json pointer - failsafe data retrieval from js and json objects", | ||
@@ -5,0 +5,0 @@ "main": "dist/jsonPointer.js", |
@@ -120,3 +120,26 @@ <h1 align="left"><img src="./docs/sagold-json-pointer.png" width="100%" alt="@sagold/json-pointer"></h1> | ||
#### array | ||
Per default `set` creates objects for each each missing data. Thus, a pointer to `/list/1` will create an object with `{ list: { 1: {} } }`. If you want to specify array-data you will need to wrap the array-index in array-brackets: | ||
```ts | ||
pointer.set({}, '/list/[1]', 42); | ||
// { list: [, 42] } | ||
``` | ||
Omitting the index from the array-brackets will **append** the item: | ||
```ts | ||
pointer.set({}, '/list/[]', 42); | ||
// { list: [42] } | ||
``` | ||
which also works for existing lists: | ||
```ts | ||
pointer.set({ list: ["first"] }, '/list/[]', 42); | ||
// { list: ["first", 42] } | ||
``` | ||
### remove | ||
@@ -258,6 +281,6 @@ | ||
- 2024/04/06 with `v6`, selection of empty properties is supported: | ||
- "/" now selects an empty property (previously root-pointer) | ||
- "a//b" is now a valid pointer to `"a" » "" » "b"` | ||
- `join` no longer removes double slashes when joining `join("/a/", "/b")` » "/a//b" | ||
- `"/"` now selects an empty property (previously root-pointer) | ||
- `"a//b"` is now a valid pointer to `"a" » "" » "b"` | ||
- `join` no longer removes double slashes when joining `join("/a/", "/b")` » `"/a//b"` | ||
- 2022/12/02 with `v5`, package has been renamed to `json-pointer` and published under `@sagold/json-pointer` | ||
- 2020/11/09 with `v4`, `pointer.delete` has been renamed to `remove` |
27877
2.69%431
0.23%284
8.81%