immutable-json-patch
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -6,21 +6,21 @@ "use strict"; | ||
}); | ||
exports.shallowClone = shallowClone; | ||
exports.applyProp = applyProp; | ||
exports.deleteIn = deleteIn; | ||
exports.existsIn = existsIn; | ||
exports.getIn = getIn; | ||
exports.insertAt = insertAt; | ||
exports.setIn = setIn; | ||
exports.shallowClone = shallowClone; | ||
exports.transform = transform; | ||
exports.updateIn = updateIn; | ||
exports.deleteIn = deleteIn; | ||
exports.insertAt = insertAt; | ||
exports.transform = transform; | ||
exports.existsIn = existsIn; | ||
var _utils = require("./utils.js"); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
@@ -27,0 +27,0 @@ /** |
@@ -6,13 +6,13 @@ "use strict"; | ||
}); | ||
exports.immutableJSONPatch = immutableJSONPatch; | ||
exports.replace = replace; | ||
exports.remove = remove; | ||
exports.add = add; | ||
exports.copy = copy; | ||
exports.immutableJSONPatch = immutableJSONPatch; | ||
exports.isArrayItem = isArrayItem; | ||
exports.move = move; | ||
exports.preprocessJSONPatchOperation = preprocessJSONPatchOperation; | ||
exports.remove = remove; | ||
exports.replace = replace; | ||
exports.resolvePathIndex = resolvePathIndex; | ||
exports.test = test; | ||
exports.isArrayItem = isArrayItem; | ||
exports.resolvePathIndex = resolvePathIndex; | ||
exports.validateJSONPatchOperation = validateJSONPatchOperation; | ||
exports.preprocessJSONPatchOperation = preprocessJSONPatchOperation; | ||
@@ -19,0 +19,0 @@ var _immutabilityHelpers = require("./immutabilityHelpers.js"); |
@@ -6,42 +6,42 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "immutableJSONPatch", { | ||
Object.defineProperty(exports, "appendToJSONPointer", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutableJSONPatch.immutableJSONPatch; | ||
return _jsonPointer.appendToJSONPointer; | ||
} | ||
}); | ||
Object.defineProperty(exports, "revertJSONPatch", { | ||
Object.defineProperty(exports, "compileJSONPointer", { | ||
enumerable: true, | ||
get: function get() { | ||
return _revertJSONPatch.revertJSONPatch; | ||
return _jsonPointer.compileJSONPointer; | ||
} | ||
}); | ||
Object.defineProperty(exports, "compileJSONPointer", { | ||
Object.defineProperty(exports, "compileJSONPointerProp", { | ||
enumerable: true, | ||
get: function get() { | ||
return _jsonPointer.compileJSONPointer; | ||
return _jsonPointer.compileJSONPointerProp; | ||
} | ||
}); | ||
Object.defineProperty(exports, "parseJSONPointer", { | ||
Object.defineProperty(exports, "deleteIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _jsonPointer.parseJSONPointer; | ||
return _immutabilityHelpers.deleteIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getIn", { | ||
Object.defineProperty(exports, "existsIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutabilityHelpers.getIn; | ||
return _immutabilityHelpers.existsIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setIn", { | ||
Object.defineProperty(exports, "getIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutabilityHelpers.setIn; | ||
return _immutabilityHelpers.getIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "existsIn", { | ||
Object.defineProperty(exports, "immutableJSONPatch", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutabilityHelpers.existsIn; | ||
return _immutableJSONPatch.immutableJSONPatch; | ||
} | ||
@@ -55,8 +55,32 @@ }); | ||
}); | ||
Object.defineProperty(exports, "deleteIn", { | ||
Object.defineProperty(exports, "parseJSONPointer", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutabilityHelpers.deleteIn; | ||
return _jsonPointer.parseJSONPointer; | ||
} | ||
}); | ||
Object.defineProperty(exports, "parseJSONPointerWithArrayIndices", { | ||
enumerable: true, | ||
get: function get() { | ||
return _jsonPointer.parseJSONPointerWithArrayIndices; | ||
} | ||
}); | ||
Object.defineProperty(exports, "revertJSONPatch", { | ||
enumerable: true, | ||
get: function get() { | ||
return _revertJSONPatch.revertJSONPatch; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _immutabilityHelpers.setIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "startsWithJSONPointer", { | ||
enumerable: true, | ||
get: function get() { | ||
return _jsonPointer.startsWithJSONPointer; | ||
} | ||
}); | ||
Object.defineProperty(exports, "updateIn", { | ||
@@ -63,0 +87,0 @@ enumerable: true, |
@@ -6,9 +6,15 @@ "use strict"; | ||
}); | ||
exports.appendToJSONPointer = appendToJSONPointer; | ||
exports.compileJSONPointer = compileJSONPointer; | ||
exports.compileJSONPointerProp = compileJSONPointerProp; | ||
exports.parseJSONPointer = parseJSONPointer; | ||
exports.compileJSONPointer = compileJSONPointer; | ||
exports.parseJSONPointerWithArrayIndices = parseJSONPointerWithArrayIndices; | ||
exports.startsWithJSONPointer = startsWithJSONPointer; | ||
var _immutabilityHelpers = require("./immutabilityHelpers.js"); | ||
/** | ||
* Parse a JSON Pointer | ||
* @param {string} pointer | ||
* @return {JSONPath} | ||
* @param {JSONPointer} pointer | ||
* @return {string[]} | ||
*/ | ||
@@ -24,13 +30,74 @@ function parseJSONPointer(pointer) { | ||
/** | ||
* Parse a JSONPointer, and turn array indices into numeric values. | ||
* For example, '/array/2/name' returns ['array', 2, 'name'] when array turns | ||
* out to be an actual Array | ||
* @param {JSONData} json | ||
* @param {JSONPointer} pointer | ||
* @return {JSONPath} | ||
*/ | ||
function parseJSONPointerWithArrayIndices(json, pointer) { | ||
var path = parseJSONPointer(pointer); // parse Array indexes into a number | ||
for (var i = 0; i < path.length; i++) { | ||
var section = path[i]; | ||
if (ARRAY_INDEX_REGEX.exec(section)) { | ||
// this path part contains a number. | ||
// See if the document actually contains an array | ||
var parentPath = path.slice(0, i); | ||
var parent = (0, _immutabilityHelpers.getIn)(json, parentPath); | ||
if (Array.isArray(parent)) { | ||
path[i] = parseInt(section, 10); | ||
} | ||
} | ||
} | ||
return path; | ||
} // test whether a string only contains one or digits, like "1" or "204" | ||
var ARRAY_INDEX_REGEX = /^\d+$/; | ||
/** | ||
* Compile a JSON Pointer | ||
* @param {JSONPath} path | ||
* @return {string} | ||
* @return {JSONPointer} | ||
*/ | ||
function compileJSONPointer(path) { | ||
return path.map(function (p) { | ||
return '/' + String(p).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
}).join(''); | ||
return path.map(compileJSONPointerProp).join(''); | ||
} | ||
/** | ||
* Compile a single path property from a JSONPath | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
function compileJSONPointerProp(pathProp) { | ||
return '/' + String(pathProp).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
} | ||
/** | ||
* Append a path property to a JSONPointer | ||
* @param {JSONPointer} pointer | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
function appendToJSONPointer(pointer, pathProp) { | ||
return pointer + compileJSONPointerProp(pathProp); | ||
} | ||
/** | ||
* @param {JSONPointer} pointer | ||
* @param {JSONPointer} searchPointer | ||
* @returns {boolean} | ||
*/ | ||
function startsWithJSONPointer(pointer, searchPointer) { | ||
return pointer.startsWith(searchPointer) && (pointer.length === searchPointer.length || pointer[searchPointer.length] === '/'); | ||
} | ||
//# sourceMappingURL=jsonPointer.js.map |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -6,10 +6,10 @@ "use strict"; | ||
}); | ||
exports.initial = initial; | ||
exports.isEqual = isEqual; | ||
exports.strictEqual = strictEqual; | ||
exports.initial = initial; | ||
exports.isObjectOrArray = isObjectOrArray; | ||
exports.last = last; | ||
exports.startsWith = startsWith; | ||
exports.isObjectOrArray = isObjectOrArray; | ||
exports.strictEqual = strictEqual; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
@@ -16,0 +16,0 @@ /** |
@@ -1,8 +0,8 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
@@ -9,0 +9,0 @@ /** |
export { immutableJSONPatch } from './immutableJSONPatch.js'; | ||
export { revertJSONPatch } from './revertJSONPatch.js'; // utils | ||
export { compileJSONPointer, parseJSONPointer } from './jsonPointer.js'; | ||
export { compileJSONPointer, compileJSONPointerProp, parseJSONPointer, parseJSONPointerWithArrayIndices, startsWithJSONPointer, appendToJSONPointer } from './jsonPointer.js'; | ||
export { getIn, setIn, existsIn, insertAt, deleteIn, updateIn } from './immutabilityHelpers.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -0,6 +1,8 @@ | ||
import { getIn } from './immutabilityHelpers.js'; | ||
/** | ||
* Parse a JSON Pointer | ||
* @param {string} pointer | ||
* @return {JSONPath} | ||
* @param {JSONPointer} pointer | ||
* @return {string[]} | ||
*/ | ||
export function parseJSONPointer(pointer) { | ||
@@ -15,12 +17,69 @@ var path = pointer.split('/'); | ||
/** | ||
* Parse a JSONPointer, and turn array indices into numeric values. | ||
* For example, '/array/2/name' returns ['array', 2, 'name'] when array turns | ||
* out to be an actual Array | ||
* @param {JSONData} json | ||
* @param {JSONPointer} pointer | ||
* @return {JSONPath} | ||
*/ | ||
export function parseJSONPointerWithArrayIndices(json, pointer) { | ||
var path = parseJSONPointer(pointer); // parse Array indexes into a number | ||
for (var i = 0; i < path.length; i++) { | ||
var section = path[i]; | ||
if (ARRAY_INDEX_REGEX.exec(section)) { | ||
// this path part contains a number. | ||
// See if the document actually contains an array | ||
var parentPath = path.slice(0, i); | ||
var parent = getIn(json, parentPath); | ||
if (Array.isArray(parent)) { | ||
path[i] = parseInt(section, 10); | ||
} | ||
} | ||
} | ||
return path; | ||
} // test whether a string only contains one or digits, like "1" or "204" | ||
var ARRAY_INDEX_REGEX = /^\d+$/; | ||
/** | ||
* Compile a JSON Pointer | ||
* @param {JSONPath} path | ||
* @return {string} | ||
* @return {JSONPointer} | ||
*/ | ||
export function compileJSONPointer(path) { | ||
return path.map(function (p) { | ||
return '/' + String(p).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
}).join(''); | ||
return path.map(compileJSONPointerProp).join(''); | ||
} | ||
/** | ||
* Compile a single path property from a JSONPath | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
export function compileJSONPointerProp(pathProp) { | ||
return '/' + String(pathProp).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
} | ||
/** | ||
* Append a path property to a JSONPointer | ||
* @param {JSONPointer} pointer | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
export function appendToJSONPointer(pointer, pathProp) { | ||
return pointer + compileJSONPointerProp(pathProp); | ||
} | ||
/** | ||
* @param {JSONPointer} pointer | ||
* @param {JSONPointer} searchPointer | ||
* @returns {boolean} | ||
*/ | ||
export function startsWithJSONPointer(pointer, searchPointer) { | ||
return pointer.startsWith(searchPointer) && (pointer.length === searchPointer.length || pointer[searchPointer.length] === '/'); | ||
} | ||
//# sourceMappingURL=jsonPointer.js.map |
@@ -0,0 +0,0 @@ import { existsIn, getIn } from './immutabilityHelpers.js'; |
@@ -1,2 +0,2 @@ | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
@@ -3,0 +3,0 @@ /** |
@@ -5,5 +5,5 @@ (function (global, factory) { | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.immutableJSONPatch = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
})(this, (function (exports) { 'use strict'; | ||
function _typeof$1(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof$1 = function _typeof(obj) { return typeof obj; }; } else { _typeof$1 = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof$1(obj); } | ||
function _typeof$1(obj) { "@babel/helpers - typeof"; return _typeof$1 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof$1(obj); } | ||
@@ -85,9 +85,9 @@ /** | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
/** | ||
@@ -316,5 +316,6 @@ * Shallow clone of an Object, Array, or value | ||
* Parse a JSON Pointer | ||
* @param {string} pointer | ||
* @return {JSONPath} | ||
* @param {JSONPointer} pointer | ||
* @return {string[]} | ||
*/ | ||
function parseJSONPointer(pointer) { | ||
@@ -329,14 +330,71 @@ var path = pointer.split('/'); | ||
/** | ||
* Parse a JSONPointer, and turn array indices into numeric values. | ||
* For example, '/array/2/name' returns ['array', 2, 'name'] when array turns | ||
* out to be an actual Array | ||
* @param {JSONData} json | ||
* @param {JSONPointer} pointer | ||
* @return {JSONPath} | ||
*/ | ||
function parseJSONPointerWithArrayIndices(json, pointer) { | ||
var path = parseJSONPointer(pointer); // parse Array indexes into a number | ||
for (var i = 0; i < path.length; i++) { | ||
var section = path[i]; | ||
if (ARRAY_INDEX_REGEX.exec(section)) { | ||
// this path part contains a number. | ||
// See if the document actually contains an array | ||
var parentPath = path.slice(0, i); | ||
var parent = getIn(json, parentPath); | ||
if (Array.isArray(parent)) { | ||
path[i] = parseInt(section, 10); | ||
} | ||
} | ||
} | ||
return path; | ||
} // test whether a string only contains one or digits, like "1" or "204" | ||
var ARRAY_INDEX_REGEX = /^\d+$/; | ||
/** | ||
* Compile a JSON Pointer | ||
* @param {JSONPath} path | ||
* @return {string} | ||
* @return {JSONPointer} | ||
*/ | ||
function compileJSONPointer(path) { | ||
return path.map(function (p) { | ||
return '/' + String(p).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
}).join(''); | ||
return path.map(compileJSONPointerProp).join(''); | ||
} | ||
/** | ||
* Compile a single path property from a JSONPath | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
function compileJSONPointerProp(pathProp) { | ||
return '/' + String(pathProp).replace(/~/g, '~0').replace(/\//g, '~1'); | ||
} | ||
/** | ||
* Append a path property to a JSONPointer | ||
* @param {JSONPointer} pointer | ||
* @param {string | number} pathProp | ||
* @returns {JSONPointer} | ||
*/ | ||
function appendToJSONPointer(pointer, pathProp) { | ||
return pointer + compileJSONPointerProp(pathProp); | ||
} | ||
/** | ||
* @param {JSONPointer} pointer | ||
* @param {JSONPointer} searchPointer | ||
* @returns {boolean} | ||
*/ | ||
function startsWithJSONPointer(pointer, searchPointer) { | ||
return pointer.startsWith(searchPointer) && (pointer.length === searchPointer.length || pointer[searchPointer.length] === '/'); | ||
} | ||
/** | ||
* Apply a patch to a JSON object | ||
@@ -700,3 +758,5 @@ * The original JSON object will not be changed, | ||
exports.appendToJSONPointer = appendToJSONPointer; | ||
exports.compileJSONPointer = compileJSONPointer; | ||
exports.compileJSONPointerProp = compileJSONPointerProp; | ||
exports.deleteIn = deleteIn; | ||
@@ -708,4 +768,6 @@ exports.existsIn = existsIn; | ||
exports.parseJSONPointer = parseJSONPointer; | ||
exports.parseJSONPointerWithArrayIndices = parseJSONPointerWithArrayIndices; | ||
exports.revertJSONPatch = revertJSONPatch; | ||
exports.setIn = setIn; | ||
exports.startsWithJSONPointer = startsWithJSONPointer; | ||
exports.updateIn = updateIn; | ||
@@ -715,3 +777,3 @@ | ||
}))); | ||
})); | ||
//# sourceMappingURL=immutableJSONPatch.js.map |
@@ -1,1 +0,1 @@ | ||
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r="undefined"!=typeof globalThis?globalThis:r||self).immutableJSONPatch={})}(this,function(r){"use strict";function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function i(r,t){return r===t}function a(r){return r.slice(0,r.length-1)}function u(r){return"object"===t(r)&&null!==r}function e(t,r){var e,n=Object.keys(t);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(t),r&&(e=e.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),n.push.apply(n,e)),n}function o(n){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach(function(r){var t,e;t=n,r=o[e=r],e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach(function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(o,r))})}return n}function f(r){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function c(t){if(Array.isArray(t)){var e=t.slice();return Object.getOwnPropertySymbols(t).forEach(function(r){e[r]=t[r]}),e}if("object"!==f(t))return t;var n=o({},t);return Object.getOwnPropertySymbols(t).forEach(function(r){n[r]=t[r]}),n}function p(r,t,e){if(r[t]===e)return r;r=c(r);return r[t]=e,r}function l(r,t){for(var e=r,n=0;n<t.length;)e=u(e)?e[t[n]]:void 0,n++;return e}function s(r,t,e){var n=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(0===t.length)return e;var o=t[0],e=s(r?r[o]:void 0,t.slice(1),e,n);if(u(r))return p(r,o,e);if(n){n="number"==typeof o?[]:{};return n[o]=e,n}throw new Error("Path does not exist")}function h(r,t,e){if(0===t.length)return e(r);if(!u(r))throw new Error("Path doesn't exist");var n=t[0];return p(r,n,h(r[n],t.slice(1),e))}function y(r,t){if(0===t.length)return r;if(!u(r))throw new Error("Path does not exist");if(1===t.length){var e=t[0];if(e in r){var n=c(r);return Array.isArray(n)?n.splice(e,1):delete n[e],n}return r}n=t[0];return p(r,n,y(r[n],t.slice(1)))}function v(r,t,e){var n=t.slice(0,t.length-1),o=t[t.length-1];return h(r,n,function(r){if(!Array.isArray(r))throw new TypeError("Array expected at path "+JSON.stringify(n));r=c(r);return r.splice(o,0,e),r})}function g(r,t){return void 0!==r&&(0===t.length||g(r[t[0]],t.slice(1)))}function d(r){r=r.split("/");return r.shift(),r.map(function(r){return r.replace(/~1/g,"/").replace(/~0/g,"~")})}function m(r){return r.map(function(r){return"/"+String(r).replace(/~/g,"~0").replace(/\//g,"~1")}).join("")}function b(r,t,e){for(var n=r,o=0;o<t.length;o++){!function(r){if(!["add","remove","replace","copy","move","test"].includes(r.op))throw new Error("Unknown JSONPatch op "+JSON.stringify(r.op));if("string"!=typeof r.path)throw new Error('Required property "path" missing or not a string in operation '+JSON.stringify(r));if(("copy"===r.op||"move"===r.op)&&"string"!=typeof r.from)throw new Error('Required property "from" missing or not a string in operation '+JSON.stringify(r))}(t[o]);var i=(i=n,{op:(u=t[o]).op,path:function(r,t){if("-"!==function(r){return r[r.length-1]}(t))return t;t=a(t),r=l(r,t);return t.concat(r.length)}(i,d(u.path)),from:void 0!==u.from?d(u.from):null,value:u.value});e&&e.before&&(void 0!==(f=e.before(n,i))&&(void 0!==f.json&&(n=f.json),void 0!==f.operation&&(i=f.operation)));var u=n,f=O[i.op];if(f)n=f(n,i);else{if("test"!==i.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(i.op));!function(r,t){var e=t.path,t=t.value;if(void 0===t)throw new Error('Test failed: no value provided (path: "'.concat(m(e),'")'));if(!g(r,e))throw new Error('Test failed: path not found (path: "'.concat(m(e),'")'));if(!function(r,t){return JSON.stringify(r)===JSON.stringify(t)}(l(r,e),t))throw new Error('Test failed, value differs (path: "'.concat(m(e),'")'))}(n,i)}e&&e.after&&(void 0!==(u=e.after(n,i,u))&&(n=u))}return n}var O={add:function(r,t){var e=t.path,t=t.value;return(w(r,e)?v:s)(r,e,t)},remove:function(r,t){t=t.path;return y(r,t)},replace:function(r,t){var e=t.path,t=t.value;return s(r,e,t)},copy:function(r,t){var e=t.path,n=t.from,t=l(r,n);{if(w(r,e))return v(r,e,t);n=l(r,n);return s(r,e,n)}},move:function(r,t){var e=t.path,n=t.from,t=l(r,n),n=y(r,n);return(w(n,e)?v:s)(n,e,t)}};function w(r,t){if(0!==t.length){t=l(r,a(t));return Array.isArray(t)}}var S={add:j,remove:P,replace:n,copy:function(r,t){var e=t.path,t=t.value;return j(r,{path:e,value:t})},move:function(r,t){var e=t.path,t=t.from;if(e.length<t.length&&function(r,t,e){var n=2<arguments.length&&void 0!==e?e:i;if(!(r.length<t.length)){for(var o=0;o<t.length;o++)if(!n(r[o],t[o]))return;return 1}}(t,e))return[{op:"replace",path:m(e),value:r}];t=[{op:"move",from:m(e),path:m(t)}];!w(r,e)&&g(r,e)&&(t=t.concat(P(r,{path:e})));return t}};function n(r,t){t=t.path;return[{op:"replace",path:m(t),value:l(r,t)}]}function P(r,t){t=t.path;return[{op:"add",path:m(t),value:l(r,t)}]}function j(r,t){var e=t.path,t=t.value;return w(r,e)||!g(r,e)?[{op:"remove",path:m(e)}]:n(r,{path:e,value:t})}r.compileJSONPointer=m,r.deleteIn=y,r.existsIn=g,r.getIn=l,r.immutableJSONPatch=b,r.insertAt=v,r.parseJSONPointer=d,r.revertJSONPatch=function(r,t){var n=[];return b(r,t,{before:function(r,t){var e=S[t.op];e&&(n=e(r,t).concat(n))}}),n},r.setIn=s,r.updateIn=h,Object.defineProperty(r,"__esModule",{value:!0})}); | ||
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r="undefined"!=typeof globalThis?globalThis:r||self).immutableJSONPatch={})}(this,function(r){"use strict";function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function i(r,t){return r===t}function p(r){return r.slice(0,r.length-1)}function a(r){return"object"===t(r)&&null!==r}function e(t,r){var e,n=Object.keys(t);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(t),r&&(e=e.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),n.push.apply(n,e)),n}function o(n){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach(function(r){var t,e;t=n,e=o[r=r],r in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach(function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(o,r))})}return n}function u(r){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function f(t){var e,n;return Array.isArray(t)?(e=t.slice(),Object.getOwnPropertySymbols(t).forEach(function(r){e[r]=t[r]}),e):"object"===u(t)?(n=o({},t),Object.getOwnPropertySymbols(t).forEach(function(r){n[r]=t[r]}),n):t}function c(r,t,e){return r[t]===e?r:((r=f(r))[t]=e,r)}function l(r,t){for(var e=r,n=0;n<t.length;)e=a(e)?e[t[n]]:void 0,n++;return e}function s(r,t,e){var n=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(0===t.length)return e;var o=t[0],t=s(r?r[o]:void 0,t.slice(1),e,n);if(a(r))return c(r,o,t);if(n)return(e="number"==typeof o?[]:{})[o]=t,e;throw new Error("Path does not exist")}function h(r,t,e){if(0===t.length)return e(r);if(!a(r))throw new Error("Path doesn't exist");var n=t[0];return c(r,n,h(r[n],t.slice(1),e))}function y(r,t){if(0===t.length)return r;if(!a(r))throw new Error("Path does not exist");var e;if(1===t.length)return(n=t[0])in r?(e=f(r),Array.isArray(e)?e.splice(n,1):delete e[n],e):r;var n=t[0];return c(r,n,y(r[n],t.slice(1)))}function v(r,t,e){var n=t.slice(0,t.length-1),o=t[t.length-1];return h(r,n,function(r){if(!Array.isArray(r))throw new TypeError("Array expected at path "+JSON.stringify(n));r=f(r);return r.splice(o,0,e),r})}function d(r,t){return void 0!==r&&(0===t.length||d(r[t[0]],t.slice(1)))}function g(r){r=r.split("/");return r.shift(),r.map(function(r){return r.replace(/~1/g,"/").replace(/~0/g,"~")})}var m=/^\d+$/;function b(r){return r.map(n).join("")}function n(r){return"/"+String(r).replace(/~/g,"~0").replace(/\//g,"~1")}function O(r,t,e){for(var n=r,o=0;o<t.length;o++){i=void 0;var i=t[o];if(!["add","remove","replace","copy","move","test"].includes(i.op))throw new Error("Unknown JSONPatch op "+JSON.stringify(i.op));if("string"!=typeof i.path)throw new Error('Required property "path" missing or not a string in operation '+JSON.stringify(i));if(("copy"===i.op||"move"===i.op)&&"string"!=typeof i.from)throw new Error('Required property "from" missing or not a string in operation '+JSON.stringify(i));i=n;var i={op:(a=t[o]).op,path:function(r,t){if("-"!==function(r){return r[r.length-1]}(t))return t;t=p(t),r=l(r,t);return t.concat(r.length)}(i,g(a.path)),from:void 0!==a.from?g(a.from):null,value:a.value},a=(e&&e.before&&(void 0!==(a=e.before(n,i))&&(void 0!==a.json&&(n=a.json),void 0!==a.operation&&(i=a.operation))),n),u=w[i.op];if(u)n=u(n,i);else{if("test"!==i.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(i.op));f=c=f=u=void 0;var u=n,f=i,c=f.path;if(void 0===(f=f.value))throw new Error('Test failed: no value provided (path: "'.concat(b(c),'")'));if(!d(u,c))throw new Error('Test failed: path not found (path: "'.concat(b(c),'")'));if(!function(r,t){return JSON.stringify(r)===JSON.stringify(t)}(l(u,c),f))throw new Error('Test failed, value differs (path: "'.concat(b(c),'")'))}e&&e.after&&(void 0!==(u=e.after(n,i,a))&&(n=u))}return n}var w={add:function(r,t){var e=t.path,t=t.value;return(S(r,e)?v:s)(r,e,t)},remove:function(r,t){t=t.path;return y(r,t)},replace:function(r,t){var e=t.path,t=t.value;return s(r,e,t)},copy:function(r,t){var e=t.path,t=t.from,n=l(r,t);return S(r,e)?v(r,e,n):(n=l(r,t),s(r,e,n))},move:function(r,t){var e=t.path,t=t.from,n=l(r,t),r=y(r,t);return(S(r,e)?v:s)(r,e,n)}};function S(r,t){if(0!==t.length)return r=l(r,p(t)),Array.isArray(r)}var P={add:N,remove:J,replace:j,copy:function(r,t){var e=t.path,t=t.value;return N(r,{path:e,value:t})},move:function(r,t){var e=t.path,t=t.from;if(e.length<t.length&&function(r,t,e){var n=2<arguments.length&&void 0!==e?e:i;if(!(r.length<t.length)){for(var o=0;o<t.length;o++)if(!n(r[o],t[o]))return;return 1}}(t,e))return[{op:"replace",path:b(e),value:r}];t=[{op:"move",from:b(e),path:b(t)}];!S(r,e)&&d(r,e)&&(t=t.concat(J(r,{path:e})));return t}};function j(r,t){t=t.path;return[{op:"replace",path:b(t),value:l(r,t)}]}function J(r,t){t=t.path;return[{op:"add",path:b(t),value:l(r,t)}]}function N(r,t){var e=t.path,t=t.value;return S(r,e)||!d(r,e)?[{op:"remove",path:b(e)}]:j(r,{path:e,value:t})}r.appendToJSONPointer=function(r,t){return r+n(t)},r.compileJSONPointer=b,r.compileJSONPointerProp=n,r.deleteIn=y,r.existsIn=d,r.getIn=l,r.immutableJSONPatch=O,r.insertAt=v,r.parseJSONPointer=g,r.parseJSONPointerWithArrayIndices=function(r,t){for(var e=g(t),n=0;n<e.length;n++){var o,i=e[n];m.exec(i)&&(o=l(r,e.slice(0,n)),Array.isArray(o)&&(e[n]=parseInt(i,10)))}return e},r.revertJSONPatch=function(r,t){var n=[];return O(r,t,{before:function(r,t){var e=P[t.op];e&&(n=e(r,t).concat(n))}}),n},r.setIn=s,r.startsWithJSONPointer=function(r,t){return r.startsWith(t)&&(r.length===t.length||"/"===r[t.length])},r.updateIn=h,Object.defineProperty(r,"__esModule",{value:!0})}); |
{ | ||
"name": "immutable-json-patch", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Immutable JSON patch with support for reverting operations", | ||
@@ -28,4 +28,4 @@ "repository": { | ||
"build:esm": "babel src --out-dir lib/esm --source-maps --config-file ./babel.config.json", | ||
"build:cjs": "babel src --out-dir lib/cjs --source-maps --config-file ./babel-cjs.config.json && cpy tools/cjs lib/cjs", | ||
"build:umd": "rollup lib/esm/index.js --format umd --name 'immutableJSONPatch' --sourcemap --output.file lib/umd/immutableJSONPatch.js && cpy tools/cjs/package.json lib/umd", | ||
"build:cjs": "babel src --out-dir lib/cjs --source-maps --config-file ./babel-cjs.config.json && cpy tools/cjs/package.json lib/cjs --flat", | ||
"build:umd": "rollup lib/esm/index.js --format umd --name 'immutableJSONPatch' --sourcemap --output.file lib/umd/immutableJSONPatch.js && cpy tools/cjs/package.json lib/umd --flat", | ||
"build:umd:min": "uglifyjs --compress --mangle --source-map --comments --output lib/umd/immutableJSONPatch.min.js -- lib/umd/immutableJSONPatch.js", | ||
@@ -45,17 +45,17 @@ "link": "npm run build", | ||
"devDependencies": { | ||
"@babel/cli": "7.14.8", | ||
"@babel/core": "7.14.8", | ||
"@babel/preset-env": "7.14.8", | ||
"cpy-cli": "3.1.1", | ||
"@babel/cli": "7.17.10", | ||
"@babel/core": "7.18.5", | ||
"@babel/preset-env": "7.18.2", | ||
"cpy-cli": "4.1.0", | ||
"del-cli": "4.0.1", | ||
"eslint": "7.31.0", | ||
"eslint-config-standard": "16.0.3", | ||
"eslint-plugin-import": "2.23.4", | ||
"eslint": "8.18.0", | ||
"eslint-config-standard": "17.0.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"eslint-plugin-n": "15.2.3", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-promise": "5.1.0", | ||
"mocha": "9.0.2", | ||
"rollup": "2.53.3", | ||
"uglify-js": "3.14.0" | ||
}, | ||
"dependencies": {} | ||
"eslint-plugin-promise": "6.0.0", | ||
"mocha": "10.0.0", | ||
"rollup": "2.75.7", | ||
"uglify-js": "3.16.1" | ||
} | ||
} |
@@ -135,8 +135,11 @@ # immutable-json-patch | ||
```ts | ||
declare function parseJSONPointer (pointer: string) : JSONPath | ||
declare function compileJSONPointer (path: JSONPath) : string | ||
declare function parseJSONPointer (pointer: JSONPointer) : JSONPath | ||
declare function compileJSONPointer (path: JSONPath) : JSONPointer | ||
declare function compileJSONPointerProp (pathProp: string | number) : JSONPointer | ||
declare function appendToJSONPointer (pointer: JSONPointer, pathProp: string | number) : JSONPointer | ||
declare function startsWithJSONPointer (pointer: JSONPointer, searchPointer: JSONPointer) : boolean | ||
declare function getIn(json: JSONData, path: JSONPath) : JSONData | ||
declare function setIn(json: JSONData, path: JSONPath, value: JSONData, createPath?: boolean) : JSONData | ||
declare function updateIn(json: JSONData, path: JSONPath, callback: (json: JSONData) => JSONData) | ||
declare function updateIn(json: JSONData, path: JSONPath, callback: (json: JSONData) => JSONData) : JSONData | ||
declare function deleteIn(json: JSONData, path: JSONPath) : JSONData | ||
@@ -143,0 +146,0 @@ declare function existsIn(json: JSONData, path: JSONPath) : boolean |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
224348
2327
180
14