@hyperjump/json-pointer
Advanced tools
Comparing version 0.9.7 to 0.9.8
@@ -49,3 +49,5 @@ define((function () { 'use strict'; | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -55,43 +57,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -101,16 +124,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -120,22 +142,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -145,20 +169,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -165,0 +187,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
define((function(){"use strict";var r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const t=r,e=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");return r.split("/").slice(1).map(u)},n=(r,t,e,i)=>{if(0===r.length)return e;if(r.length>1){if(Array.isArray(t)){const o=r.shift(),l=[...t];return l[o]=n(r,f(t,o,i),e,s(o,i)),l}{const o=r.shift();return{...t,[o]:n(r,f(t,o,i),e,s(o,i))}}}if(Array.isArray(t)){const n=[...t];return n[c(t,r[0])]=e,n}return"object"==typeof t&&null!==t?{...t,[r[0]]:e}:f(t,r[0],i)},i=(r,t,e,n)=>{if(0!==r.length)if(1!==r.length||p(t)){const o=r.shift();i(r,f(t,o,n),e,s(o,n))}else{t[c(t,r[0])]=e}},o=(r,t,e)=>{if(0!=r.length){if(r.length>1){const n=r.shift(),i=f(t,n,e);return{...t,[n]:o(r,i,s(n,e))}}if(Array.isArray(t))return t.filter(((t,e)=>e!=r[0]));if("object"==typeof t&&null!==t){const{[r[0]]:e,...n}=t;return n}return f(t,r[0],e)}},l=(r,t,e)=>{if(0!==r.length)if(r.length>1){const n=r.shift(),i=f(t,n,e);l(r,i,s(n,e))}else Array.isArray(t)?t.splice(r[0],1):"object"==typeof t&&null!==t?delete t[r[0]]:f(t,r[0],e)},s=t(((r,t)=>t+"/"+a(r))),a=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),u=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),c=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,f=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(p(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[c(r,t)]},p=r=>null===r||"object"!=typeof r;return{nil:"",append:s,get:(r,t)=>{const n=e(r),i=r=>n.reduce((([r,t],e)=>[f(r,e,t),s(e,t)]),[r,""])[0];return void 0===t?i:i(t)},set:(r,i,o)=>{const l=e(r),s=t(((r,t)=>n(l,r,t,"")));return void 0===i?s:s(i,o)},assign:(r,n,o)=>{const l=e(r),s=t(((r,t)=>i(l,r,t,"")));return void 0===n?s:s(n,o)},unset:(r,t)=>{const n=e(r),i=r=>o(n,r,"");return void 0===t?i:i(t)},remove:(r,t)=>{const n=e(r),i=r=>l(n,r,"");return void 0===t?i:i(t)}}})); | ||
define((function(){"use strict";var r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const t=r,e="",n=Symbol("EXISTS"),o=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");let t=1,e=0;return o=>{if(o===n)return e<r.length;if(e>=r.length)return;const i=r.indexOf("/",t);e=-1===i?r.length:i;const l=d(r.slice(t,e));return t=e+1,l}},i=(r,t,e)=>{if(r(n)){const n=r();return i(r,y(t,n,e),a(n,e))}return t},l=(r,t,e,o)=>{const i=r();if(void 0===i)return e;if(r(n)){if(Array.isArray(t)){const n=[...t];return n[i]=l(r,y(t,i,o),e,a(i,o)),n}return{...t,[i]:l(r,y(t,i,o),e,a(i,o))}}if(Array.isArray(t)){const r=[...t];return r[p(t,i)]=e,r}return"object"==typeof t&&null!==t?{...t,[i]:e}:y(t,i,o)},s=(r,t,e,o)=>{const i=r();void 0!==i&&(r(n)||v(t)?s(r,y(t,i,o),e,a(i,o)):t[p(t,i)]=e)},u=(r,t,e)=>{const o=r();if(void 0!==o){if(r(n)){const n=y(t,o,e);return{...t,[o]:u(r,n,a(o,e))}}if(Array.isArray(t)){const r=[...t];return delete r[p(t,o)],r}if("object"==typeof t&&null!==t){const{[o]:r,...e}=t;return e}return y(t,o,e)}},c=(r,t,e)=>{const o=r();if(void 0!==o)if(r(n)){const n=y(t,o,e);c(r,n,a(o,e))}else v(t)?y(t,o,e):delete t[o]},a=t(((r,t)=>t+"/"+f(r))),f=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),d=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),p=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,y=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(v(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[p(r,t)]},v=r=>null===r||"object"!=typeof r;return{nil:e,append:a,get:(r,t)=>{const n=o(r),l=r=>i(n,r,e);return void 0===t?l:l(t)},set:(r,n,i)=>{const s=o(r),u=t(((r,t)=>l(s,r,t,e)));return void 0===n?u:u(n,i)},assign:(r,n,i)=>{const l=o(r),u=t(((r,t)=>s(l,r,t,e)));return void 0===n?u:u(n,i)},unset:(r,t)=>{const n=o(r),i=r=>u(n,r,e);return void 0===t?i:i(t)},remove:(r,t)=>{const n=o(r),i=r=>c(n,r,e);return void 0===t?i:i(t)}}})); | ||
//# sourceMappingURL=json-pointer-amd.min.js.map |
@@ -49,3 +49,5 @@ 'use strict'; | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -55,43 +57,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -101,16 +124,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -120,22 +142,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -145,20 +169,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -165,0 +187,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";const r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}},t=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");return r.split("/").slice(1).map(a)},e=(r,t,n,o)=>{if(0===r.length)return n;if(r.length>1){if(Array.isArray(t)){const i=r.shift(),s=[...t];return s[i]=e(r,c(t,i,o),n,l(i,o)),s}{const i=r.shift();return{...t,[i]:e(r,c(t,i,o),n,l(i,o))}}}if(Array.isArray(t)){const e=[...t];return e[u(t,r[0])]=n,e}return"object"==typeof t&&null!==t?{...t,[r[0]]:n}:c(t,r[0],o)},n=(r,t,e,o)=>{if(0!==r.length)if(1!==r.length||f(t)){const i=r.shift();n(r,c(t,i,o),e,l(i,o))}else{t[u(t,r[0])]=e}},o=(r,t,e)=>{if(0!=r.length){if(r.length>1){const n=r.shift(),i=c(t,n,e);return{...t,[n]:o(r,i,l(n,e))}}if(Array.isArray(t))return t.filter(((t,e)=>e!=r[0]));if("object"==typeof t&&null!==t){const{[r[0]]:e,...n}=t;return n}return c(t,r[0],e)}},i=(r,t,e)=>{if(0!==r.length)if(r.length>1){const n=r.shift(),o=c(t,n,e);i(r,o,l(n,e))}else Array.isArray(t)?t.splice(r[0],1):"object"==typeof t&&null!==t?delete t[r[0]]:c(t,r[0],e)},l=r(((r,t)=>t+"/"+s(r))),s=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),a=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),u=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,c=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(f(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[u(r,t)]},f=r=>null===r||"object"!=typeof r;var p={nil:"",append:l,get:(r,e)=>{const n=t(r),o=r=>n.reduce((([r,t],e)=>[c(r,e,t),l(e,t)]),[r,""])[0];return void 0===e?o:o(e)},set:(n,o,i)=>{const l=t(n),s=r(((r,t)=>e(l,r,t,"")));return void 0===o?s:s(o,i)},assign:(e,o,i)=>{const l=t(e),s=r(((r,t)=>n(l,r,t,"")));return void 0===o?s:s(o,i)},unset:(r,e)=>{const n=t(r),i=r=>o(n,r,"");return void 0===e?i:i(e)},remove:(r,e)=>{const n=t(r),o=r=>i(n,r,"");return void 0===e?o:o(e)}};module.exports=p; | ||
"use strict";const r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}},t="",e=Symbol("EXISTS"),n=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");let t=1,n=0;return o=>{if(o===e)return n<r.length;if(n>=r.length)return;const i=r.indexOf("/",t);n=-1===i?r.length:i;const l=f(r.slice(t,n));return t=n+1,l}},o=(r,t,n)=>{if(r(e)){const e=r();return o(r,p(t,e,n),c(e,n))}return t},i=(r,t,n,o)=>{const l=r();if(void 0===l)return n;if(r(e)){if(Array.isArray(t)){const e=[...t];return e[l]=i(r,p(t,l,o),n,c(l,o)),e}return{...t,[l]:i(r,p(t,l,o),n,c(l,o))}}if(Array.isArray(t)){const r=[...t];return r[d(t,l)]=n,r}return"object"==typeof t&&null!==t?{...t,[l]:n}:p(t,l,o)},l=(r,t,n,o)=>{const i=r();void 0!==i&&(r(e)||y(t)?l(r,p(t,i,o),n,c(i,o)):t[d(t,i)]=n)},s=(r,t,n)=>{const o=r();if(void 0!==o){if(r(e)){const e=p(t,o,n);return{...t,[o]:s(r,e,c(o,n))}}if(Array.isArray(t)){const r=[...t];return delete r[d(t,o)],r}if("object"==typeof t&&null!==t){const{[o]:r,...e}=t;return e}return p(t,o,n)}},u=(r,t,n)=>{const o=r();if(void 0!==o)if(r(e)){const e=p(t,o,n);u(r,e,c(o,n))}else y(t)?p(t,o,n):delete t[o]},c=r(((r,t)=>t+"/"+a(r))),a=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),f=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),d=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,p=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(y(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[d(r,t)]},y=r=>null===r||"object"!=typeof r;var v={nil:t,append:c,get:(r,e)=>{const i=n(r),l=r=>o(i,r,t);return void 0===e?l:l(e)},set:(e,o,l)=>{const s=n(e),u=r(((r,e)=>i(s,r,e,t)));return void 0===o?u:u(o,l)},assign:(e,o,i)=>{const s=n(e),u=r(((r,e)=>l(s,r,e,t)));return void 0===o?u:u(o,i)},unset:(r,e)=>{const o=n(r),i=r=>s(o,r,t);return void 0===e?i:i(e)},remove:(r,e)=>{const o=n(r),i=r=>u(o,r,t);return void 0===e?i:i(e)}};module.exports=v; | ||
//# sourceMappingURL=json-pointer-cjs.min.js.map |
@@ -47,3 +47,5 @@ var justCurryIt = curry$1; | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -53,43 +55,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -99,16 +122,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -118,22 +140,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -143,20 +167,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -163,0 +185,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
const r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}},t=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");return r.split("/").slice(1).map(a)},e=(r,t,n,o)=>{if(0===r.length)return n;if(r.length>1){if(Array.isArray(t)){const i=r.shift(),s=[...t];return s[i]=e(r,c(t,i,o),n,l(i,o)),s}{const i=r.shift();return{...t,[i]:e(r,c(t,i,o),n,l(i,o))}}}if(Array.isArray(t)){const e=[...t];return e[u(t,r[0])]=n,e}return"object"==typeof t&&null!==t?{...t,[r[0]]:n}:c(t,r[0],o)},n=(r,t,e,o)=>{if(0!==r.length)if(1!==r.length||f(t)){const i=r.shift();n(r,c(t,i,o),e,l(i,o))}else{t[u(t,r[0])]=e}},o=(r,t,e)=>{if(0!=r.length){if(r.length>1){const n=r.shift(),i=c(t,n,e);return{...t,[n]:o(r,i,l(n,e))}}if(Array.isArray(t))return t.filter(((t,e)=>e!=r[0]));if("object"==typeof t&&null!==t){const{[r[0]]:e,...n}=t;return n}return c(t,r[0],e)}},i=(r,t,e)=>{if(0!==r.length)if(r.length>1){const n=r.shift(),o=c(t,n,e);i(r,o,l(n,e))}else Array.isArray(t)?t.splice(r[0],1):"object"==typeof t&&null!==t?delete t[r[0]]:c(t,r[0],e)},l=r(((r,t)=>t+"/"+s(r))),s=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),a=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),u=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,c=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(f(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[u(r,t)]},f=r=>null===r||"object"!=typeof r;var p={nil:"",append:l,get:(r,e)=>{const n=t(r),o=r=>n.reduce((([r,t],e)=>[c(r,e,t),l(e,t)]),[r,""])[0];return void 0===e?o:o(e)},set:(n,o,i)=>{const l=t(n),s=r(((r,t)=>e(l,r,t,"")));return void 0===o?s:s(o,i)},assign:(e,o,i)=>{const l=t(e),s=r(((r,t)=>n(l,r,t,"")));return void 0===o?s:s(o,i)},unset:(r,e)=>{const n=t(r),i=r=>o(n,r,"");return void 0===e?i:i(e)},remove:(r,e)=>{const n=t(r),o=r=>i(n,r,"");return void 0===e?o:o(e)}};export{p as default}; | ||
const r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}},t="",e=Symbol("EXISTS"),n=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");let t=1,n=0;return o=>{if(o===e)return n<r.length;if(n>=r.length)return;const i=r.indexOf("/",t);n=-1===i?r.length:i;const l=f(r.slice(t,n));return t=n+1,l}},o=(r,t,n)=>{if(r(e)){const e=r();return o(r,p(t,e,n),a(e,n))}return t},i=(r,t,n,o)=>{const l=r();if(void 0===l)return n;if(r(e)){if(Array.isArray(t)){const e=[...t];return e[l]=i(r,p(t,l,o),n,a(l,o)),e}return{...t,[l]:i(r,p(t,l,o),n,a(l,o))}}if(Array.isArray(t)){const r=[...t];return r[d(t,l)]=n,r}return"object"==typeof t&&null!==t?{...t,[l]:n}:p(t,l,o)},l=(r,t,n,o)=>{const i=r();void 0!==i&&(r(e)||y(t)?l(r,p(t,i,o),n,a(i,o)):t[d(t,i)]=n)},s=(r,t,n)=>{const o=r();if(void 0!==o){if(r(e)){const e=p(t,o,n);return{...t,[o]:s(r,e,a(o,n))}}if(Array.isArray(t)){const r=[...t];return delete r[d(t,o)],r}if("object"==typeof t&&null!==t){const{[o]:r,...e}=t;return e}return p(t,o,n)}},u=(r,t,n)=>{const o=r();if(void 0!==o)if(r(e)){const e=p(t,o,n);u(r,e,a(o,n))}else y(t)?p(t,o,n):delete t[o]},a=r(((r,t)=>t+"/"+c(r))),c=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),f=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),d=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,p=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(y(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[d(r,t)]},y=r=>null===r||"object"!=typeof r;var v={nil:t,append:a,get:(r,e)=>{const i=n(r),l=r=>o(i,r,t);return void 0===e?l:l(e)},set:(e,o,l)=>{const s=n(e),u=r(((r,e)=>i(s,r,e,t)));return void 0===o?u:u(o,l)},assign:(e,o,i)=>{const s=n(e),u=r(((r,e)=>l(s,r,e,t)));return void 0===o?u:u(o,i)},unset:(r,e)=>{const o=n(r),i=r=>s(o,r,t);return void 0===e?i:i(e)},remove:(r,e)=>{const o=n(r),i=r=>u(o,r,t);return void 0===e?i:i(e)}};export{v as default}; | ||
//# sourceMappingURL=json-pointer-esm.min.js.map |
@@ -50,3 +50,5 @@ var JsonPointer = (function () { | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -56,43 +58,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -102,16 +125,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -121,22 +143,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -146,20 +170,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -166,0 +188,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
var JsonPointer=function(){"use strict";var r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const t=r,e=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");return r.split("/").slice(1).map(u)},n=(r,t,e,i)=>{if(0===r.length)return e;if(r.length>1){if(Array.isArray(t)){const o=r.shift(),l=[...t];return l[o]=n(r,f(t,o,i),e,s(o,i)),l}{const o=r.shift();return{...t,[o]:n(r,f(t,o,i),e,s(o,i))}}}if(Array.isArray(t)){const n=[...t];return n[c(t,r[0])]=e,n}return"object"==typeof t&&null!==t?{...t,[r[0]]:e}:f(t,r[0],i)},i=(r,t,e,n)=>{if(0!==r.length)if(1!==r.length||p(t)){const o=r.shift();i(r,f(t,o,n),e,s(o,n))}else{t[c(t,r[0])]=e}},o=(r,t,e)=>{if(0!=r.length){if(r.length>1){const n=r.shift(),i=f(t,n,e);return{...t,[n]:o(r,i,s(n,e))}}if(Array.isArray(t))return t.filter(((t,e)=>e!=r[0]));if("object"==typeof t&&null!==t){const{[r[0]]:e,...n}=t;return n}return f(t,r[0],e)}},l=(r,t,e)=>{if(0!==r.length)if(r.length>1){const n=r.shift(),i=f(t,n,e);l(r,i,s(n,e))}else Array.isArray(t)?t.splice(r[0],1):"object"==typeof t&&null!==t?delete t[r[0]]:f(t,r[0],e)},s=t(((r,t)=>t+"/"+a(r))),a=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),u=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),c=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,f=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(p(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[c(r,t)]},p=r=>null===r||"object"!=typeof r;return{nil:"",append:s,get:(r,t)=>{const n=e(r),i=r=>n.reduce((([r,t],e)=>[f(r,e,t),s(e,t)]),[r,""])[0];return void 0===t?i:i(t)},set:(r,i,o)=>{const l=e(r),s=t(((r,t)=>n(l,r,t,"")));return void 0===i?s:s(i,o)},assign:(r,n,o)=>{const l=e(r),s=t(((r,t)=>i(l,r,t,"")));return void 0===n?s:s(n,o)},unset:(r,t)=>{const n=e(r),i=r=>o(n,r,"");return void 0===t?i:i(t)},remove:(r,t)=>{const n=e(r),i=r=>l(n,r,"");return void 0===t?i:i(t)}}}(); | ||
var JsonPointer=function(){"use strict";var r=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const t=r,e="",n=Symbol("EXISTS"),o=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");let t=1,e=0;return o=>{if(o===n)return e<r.length;if(e>=r.length)return;const i=r.indexOf("/",t);e=-1===i?r.length:i;const l=d(r.slice(t,e));return t=e+1,l}},i=(r,t,e)=>{if(r(n)){const n=r();return i(r,y(t,n,e),a(n,e))}return t},l=(r,t,e,o)=>{const i=r();if(void 0===i)return e;if(r(n)){if(Array.isArray(t)){const n=[...t];return n[i]=l(r,y(t,i,o),e,a(i,o)),n}return{...t,[i]:l(r,y(t,i,o),e,a(i,o))}}if(Array.isArray(t)){const r=[...t];return r[p(t,i)]=e,r}return"object"==typeof t&&null!==t?{...t,[i]:e}:y(t,i,o)},s=(r,t,e,o)=>{const i=r();void 0!==i&&(r(n)||v(t)?s(r,y(t,i,o),e,a(i,o)):t[p(t,i)]=e)},u=(r,t,e)=>{const o=r();if(void 0!==o){if(r(n)){const n=y(t,o,e);return{...t,[o]:u(r,n,a(o,e))}}if(Array.isArray(t)){const r=[...t];return delete r[p(t,o)],r}if("object"==typeof t&&null!==t){const{[o]:r,...e}=t;return e}return y(t,o,e)}},c=(r,t,e)=>{const o=r();if(void 0!==o)if(r(n)){const n=y(t,o,e);c(r,n,a(o,e))}else v(t)?y(t,o,e):delete t[o]},a=t(((r,t)=>t+"/"+f(r))),f=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),d=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),p=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,y=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(v(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[p(r,t)]},v=r=>null===r||"object"!=typeof r;return{nil:e,append:a,get:(r,t)=>{const n=o(r),l=r=>i(n,r,e);return void 0===t?l:l(t)},set:(r,n,i)=>{const s=o(r),u=t(((r,t)=>l(s,r,t,e)));return void 0===n?u:u(n,i)},assign:(r,n,i)=>{const l=o(r),u=t(((r,t)=>s(l,r,t,e)));return void 0===n?u:u(n,i)},unset:(r,t)=>{const n=o(r),i=r=>u(n,r,e);return void 0===t?i:i(t)},remove:(r,t)=>{const n=o(r),i=r=>c(n,r,e);return void 0===t?i:i(t)}}}(); | ||
//# sourceMappingURL=json-pointer-iife.min.js.map |
@@ -52,3 +52,5 @@ System.register('JsonPointer', [], (function (exports) { | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -58,43 +60,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -104,16 +127,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -123,22 +145,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -148,20 +172,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -168,0 +190,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
System.register("JsonPointer",[],(function(r){"use strict";return{execute:function(){var t=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const e=t,n=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");return r.split("/").slice(1).map(c)},i=(r,t,e,n)=>{if(0===r.length)return e;if(r.length>1){if(Array.isArray(t)){const o=r.shift(),l=[...t];return l[o]=i(r,p(t,o,n),e,u(o,n)),l}{const o=r.shift();return{...t,[o]:i(r,p(t,o,n),e,u(o,n))}}}if(Array.isArray(t)){const n=[...t];return n[f(t,r[0])]=e,n}return"object"==typeof t&&null!==t?{...t,[r[0]]:e}:p(t,r[0],n)},o=(r,t,e,n)=>{if(0!==r.length)if(1!==r.length||h(t)){const i=r.shift();o(r,p(t,i,n),e,u(i,n))}else{t[f(t,r[0])]=e}},l=(r,t,e)=>{if(0!=r.length){if(r.length>1){const n=r.shift(),i=p(t,n,e);return{...t,[n]:l(r,i,u(n,e))}}if(Array.isArray(t))return t.filter(((t,e)=>e!=r[0]));if("object"==typeof t&&null!==t){const{[r[0]]:e,...n}=t;return n}return p(t,r[0],e)}},s=(r,t,e)=>{if(0!==r.length)if(r.length>1){const n=r.shift(),i=p(t,n,e);s(r,i,u(n,e))}else Array.isArray(t)?t.splice(r[0],1):"object"==typeof t&&null!==t?delete t[r[0]]:p(t,r[0],e)},u=e(((r,t)=>t+"/"+a(r))),a=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),c=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),f=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,p=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(h(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[f(r,t)]},h=r=>null===r||"object"!=typeof r;r("default",{nil:"",append:u,get:(r,t)=>{const e=n(r),i=r=>e.reduce((([r,t],e)=>[p(r,e,t),u(e,t)]),[r,""])[0];return void 0===t?i:i(t)},set:(r,t,o)=>{const l=n(r),s=e(((r,t)=>i(l,r,t,"")));return void 0===t?s:s(t,o)},assign:(r,t,i)=>{const l=n(r),s=e(((r,t)=>o(l,r,t,"")));return void 0===t?s:s(t,i)},unset:(r,t)=>{const e=n(r),i=r=>l(e,r,"");return void 0===t?i:i(t)},remove:(r,t)=>{const e=n(r),i=r=>s(e,r,"");return void 0===t?i:i(t)}})}}})); | ||
System.register("JsonPointer",[],(function(r){"use strict";return{execute:function(){var t=function(r,t){return function e(){null==t&&(t=r.length);var n=[].slice.call(arguments);return n.length>=t?r.apply(this,n):function(){return e.apply(this,n.concat([].slice.call(arguments)))}}};const e=t,n="",o=Symbol("EXISTS"),i=r=>{if(r.length>0&&"/"!==r[0])throw Error("Invalid JSON Pointer");let t=1,e=0;return n=>{if(n===o)return e<r.length;if(e>=r.length)return;const i=r.indexOf("/",t);e=-1===i?r.length:i;const s=p(r.slice(t,e));return t=e+1,s}},s=(r,t,e)=>{if(r(o)){const n=r();return s(r,v(t,n,e),f(n,e))}return t},u=(r,t,e,n)=>{const i=r();if(void 0===i)return e;if(r(o)){if(Array.isArray(t)){const o=[...t];return o[i]=u(r,v(t,i,n),e,f(i,n)),o}return{...t,[i]:u(r,v(t,i,n),e,f(i,n))}}if(Array.isArray(t)){const r=[...t];return r[y(t,i)]=e,r}return"object"==typeof t&&null!==t?{...t,[i]:e}:v(t,i,n)},l=(r,t,e,n)=>{const i=r();void 0!==i&&(r(o)||g(t)?l(r,v(t,i,n),e,f(i,n)):t[y(t,i)]=e)},c=(r,t,e)=>{const n=r();if(void 0!==n){if(r(o)){const o=v(t,n,e);return{...t,[n]:c(r,o,f(n,e))}}if(Array.isArray(t)){const r=[...t];return delete r[y(t,n)],r}if("object"==typeof t&&null!==t){const{[n]:r,...e}=t;return e}return v(t,n,e)}},a=(r,t,e)=>{const n=r();if(void 0!==n)if(r(o)){const o=v(t,n,e);a(r,o,f(n,e))}else g(t)?v(t,n,e):delete t[n]},f=e(((r,t)=>t+"/"+d(r))),d=r=>r.toString().replace(/~/g,"~0").replace(/\//g,"~1"),p=r=>r.toString().replace(/~1/g,"/").replace(/~0/g,"~"),y=(r,t)=>Array.isArray(r)&&"-"===t?r.length:t,v=(r,t,e="")=>{if(void 0===r)throw TypeError(`Value at '${e}' is undefined and does not have property '${t}'`);if(null===r)throw TypeError(`Value at '${e}' is null and does not have property '${t}'`);if(g(r))throw TypeError(`Value at '${e}' is a ${typeof r} and does not have property '${t}'`);return r[y(r,t)]},g=r=>null===r||"object"!=typeof r;r("default",{nil:n,append:f,get:(r,t)=>{const e=i(r),o=r=>s(e,r,n);return void 0===t?o:o(t)},set:(r,t,o)=>{const s=i(r),l=e(((r,t)=>u(s,r,t,n)));return void 0===t?l:l(t,o)},assign:(r,t,o)=>{const s=i(r),u=e(((r,t)=>l(s,r,t,n)));return void 0===t?u:u(t,o)},unset:(r,t)=>{const e=i(r),o=r=>c(e,r,n);return void 0===t?o:o(t)},remove:(r,t)=>{const e=i(r),o=r=>a(e,r,n);return void 0===t?o:o(t)}})}}})); | ||
//# sourceMappingURL=json-pointer-system.min.js.map |
@@ -53,3 +53,5 @@ (function (global, factory) { | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -59,43 +61,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -105,16 +128,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -124,22 +146,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -149,20 +173,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -169,0 +191,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).JsonPointer=t()}(this,(function(){"use strict";var e=function(e,t){return function r(){null==t&&(t=e.length);var n=[].slice.call(arguments);return n.length>=t?e.apply(this,n):function(){return r.apply(this,n.concat([].slice.call(arguments)))}}};const t=e,r=e=>{if(e.length>0&&"/"!==e[0])throw Error("Invalid JSON Pointer");return e.split("/").slice(1).map(u)},n=(e,t,r,o)=>{if(0===e.length)return r;if(e.length>1){if(Array.isArray(t)){const i=e.shift(),l=[...t];return l[i]=n(e,c(t,i,o),r,s(i,o)),l}{const i=e.shift();return{...t,[i]:n(e,c(t,i,o),r,s(i,o))}}}if(Array.isArray(t)){const n=[...t];return n[a(t,e[0])]=r,n}return"object"==typeof t&&null!==t?{...t,[e[0]]:r}:c(t,e[0],o)},o=(e,t,r,n)=>{if(0!==e.length)if(1!==e.length||p(t)){const i=e.shift();o(e,c(t,i,n),r,s(i,n))}else{t[a(t,e[0])]=r}},i=(e,t,r)=>{if(0!=e.length){if(e.length>1){const n=e.shift(),o=c(t,n,r);return{...t,[n]:i(e,o,s(n,r))}}if(Array.isArray(t))return t.filter(((t,r)=>r!=e[0]));if("object"==typeof t&&null!==t){const{[e[0]]:r,...n}=t;return n}return c(t,e[0],r)}},l=(e,t,r)=>{if(0!==e.length)if(e.length>1){const n=e.shift(),o=c(t,n,r);l(e,o,s(n,r))}else Array.isArray(t)?t.splice(e[0],1):"object"==typeof t&&null!==t?delete t[e[0]]:c(t,e[0],r)},s=t(((e,t)=>t+"/"+f(e))),f=e=>e.toString().replace(/~/g,"~0").replace(/\//g,"~1"),u=e=>e.toString().replace(/~1/g,"/").replace(/~0/g,"~"),a=(e,t)=>Array.isArray(e)&&"-"===t?e.length:t,c=(e,t,r="")=>{if(void 0===e)throw TypeError(`Value at '${r}' is undefined and does not have property '${t}'`);if(null===e)throw TypeError(`Value at '${r}' is null and does not have property '${t}'`);if(p(e))throw TypeError(`Value at '${r}' is a ${typeof e} and does not have property '${t}'`);return e[a(e,t)]},p=e=>null===e||"object"!=typeof e;return{nil:"",append:s,get:(e,t)=>{const n=r(e),o=e=>n.reduce((([e,t],r)=>[c(e,r,t),s(r,t)]),[e,""])[0];return void 0===t?o:o(t)},set:(e,o,i)=>{const l=r(e),s=t(((e,t)=>n(l,e,t,"")));return void 0===o?s:s(o,i)},assign:(e,n,i)=>{const l=r(e),s=t(((e,t)=>o(l,e,t,"")));return void 0===n?s:s(n,i)},unset:(e,t)=>{const n=r(e),o=e=>i(n,e,"");return void 0===t?o:o(t)},remove:(e,t)=>{const n=r(e),o=e=>l(n,e,"");return void 0===t?o:o(t)}}})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).JsonPointer=r()}(this,(function(){"use strict";var e=function(e,r){return function t(){null==r&&(r=e.length);var n=[].slice.call(arguments);return n.length>=r?e.apply(this,n):function(){return t.apply(this,n.concat([].slice.call(arguments)))}}};const r=e,t="",n=Symbol("EXISTS"),o=e=>{if(e.length>0&&"/"!==e[0])throw Error("Invalid JSON Pointer");let r=1,t=0;return o=>{if(o===n)return t<e.length;if(t>=e.length)return;const i=e.indexOf("/",r);t=-1===i?e.length:i;const l=d(e.slice(r,t));return r=t+1,l}},i=(e,r,t)=>{if(e(n)){const n=e();return i(e,y(r,n,t),f(n,t))}return r},l=(e,r,t,o)=>{const i=e();if(void 0===i)return t;if(e(n)){if(Array.isArray(r)){const n=[...r];return n[i]=l(e,y(r,i,o),t,f(i,o)),n}return{...r,[i]:l(e,y(r,i,o),t,f(i,o))}}if(Array.isArray(r)){const e=[...r];return e[p(r,i)]=t,e}return"object"==typeof r&&null!==r?{...r,[i]:t}:y(r,i,o)},s=(e,r,t,o)=>{const i=e();void 0!==i&&(e(n)||h(r)?s(e,y(r,i,o),t,f(i,o)):r[p(r,i)]=t)},u=(e,r,t)=>{const o=e();if(void 0!==o){if(e(n)){const n=y(r,o,t);return{...r,[o]:u(e,n,f(o,t))}}if(Array.isArray(r)){const e=[...r];return delete e[p(r,o)],e}if("object"==typeof r&&null!==r){const{[o]:e,...t}=r;return t}return y(r,o,t)}},c=(e,r,t)=>{const o=e();if(void 0!==o)if(e(n)){const n=y(r,o,t);c(e,n,f(o,t))}else h(r)?y(r,o,t):delete r[o]},f=r(((e,r)=>r+"/"+a(e))),a=e=>e.toString().replace(/~/g,"~0").replace(/\//g,"~1"),d=e=>e.toString().replace(/~1/g,"/").replace(/~0/g,"~"),p=(e,r)=>Array.isArray(e)&&"-"===r?e.length:r,y=(e,r,t="")=>{if(void 0===e)throw TypeError(`Value at '${t}' is undefined and does not have property '${r}'`);if(null===e)throw TypeError(`Value at '${t}' is null and does not have property '${r}'`);if(h(e))throw TypeError(`Value at '${t}' is a ${typeof e} and does not have property '${r}'`);return e[p(e,r)]},h=e=>null===e||"object"!=typeof e;return{nil:t,append:f,get:(e,r)=>{const n=o(e),l=e=>i(n,e,t);return void 0===r?l:l(r)},set:(e,n,i)=>{const s=o(e),u=r(((e,r)=>l(s,e,r,t)));return void 0===n?u:u(n,i)},assign:(e,n,i)=>{const l=o(e),u=r(((e,r)=>s(l,e,r,t)));return void 0===n?u:u(n,i)},unset:(e,r)=>{const n=o(e),i=e=>u(n,e,t);return void 0===r?i:i(r)},remove:(e,r)=>{const n=o(e),i=e=>c(n,e,t);return void 0===r?i:i(r)}}})); | ||
//# sourceMappingURL=json-pointer-umd.min.js.map |
130
lib/index.js
@@ -6,3 +6,5 @@ const curry = require("just-curry-it"); | ||
const compile = (pointer) => { | ||
const EXISTS = Symbol("EXISTS"); | ||
const segmentGenerator = (pointer) => { | ||
if (pointer.length > 0 && pointer[0] !== "/") { | ||
@@ -12,43 +14,64 @@ throw Error("Invalid JSON Pointer"); | ||
return pointer.split("/").slice(1).map(unescape); | ||
}; | ||
let segmentStart = 1; | ||
let segmentEnd = 0; | ||
const get = (pointer, value = undefined) => { | ||
const ptr = compile(pointer); | ||
return (mode) => { | ||
if (mode === EXISTS) { | ||
return segmentEnd < pointer.length; | ||
} | ||
const fn = (value) => ptr.reduce(([value, pointer], segment) => { | ||
return [applySegment(value, segment, pointer), append(segment, pointer)]; | ||
}, [value, ""])[0]; | ||
if (segmentEnd >= pointer.length) { | ||
return; | ||
} | ||
return value === undefined ? fn : fn(value); | ||
const position = pointer.indexOf("/", segmentStart); | ||
segmentEnd = position === -1 ? pointer.length : position; | ||
const segment = unescape(pointer.slice(segmentStart, segmentEnd)); | ||
segmentStart = segmentEnd + 1; | ||
return segment; | ||
}; | ||
}; | ||
const get = (pointer, subject = undefined) => { | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _get(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _get = (nextSegment, subject, cursor) => { | ||
if (!nextSegment(EXISTS)) { | ||
return subject; | ||
} else { | ||
const segment = nextSegment(); | ||
return _get(nextSegment, applySegment(subject, segment, cursor), append(segment, cursor)); | ||
} | ||
}; | ||
const set = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _set(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _set(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _set = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _set = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return value; | ||
} else if (pointer.length > 1) { | ||
} else if (nextSegment(EXISTS)) { | ||
if (Array.isArray(subject)) { | ||
const index = pointer.shift(); | ||
const clonedSubject = [...subject]; | ||
clonedSubject[index] = _set(pointer, applySegment(subject, index, cursor), value, append(index, cursor)); | ||
clonedSubject[segment] = _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return clonedSubject; | ||
} else { | ||
const segment = pointer.shift(); | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _set(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} | ||
} else if (Array.isArray(subject)) { | ||
const clonedSubject = [...subject]; | ||
const segment = computeSegment(subject, pointer[0]); | ||
clonedSubject[segment] = value; | ||
clonedSubject[computeSegment(subject, segment)] = value; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
return { ...subject, [pointer[0]]: value }; | ||
return { ...subject, [segment]: value }; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -58,16 +81,15 @@ }; | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = curry((subject, value) => _assign(nextSegment, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
const _assign = (nextSegment, subject, value, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = computeSegment(subject, pointer[0]); | ||
subject[segment] = value; | ||
} else if (!nextSegment(EXISTS) && !isScalar(subject)) { | ||
subject[computeSegment(subject, segment)] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(nextSegment, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
@@ -77,22 +99,24 @@ }; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _unset(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _unset = (pointer, subject, cursor) => { | ||
if (pointer.length == 0) { | ||
return undefined; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
const _unset = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
return { ...subject, [segment]: _unset(nextSegment, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
const clonedSubject = [...subject]; | ||
delete clonedSubject[computeSegment(subject, segment)]; | ||
return clonedSubject; | ||
} else if (typeof subject === "object" && subject !== null) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { [pointer[0]]: _, ...result } = subject; | ||
const { [segment]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
return applySegment(subject, segment, cursor); | ||
} | ||
@@ -102,20 +126,18 @@ }; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
const nextSegment = segmentGenerator(pointer); | ||
const fn = (subject) => _remove(nextSegment, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
const _remove = (nextSegment, subject, cursor) => { | ||
const segment = nextSegment(); | ||
if (segment === undefined) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
} else if (nextSegment(EXISTS)) { | ||
const value = applySegment(subject, segment, cursor); | ||
_remove(pointer, value, append(segment, cursor)); | ||
} else if (Array.isArray(subject)) { | ||
subject.splice(pointer[0], 1); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
delete subject[pointer[0]]; | ||
_remove(nextSegment, value, append(segment, cursor)); | ||
} else if (!isScalar(subject)) { | ||
delete subject[segment]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
applySegment(subject, segment, cursor); | ||
} | ||
@@ -122,0 +144,0 @@ }; |
{ | ||
"name": "@hyperjump/json-pointer", | ||
"version": "0.9.7", | ||
"version": "0.9.8", | ||
"description": "An RFC-6901 JSON Pointer implementation", | ||
@@ -49,4 +49,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"just-curry-it": "^5.2.1" | ||
"just-curry-it": "^5.3.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
AI detected security risk
Supply chain riskAI has determined that this package may contain potential security issues or vulnerabilities.
Found 1 instance in 1 package
220657
1340
0
Updatedjust-curry-it@^5.3.0