@hyperjump/json-pointer
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -35,4 +35,3 @@ const curry = require("just-curry-it"); | ||
const segment = pointer.shift(); | ||
subject[segment] = _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
return subject; | ||
return { ...subject, [segment]: _set(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
@@ -49,2 +48,20 @@ const clonedSubject = subject.map((a) => a); | ||
const mutate = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _mutate(ptr, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _mutate = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
return; | ||
} else if (pointer.length === 1 && !isScalar(subject)) { | ||
const segment = pointer[0]; | ||
subject[segment] = value; | ||
} else { | ||
const segment = pointer.shift(); | ||
_mutate(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
}; | ||
const append = curry((segment, pointer) => pointer + "/" + escape(segment)); | ||
@@ -56,3 +73,3 @@ | ||
const applySegment = (value, segment, cursor = "") => { | ||
if (value === null || typeof value !== "object") { | ||
if (isScalar(value)) { | ||
throw Error(`Value at '${cursor}' is a scalar and can't be indexed`); | ||
@@ -66,2 +83,4 @@ } else if (!(segment in value)) { | ||
module.exports = { nil, get, set, append }; | ||
const isScalar = (value) => value === null || typeof value !== "object"; | ||
module.exports = { nil, get, set, mutate, append }; |
{ | ||
"name": "@hyperjump/json-pointer", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "An RFC-6901 JSON Pointer implementation", | ||
@@ -5,0 +5,0 @@ "main": "lib/json-pointer.js", |
@@ -37,4 +37,10 @@ JSON Pointer | ||
// Set value from pointer | ||
// New value is returned without modifying the original | ||
const setFooBar = JsonPointer.set(fooBarPointer); | ||
getFooBar(value, 33); // 33 | ||
setFooBar(value, 33); // { "foo": { "bar": 33 } } | ||
// Mutate value from pointer | ||
// The original value is changed and no value is returned | ||
const mutateFooBar = JsonPointer.mutate(fooBarPointer); | ||
mutateFooBar(value, 33); // { "foo": { "bar": 33 } } | ||
``` | ||
@@ -41,0 +47,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5763
65
62