immutable-json-patch
Version:
Immutable JSON patch with support for reverting operations
48 lines (42 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.appendToJSONPointer = appendToJSONPointer;
exports.compileJSONPointer = compileJSONPointer;
exports.compileJSONPointerProp = compileJSONPointerProp;
exports.parseJSONPointer = parseJSONPointer;
exports.startsWithJSONPointer = startsWithJSONPointer;
/**
* Parse a JSON Pointer
*/
function parseJSONPointer(pointer) {
const path = pointer.split('/');
path.shift(); // remove the first empty entry
return path.map(p => p.replace(/~1/g, '/').replace(/~0/g, '~'));
}
/**
* Compile a JSON Pointer
*/
function compileJSONPointer(path) {
return path.map(compileJSONPointerProp).join('');
}
/**
* Compile a single path property from a JSONPath
*/
function compileJSONPointerProp(pathProp) {
return `/${String(pathProp).replace(/~/g, '~0').replace(/\//g, '~1')}`;
}
/**
* Append a path property to a JSONPointer
*/
function appendToJSONPointer(pointer, pathProp) {
return pointer + compileJSONPointerProp(pathProp);
}
/**
* Test whether `pointer` starts with `searchPointer`
*/
function startsWithJSONPointer(pointer, searchPointer) {
return pointer.startsWith(searchPointer) && (pointer.length === searchPointer.length || pointer[searchPointer.length] === '/');
}
//# sourceMappingURL=jsonPointer.js.map