Comparing version 5.0.1 to 5.1.0
@@ -53,5 +53,5 @@ import { Pointer } from './pointer'; | ||
} | ||
export declare type Operation = AddOperation | RemoveOperation | ReplaceOperation | MoveOperation | CopyOperation | TestOperation; | ||
export type Operation = AddOperation | RemoveOperation | ReplaceOperation | MoveOperation | CopyOperation | TestOperation; | ||
export declare function isDestructive({ op }: Operation): boolean; | ||
export declare type Diff = (input: any, output: any, ptr: Pointer) => Operation[]; | ||
export type Diff = (input: any, output: any, ptr: Pointer) => Operation[]; | ||
/** | ||
@@ -62,3 +62,3 @@ VoidableDiff exists to allow the user to provide a partial diff(...) function, | ||
*/ | ||
export declare type VoidableDiff = (input: any, output: any, ptr: Pointer) => Operation[] | void; | ||
export type VoidableDiff = (input: any, output: any, ptr: Pointer) => Operation[] | void; | ||
/** | ||
@@ -65,0 +65,0 @@ List the keys in `minuend` that are not in `subtrahend`. |
@@ -131,3 +131,3 @@ "use strict"; | ||
// memoized | ||
var memo_key = i + "," + j; | ||
var memo_key = "".concat(i, ",").concat(j); | ||
var memoized = memo[memo_key]; | ||
@@ -269,4 +269,4 @@ if (memoized === undefined) { | ||
} | ||
var input_type = util_1.objectType(input); | ||
var output_type = util_1.objectType(output); | ||
var input_type = (0, util_1.objectType)(input); | ||
var output_type = (0, util_1.objectType)(output); | ||
if (input_type == 'array' && output_type == 'array') { | ||
@@ -273,0 +273,0 @@ return diffArrays(input, output, ptr, diff); |
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.rfc6902 = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
})(this, (function (exports) { 'use strict'; | ||
@@ -86,10 +86,6 @@ /** | ||
set(object, value) { | ||
let cursor = object; | ||
for (let i = 1, l = this.tokens.length - 1, token = this.tokens[i]; i < l; i++) { | ||
// not sure if this the best way to handle non-existant paths... | ||
cursor = (cursor || {})[token]; | ||
const endpoint = this.evaluate(object); | ||
if (endpoint.parent) { | ||
endpoint.parent[endpoint.key] = value; | ||
} | ||
if (cursor) { | ||
cursor[this.tokens[this.tokens.length - 1]] = value; | ||
} | ||
} | ||
@@ -714,2 +710,3 @@ push(token) { | ||
exports.Pointer = Pointer; | ||
exports.applyPatch = applyPatch; | ||
@@ -721,2 +718,2 @@ exports.createPatch = createPatch; | ||
}))); | ||
})); |
@@ -1,2 +0,2 @@ | ||
(function(p,r){"object"===typeof exports&&"undefined"!==typeof module?r(exports):"function"===typeof define&&define.amd?define(["exports"],r):(p="undefined"!==typeof globalThis?globalThis:p||self,r(p.rfc6902={}))})(this,function(p){function r(a){return a.replace(/~1/g,"/").replace(/~0/g,"~")}function E(a){return a.replace(/~/g,"~0").replace(/\//g,"~1")}function z(a){return void 0===a?"undefined":null===a?"null":Array.isArray(a)?"array":typeof a}function v(a){if(null==a||"object"!=typeof a)return a; | ||
(function(n,r){"object"===typeof exports&&"undefined"!==typeof module?r(exports):"function"===typeof define&&define.amd?define(["exports"],r):(n="undefined"!==typeof globalThis?globalThis:n||self,r(n.rfc6902={}))})(this,function(n){function r(a){return a.replace(/~1/g,"/").replace(/~0/g,"~")}function E(a){return a.replace(/~/g,"~0").replace(/\//g,"~1")}function z(a){return void 0===a?"undefined":null===a?"null":Array.isArray(a)?"array":typeof a}function v(a){if(null==a||"object"!=typeof a)return a; | ||
if(a.constructor==Array){var b=a.length,c=Array(b);for(let d=0;d<b;d++)c[d]=v(a[d]);return c}if(a.constructor==Date)return new Date(+a);b={};for(c in a)w.call(a,c)&&(b[c]=v(a[c]));return b}function F({op:a}){return"remove"===a||"replace"===a||"copy"===a||"move"===a}function A(a,b){const c={};for(const d in a)w.call(a,d)&&void 0!==a[d]&&(c[d]=1);for(const d in b)w.call(b,d)&&void 0!==b[d]&&delete c[d];return Object.keys(c)}function G(a){const b=a.length,c={};for(let d=0;d<b;d++){const e=a[d];for(const f in e)w.call(e, | ||
@@ -6,7 +6,7 @@ f)&&void 0!==e[f]&&(c[f]=(c[f]||0)+1)}for(const d in c)c[d]<b&&delete c[d];return Object.keys(c)}function x(a,b){return{operations:a.operations.concat(b),cost:a.cost+1}}function H(a,b,c,d=q){function e(k,g){const h=`${k},${g}`;var l=f[h];if(void 0===l){if(0<k&&0<g&&!d(a[k-1],b[g-1],c.add(String(k-1))).length)l=e(k-1,g-1);else{l=[];if(0<k){var t=e(k-1,g);l.push(x(t,{op:"remove",index:k-1}))}0<g&&(t=e(k,g-1),l.push(x(t,{op:"add",index:k-1,value:b[g-1]})));0<k&&0<g&&(t=e(k-1,g-1),l.push(x(t,{op:"replace", | ||
g));h=d(h.original,h.value,l);return[k.concat(...h),g]},[[],0]);return u}function K(a,b,c,d=q){const e=[];A(a,b).forEach(f=>{e.push({op:"remove",path:c.add(f).toString()})});A(b,a).forEach(f=>{e.push({op:"add",path:c.add(f).toString(),value:b[f]})});G([a,b]).forEach(f=>{e.push(...d(a[f],b[f],c.add(f)))});return e}function q(a,b,c,d=q){if(a===b)return[];const e=z(a),f=z(b);return"array"==e&&"array"==f?H(a,b,c,d):"object"==e&&"object"==f?K(a,b,c,d):[{op:"replace",path:c.toString(),value:b}]}function y(a, | ||
b,c){Array.isArray(a)?"-"==b?a.push(c):(b=parseInt(b,10),a.splice(b,0,c)):a[b]=c}function C(a,b){Array.isArray(a)?(b=parseInt(b,10),a.splice(b,1)):delete a[b]}function L(a,b){a=m.fromJSON(b.path).evaluate(a);if(null===a.parent)return new n(b.path);if(Array.isArray(a.parent)){if(parseInt(a.key,10)>=a.parent.length)return new n(b.path)}else if(void 0===a.value)return new n(b.path);a.parent[a.key]=b.value;return null}function M(a,b){switch(b.op){case "add":return a=m.fromJSON(b.path).evaluate(a),void 0=== | ||
a.parent?b=new n(b.path):(y(a.parent,a.key,v(b.value)),b=null),b;case "remove":return a=m.fromJSON(b.path).evaluate(a),void 0===a.value?b=new n(b.path):(C(a.parent,a.key),b=null),b;case "replace":return L(a,b);case "move":var c=m.fromJSON(b.from).evaluate(a);void 0===c.value?b=new n(b.from):(a=m.fromJSON(b.path).evaluate(a),void 0===a.parent?b=new n(b.path):(C(c.parent,c.key),y(a.parent,a.key,c.value),b=null));return b;case "copy":return c=m.fromJSON(b.from).evaluate(a),void 0===c.value?b=new n(b.from): | ||
(a=m.fromJSON(b.path).evaluate(a),void 0===a.parent?b=new n(b.path):(y(a.parent,a.key,v(c.value)),b=null)),b;case "test":return a=m.fromJSON(b.path).evaluate(a),b=q(a.value,b.value,new m).length?new N(a.value,b.value):null,b}return new O(b)}function P(a){function b(c,d,e){const f=a(c,d,e);return Array.isArray(f)?f:q(c,d,e,b)}return b}function D(a,b){a=m.fromJSON(b).evaluate(a);if(void 0!==a)return{op:"test",path:b,value:a.value}}class m{constructor(a=[""]){this.tokens=a}static fromJSON(a){const b= | ||
a.split("/").map(r);if(""!==b[0])throw Error(`Invalid JSON Pointer: ${a}`);return new m(b)}toString(){return this.tokens.map(E).join("/")}evaluate(a){let b=null,c="";for(let d=1,e=this.tokens.length;d<e;d++)b=a,c=this.tokens[d],"__proto__"!=c&&"constructor"!=c&&"prototype"!=c&&(a=(b||{})[c]);return{parent:b,key:c,value:a}}get(a){return this.evaluate(a).value}set(a,b){for(let c=1,d=this.tokens.length-1,e=this.tokens[c];c<d;c++)a=(a||{})[e];a&&(a[this.tokens[this.tokens.length-1]]=b)}push(a){this.tokens.push(a)}add(a){a= | ||
this.tokens.concat(String(a));return new m(a)}}const w=Object.prototype.hasOwnProperty;class n extends Error{constructor(a){super(`Value required at path: ${a}`);this.path=a;this.name="MissingError"}}class N extends Error{constructor(a,b){super(`Test failed: ${a} != ${b}`);this.actual=a;this.expected=b;this.name="TestError"}}class O extends Error{constructor(a){super(`Invalid operation: ${a.op}`);this.operation=a;this.name="InvalidOperationError"}}p.applyPatch=function(a,b){return b.map(c=>M(a,c))}; | ||
p.createPatch=function(a,b,c){const d=new m;return(c?P(c):q)(a,b,d)};p.createTests=function(a,b){const c=[];b.filter(F).forEach(d=>{const e=D(a,d.path);e&&c.push(e);"from"in d&&(d=D(a,d.from))&&c.push(d)});return c};Object.defineProperty(p,"__esModule",{value:!0})}); | ||
b,c){Array.isArray(a)?"-"==b?a.push(c):(b=parseInt(b,10),a.splice(b,0,c)):a[b]=c}function C(a,b){Array.isArray(a)?(b=parseInt(b,10),a.splice(b,1)):delete a[b]}function L(a,b){a=m.fromJSON(b.path).evaluate(a);if(null===a.parent)return new p(b.path);if(Array.isArray(a.parent)){if(parseInt(a.key,10)>=a.parent.length)return new p(b.path)}else if(void 0===a.value)return new p(b.path);a.parent[a.key]=b.value;return null}function M(a,b){switch(b.op){case "add":return a=m.fromJSON(b.path).evaluate(a),void 0=== | ||
a.parent?b=new p(b.path):(y(a.parent,a.key,v(b.value)),b=null),b;case "remove":return a=m.fromJSON(b.path).evaluate(a),void 0===a.value?b=new p(b.path):(C(a.parent,a.key),b=null),b;case "replace":return L(a,b);case "move":var c=m.fromJSON(b.from).evaluate(a);void 0===c.value?b=new p(b.from):(a=m.fromJSON(b.path).evaluate(a),void 0===a.parent?b=new p(b.path):(C(c.parent,c.key),y(a.parent,a.key,c.value),b=null));return b;case "copy":return c=m.fromJSON(b.from).evaluate(a),void 0===c.value?b=new p(b.from): | ||
(a=m.fromJSON(b.path).evaluate(a),void 0===a.parent?b=new p(b.path):(y(a.parent,a.key,v(c.value)),b=null)),b;case "test":return a=m.fromJSON(b.path).evaluate(a),b=q(a.value,b.value,new m).length?new N(a.value,b.value):null,b}return new O(b)}function P(a){function b(c,d,e){const f=a(c,d,e);return Array.isArray(f)?f:q(c,d,e,b)}return b}function D(a,b){a=m.fromJSON(b).evaluate(a);if(void 0!==a)return{op:"test",path:b,value:a.value}}class m{constructor(a=[""]){this.tokens=a}static fromJSON(a){const b= | ||
a.split("/").map(r);if(""!==b[0])throw Error(`Invalid JSON Pointer: ${a}`);return new m(b)}toString(){return this.tokens.map(E).join("/")}evaluate(a){let b=null,c="";for(let d=1,e=this.tokens.length;d<e;d++)b=a,c=this.tokens[d],"__proto__"!=c&&"constructor"!=c&&"prototype"!=c&&(a=(b||{})[c]);return{parent:b,key:c,value:a}}get(a){return this.evaluate(a).value}set(a,b){a=this.evaluate(a);a.parent&&(a.parent[a.key]=b)}push(a){this.tokens.push(a)}add(a){a=this.tokens.concat(String(a));return new m(a)}} | ||
const w=Object.prototype.hasOwnProperty;class p extends Error{constructor(a){super(`Value required at path: ${a}`);this.path=a;this.name="MissingError"}}class N extends Error{constructor(a,b){super(`Test failed: ${a} != ${b}`);this.actual=a;this.expected=b;this.name="TestError"}}class O extends Error{constructor(a){super(`Invalid operation: ${a.op}`);this.operation=a;this.name="InvalidOperationError"}}n.Pointer=m;n.applyPatch=function(a,b){return b.map(c=>M(a,c))};n.createPatch=function(a,b,c){const d= | ||
new m;return(c?P(c):q)(a,b,d)};n.createTests=function(a,b){const c=[];b.filter(F).forEach(d=>{const e=D(a,d.path);e&&c.push(e);"from"in d&&(d=D(a,d.from))&&c.push(d)});return c};Object.defineProperty(n,"__esModule",{value:!0})}); |
@@ -0,4 +1,6 @@ | ||
import { Pointer } from './pointer'; | ||
export { Pointer }; | ||
import { Operation, TestOperation, VoidableDiff } from './diff'; | ||
export { Operation, TestOperation }; | ||
export declare type Patch = Operation[]; | ||
export type Patch = Operation[]; | ||
/** | ||
@@ -5,0 +7,0 @@ Apply a 'application/json-patch+json'-type patch to an object. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createTests = exports.createPatch = exports.applyPatch = void 0; | ||
exports.createTests = exports.createPatch = exports.applyPatch = exports.Pointer = void 0; | ||
var pointer_1 = require("./pointer"); | ||
Object.defineProperty(exports, "Pointer", { enumerable: true, get: function () { return pointer_1.Pointer; } }); | ||
var patch_1 = require("./patch"); | ||
@@ -24,3 +25,3 @@ var diff_1 = require("./diff"); | ||
function applyPatch(object, patch) { | ||
return patch.map(function (operation) { return patch_1.apply(object, operation); }); | ||
return patch.map(function (operation) { return (0, patch_1.apply)(object, operation); }); | ||
} | ||
@@ -32,3 +33,3 @@ exports.applyPatch = applyPatch; | ||
// ensure an array is always returned | ||
return Array.isArray(custom_patch) ? custom_patch : diff_1.diffAny(input, output, ptr, wrappedDiff); | ||
return Array.isArray(custom_patch) ? custom_patch : (0, diff_1.diffAny)(input, output, ptr, wrappedDiff); | ||
} | ||
@@ -35,0 +36,0 @@ return wrappedDiff; |
{ | ||
"name": "rfc6902", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "Complete implementation of RFC6902 (patch and diff)", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
12
patch.js
@@ -25,3 +25,3 @@ "use strict"; | ||
function MissingError(path) { | ||
var _this = _super.call(this, "Value required at path: " + path) || this; | ||
var _this = _super.call(this, "Value required at path: ".concat(path)) || this; | ||
_this.path = path; | ||
@@ -37,3 +37,3 @@ _this.name = 'MissingError'; | ||
function TestError(actual, expected) { | ||
var _this = _super.call(this, "Test failed: " + actual + " != " + expected) || this; | ||
var _this = _super.call(this, "Test failed: ".concat(actual, " != ").concat(expected)) || this; | ||
_this.actual = actual; | ||
@@ -87,3 +87,3 @@ _this.expected = expected; | ||
} | ||
_add(endpoint.parent, endpoint.key, util_1.clone(operation.value)); | ||
_add(endpoint.parent, endpoint.key, (0, util_1.clone)(operation.value)); | ||
return null; | ||
@@ -188,3 +188,3 @@ } | ||
} | ||
_add(endpoint.parent, endpoint.key, util_1.clone(from_endpoint.value)); | ||
_add(endpoint.parent, endpoint.key, (0, util_1.clone)(from_endpoint.value)); | ||
return null; | ||
@@ -204,3 +204,3 @@ } | ||
// TODO: this diffAny(...).length usage could/should be lazy | ||
if (diff_1.diffAny(endpoint.value, operation.value, new pointer_1.Pointer()).length) { | ||
if ((0, diff_1.diffAny)(endpoint.value, operation.value, new pointer_1.Pointer()).length) { | ||
return new TestError(endpoint.value, operation.value); | ||
@@ -214,3 +214,3 @@ } | ||
function InvalidOperationError(operation) { | ||
var _this = _super.call(this, "Invalid operation: " + operation.op) || this; | ||
var _this = _super.call(this, "Invalid operation: ".concat(operation.op)) || this; | ||
_this.operation = operation; | ||
@@ -217,0 +217,0 @@ _this.name = 'InvalidOperationError'; |
@@ -52,3 +52,3 @@ "use strict"; | ||
if (tokens[0] !== '') | ||
throw new Error("Invalid JSON Pointer: " + path); | ||
throw new Error("Invalid JSON Pointer: ".concat(path)); | ||
return new Pointer(tokens); | ||
@@ -84,10 +84,6 @@ }; | ||
Pointer.prototype.set = function (object, value) { | ||
var cursor = object; | ||
for (var i = 1, l = this.tokens.length - 1, token = this.tokens[i]; i < l; i++) { | ||
// not sure if this the best way to handle non-existant paths... | ||
cursor = (cursor || {})[token]; | ||
var endpoint = this.evaluate(object); | ||
if (endpoint.parent) { | ||
endpoint.parent[endpoint.key] = value; | ||
} | ||
if (cursor) { | ||
cursor[this.tokens[this.tokens.length - 1]] = value; | ||
} | ||
}; | ||
@@ -94,0 +90,0 @@ Pointer.prototype.push = function (token) { |
@@ -30,3 +30,3 @@ # rfc6902 | ||
```js | ||
var rfc6902 = require('rfc6902') | ||
const rfc6902 = require('rfc6902') | ||
``` | ||
@@ -44,3 +44,3 @@ | ||
```js | ||
var users = [{first: 'Chris', last: 'Brown', age: 20}] | ||
const users = [{first: 'Chris', last: 'Brown', age: 20}] | ||
rfc6902.applyPatch(users, [ | ||
@@ -264,6 +264,6 @@ {op: 'replace', path: '/0/age', value: 21}, | ||
This project implements JSON Pointer functionality in `rfc6902/pointer`; e.g.: | ||
This project implements JSON Pointer functionality; e.g.: | ||
```js | ||
const {Pointer} = require('rfc6902/pointer') | ||
const {Pointer} = require('rfc6902') | ||
const repository = { | ||
@@ -270,0 +270,0 @@ contributors: ['chbrown', 'diachedelic', 'nathanrobinson', 'kbiedrzycki', 'stefanmaric'] |
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
91519
1748