@hyperjump/json-pointer
Advanced tools
Comparing version 0.5.1 to 0.6.0
@@ -47,9 +47,9 @@ const curry = require("just-curry-it"); | ||
const mutate = (pointer, subject = undefined, value = undefined) => { | ||
const assign = (pointer, subject = undefined, value = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = curry((subject, value) => _mutate(ptr, subject, value, nil)); | ||
const fn = curry((subject, value) => _assign(ptr, subject, value, nil)); | ||
return subject === undefined ? fn : fn(subject, value); | ||
}; | ||
const _mutate = (pointer, subject, value, cursor) => { | ||
const _assign = (pointer, subject, value, cursor) => { | ||
if (pointer.length === 0) { | ||
@@ -62,6 +62,51 @@ return; | ||
const segment = pointer.shift(); | ||
_mutate(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
_assign(pointer, applySegment(subject, segment, cursor), value, append(segment, cursor)); | ||
} | ||
}; | ||
const unset = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _unset(ptr, 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 value = applySegment(subject, segment, cursor); | ||
return { ...subject, [segment]: _unset(pointer, value, append(segment, cursor)) }; | ||
} else if (Array.isArray(subject)) { | ||
return subject.filter((_, ndx) => ndx != pointer[0]); | ||
} else if (typeof subject === "object" && subject !== null) { | ||
const { [pointer[0]]: _, ...result } = subject; | ||
return result; | ||
} else { | ||
return applySegment(subject, pointer[0], cursor); | ||
} | ||
}; | ||
const remove = (pointer, subject = undefined) => { | ||
const ptr = compile(pointer); | ||
const fn = (subject) => _remove(ptr, subject, nil); | ||
return subject === undefined ? fn : fn(subject); | ||
}; | ||
const _remove = (pointer, subject, cursor) => { | ||
if (pointer.length === 0) { | ||
return; | ||
} else if (pointer.length > 1) { | ||
const segment = pointer.shift(); | ||
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]]; | ||
} else { | ||
applySegment(subject, pointer[0], cursor); | ||
} | ||
}; | ||
const append = curry((segment, pointer) => pointer + "/" + escape(segment)); | ||
@@ -84,2 +129,2 @@ | ||
module.exports = { nil, get, set, mutate, append }; | ||
module.exports = { nil, append, get, set, assign, unset, delete: remove }; |
{ | ||
"name": "@hyperjump/json-pointer", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "An RFC-6901 JSON Pointer implementation", | ||
@@ -5,0 +5,0 @@ "main": "lib/json-pointer.js", |
@@ -32,7 +32,7 @@ JSON Pointer | ||
// Get value from pointer | ||
// Get a value from a pointer | ||
const getFooBar = JsonPointer.get(fooBarPointer); | ||
getFooBar(value); // 42 | ||
// Set value from pointer | ||
// Set a value from a pointer | ||
// New value is returned without modifying the original | ||
@@ -42,6 +42,16 @@ const setFooBar = JsonPointer.set(fooBarPointer); | ||
// Mutate value from pointer | ||
// Assign a value from a pointer | ||
// The original value is changed and no value is returned | ||
const mutateFooBar = JsonPointer.mutate(fooBarPointer); | ||
mutateFooBar(value, 33); // { "foo": { "bar": 33 } } | ||
const assignFooBar = JsonPointer.assign(fooBarPointer); | ||
assignFooBar(value, 33); // { "foo": { "bar": 33 } } | ||
// Unset a value from a pointer | ||
// New value is returned without modifying the original | ||
const deleteFooBar = JsonPointer.unset(fooBarPointer); | ||
setFooBar(value); // { "foo": {} } | ||
// Delete a value from a pointer | ||
// New value is returned without modifying the original | ||
const deleteFooBar = JsonPointer.delete(fooBarPointer); | ||
setFooBar(value); // { "foo": {} } | ||
``` | ||
@@ -48,0 +58,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
7665
106
72