Comparing version 2.2.0 to 2.2.1
74
diff.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const equal_1 = require("./equal"); | ||
function isDestructive({ op }) { | ||
var equal_1 = require("./equal"); | ||
function isDestructive(_a) { | ||
var op = _a.op; | ||
return op === 'remove' || op === 'replace' || op === 'copy' || op === 'move'; | ||
@@ -12,7 +13,7 @@ } | ||
function subtract(a, b) { | ||
const obj = {}; | ||
for (let add_key in a) { | ||
var obj = {}; | ||
for (var add_key in a) { | ||
obj[add_key] = 1; | ||
} | ||
for (let del_key in b) { | ||
for (var del_key in b) { | ||
delete obj[del_key]; | ||
@@ -28,5 +29,5 @@ } | ||
// initialize like union() | ||
const key_counts = {}; | ||
objects.forEach(object => { | ||
for (let key in object) { | ||
var key_counts = {}; | ||
objects.forEach(function (object) { | ||
for (var key in object) { | ||
key_counts[key] = (key_counts[key] || 0) + 1; | ||
@@ -36,4 +37,4 @@ } | ||
// but then, extra requirement: delete less commonly-seen keys | ||
const threshold = objects.length; | ||
for (let key in key_counts) { | ||
var threshold = objects.length; | ||
for (var key in key_counts) { | ||
if (key_counts[key] < threshold) { | ||
@@ -94,3 +95,3 @@ delete key_counts[key]; | ||
// set up cost matrix (very simple initialization: just a map) | ||
const memo = { | ||
var memo = { | ||
'0,0': { operations: [], cost: 0 } | ||
@@ -110,3 +111,3 @@ }; | ||
// memoized | ||
let memoized = memo[i + ',' + j]; | ||
var memoized = memo[i + ',' + j]; | ||
if (memoized === undefined) { | ||
@@ -118,6 +119,6 @@ if (equal_1.compare(input[i - 1], output[j - 1])) { | ||
else { | ||
const alternatives = []; | ||
var alternatives = []; | ||
if (i > 0) { | ||
// NOT topmost row | ||
const remove_alternative = dist(i - 1, j); | ||
var remove_alternative = dist(i - 1, j); | ||
alternatives.push({ | ||
@@ -134,3 +135,3 @@ // the new operation must be pushed on the end | ||
// NOT leftmost column | ||
const add_alternative = dist(i, j - 1); | ||
var add_alternative = dist(i, j - 1); | ||
alternatives.push({ | ||
@@ -148,3 +149,3 @@ operations: add_alternative.operations.concat({ | ||
// supposing we replaced it, compute the rest of the costs: | ||
const replace_alternative = dist(i - 1, j - 1); | ||
var replace_alternative = dist(i - 1, j - 1); | ||
// okay, the general plan is to replace it, but we can be smarter, | ||
@@ -167,3 +168,3 @@ // recursing into the structure and replacing only part of it if | ||
// [4, 6, 7, 1, 2].sort((a, b) => a - b) -> [ 1, 2, 4, 6, 7 ] | ||
const best = alternatives.sort((a, b) => a.cost - b.cost)[0]; | ||
var best = alternatives.sort(function (a, b) { return a.cost - b.cost; })[0]; | ||
memoized = best; | ||
@@ -177,10 +178,11 @@ } | ||
// properties by using 0 for everything but positive numbers | ||
const input_length = (isNaN(input.length) || input.length <= 0) ? 0 : input.length; | ||
const output_length = (isNaN(output.length) || output.length <= 0) ? 0 : output.length; | ||
const array_operations = dist(input_length, output_length).operations; | ||
const [operations, padding] = array_operations.reduce(([operations, padding], array_operation) => { | ||
var input_length = (isNaN(input.length) || input.length <= 0) ? 0 : input.length; | ||
var output_length = (isNaN(output.length) || output.length <= 0) ? 0 : output.length; | ||
var array_operations = dist(input_length, output_length).operations; | ||
var _a = array_operations.reduce(function (_a, array_operation) { | ||
var operations = _a[0], padding = _a[1]; | ||
if (isArrayAdd(array_operation)) { | ||
const padded_index = array_operation.index + 1 + padding; | ||
const index_token = padded_index < (input_length + padding) ? String(padded_index) : '-'; | ||
const operation = { | ||
var padded_index = array_operation.index + 1 + padding; | ||
var index_token = padded_index < (input_length + padding) ? String(padded_index) : '-'; | ||
var operation = { | ||
op: array_operation.op, | ||
@@ -194,3 +196,3 @@ path: ptr.add(index_token).toString(), | ||
else if (isArrayRemove(array_operation)) { | ||
const operation = { | ||
var operation = { | ||
op: array_operation.op, | ||
@@ -203,7 +205,7 @@ path: ptr.add(String(array_operation.index + padding)).toString(), | ||
else { | ||
const replace_ptr = ptr.add(String(array_operation.index + padding)); | ||
const replace_operations = diffAny(array_operation.original, array_operation.value, replace_ptr); | ||
return [operations.concat(...replace_operations), padding]; | ||
var replace_ptr = ptr.add(String(array_operation.index + padding)); | ||
var replace_operations = diffAny(array_operation.original, array_operation.value, replace_ptr); | ||
return [operations.concat.apply(operations, replace_operations), padding]; | ||
} | ||
}, [[], 0]); | ||
}, [[], 0]), operations = _a[0], padding = _a[1]; | ||
return operations; | ||
@@ -214,13 +216,13 @@ } | ||
// if a key is in input but not output -> remove it | ||
const operations = []; | ||
subtract(input, output).forEach(key => { | ||
var operations = []; | ||
subtract(input, output).forEach(function (key) { | ||
operations.push({ op: 'remove', path: ptr.add(key).toString() }); | ||
}); | ||
// if a key is in output but not input -> add it | ||
subtract(output, input).forEach(key => { | ||
subtract(output, input).forEach(function (key) { | ||
operations.push({ op: 'add', path: ptr.add(key).toString(), value: output[key] }); | ||
}); | ||
// if a key is in both, diff it recursively | ||
intersection([input, output]).forEach(key => { | ||
operations.push(...diffAny(input[key], output[key], ptr.add(key))); | ||
intersection([input, output]).forEach(function (key) { | ||
operations.push.apply(operations, diffAny(input[key], output[key], ptr.add(key))); | ||
}); | ||
@@ -238,4 +240,4 @@ return operations; | ||
function diffAny(input, output, ptr) { | ||
const input_type = objectType(input); | ||
const output_type = objectType(output); | ||
var input_type = objectType(input); | ||
var output_type = objectType(output); | ||
if (input_type == 'array' && output_type == 'array') { | ||
@@ -242,0 +244,0 @@ return diffArrays(input, output, ptr); |
@@ -20,3 +20,3 @@ "use strict"; | ||
} | ||
return zip(left, right).every(pair => compare(pair[0], pair[1])); | ||
return zip(left, right).every(function (pair) { return compare(pair[0], pair[1]); }); | ||
} | ||
@@ -32,3 +32,3 @@ /** | ||
} | ||
return left_keys.every(key => compare(left[key], right[key])); | ||
return left_keys.every(function (key) { return compare(left[key], right[key]); }); | ||
} | ||
@@ -35,0 +35,0 @@ /** |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class MissingError extends Error { | ||
constructor(path) { | ||
super(`Value required at path: ${path}`); | ||
this.path = path; | ||
this.name = 'MissingError'; | ||
var MissingError = /** @class */ (function (_super) { | ||
__extends(MissingError, _super); | ||
function MissingError(path) { | ||
var _this = _super.call(this, "Value required at path: " + path) || this; | ||
_this.path = path; | ||
_this.name = 'MissingError'; | ||
return _this; | ||
} | ||
} | ||
return MissingError; | ||
}(Error)); | ||
exports.MissingError = MissingError; | ||
class InvalidOperationError extends Error { | ||
constructor(op) { | ||
super(`Invalid operation: ${op}`); | ||
this.op = op; | ||
this.name = 'InvalidOperationError'; | ||
var InvalidOperationError = /** @class */ (function (_super) { | ||
__extends(InvalidOperationError, _super); | ||
function InvalidOperationError(op) { | ||
var _this = _super.call(this, "Invalid operation: " + op) || this; | ||
_this.op = op; | ||
_this.name = 'InvalidOperationError'; | ||
return _this; | ||
} | ||
} | ||
return InvalidOperationError; | ||
}(Error)); | ||
exports.InvalidOperationError = InvalidOperationError; | ||
class TestError extends Error { | ||
constructor(actual, expected) { | ||
super(`Test failed: ${actual} != ${expected}`); | ||
this.actual = actual; | ||
this.expected = expected; | ||
this.name = 'TestError'; | ||
this.actual = actual; | ||
this.expected = expected; | ||
var TestError = /** @class */ (function (_super) { | ||
__extends(TestError, _super); | ||
function TestError(actual, expected) { | ||
var _this = _super.call(this, "Test failed: " + actual + " != " + expected) || this; | ||
_this.actual = actual; | ||
_this.expected = expected; | ||
_this.name = 'TestError'; | ||
_this.actual = actual; | ||
_this.expected = expected; | ||
return _this; | ||
} | ||
} | ||
return TestError; | ||
}(Error)); | ||
exports.TestError = TestError; |
26
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("./errors"); | ||
const pointer_1 = require("./pointer"); | ||
const operationFunctions = require("./patch"); | ||
const diff_1 = require("./diff"); | ||
var errors_1 = require("./errors"); | ||
var pointer_1 = require("./pointer"); | ||
var operationFunctions = require("./patch"); | ||
var diff_1 = require("./diff"); | ||
/** | ||
@@ -25,4 +25,4 @@ Apply a 'application/json-patch+json'-type patch to an object. | ||
function applyPatch(object, patch) { | ||
return patch.map(operation => { | ||
const operationFunction = operationFunctions[operation.op]; | ||
return patch.map(function (operation) { | ||
var operationFunction = operationFunctions[operation.op]; | ||
// speedy exit if we don't recognize the operation name | ||
@@ -46,3 +46,3 @@ if (operationFunction === undefined) { | ||
function createPatch(input, output) { | ||
const ptr = new pointer_1.Pointer(); | ||
var ptr = new pointer_1.Pointer(); | ||
// a new Pointer gets a default path of [''] if not specified | ||
@@ -53,5 +53,5 @@ return diff_1.diffAny(input, output, ptr); | ||
function createTest(input, path) { | ||
const endpoint = pointer_1.Pointer.fromJSON(path).evaluate(input); | ||
var endpoint = pointer_1.Pointer.fromJSON(path).evaluate(input); | ||
if (endpoint !== undefined) { | ||
return { op: 'test', path, value: endpoint.value }; | ||
return { op: 'test', path: path, value: endpoint.value }; | ||
} | ||
@@ -70,9 +70,9 @@ } | ||
function createTests(input, patch) { | ||
const tests = new Array(); | ||
patch.filter(diff_1.isDestructive).forEach(operation => { | ||
const pathTest = createTest(input, operation.path); | ||
var tests = new Array(); | ||
patch.filter(diff_1.isDestructive).forEach(function (operation) { | ||
var pathTest = createTest(input, operation.path); | ||
if (pathTest) | ||
tests.push(pathTest); | ||
if ('from' in operation) { | ||
const fromTest = createTest(input, operation['from']); | ||
var fromTest = createTest(input, operation['from']); | ||
if (fromTest) | ||
@@ -79,0 +79,0 @@ tests.push(fromTest); |
{ | ||
"name": "rfc6902", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Complete implementation of RFC6902 (patch and diff)", | ||
@@ -19,4 +19,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-core": "^5.0.0", | ||
"coveralls": "^3.0.0", | ||
"@types/js-yaml": "^3.9.1", | ||
"@types/mocha": "^2.2.43", | ||
"@types/node": "^8.0.33", | ||
"coveralls": "^2.13.3", | ||
"istanbul": "0.4.5", | ||
@@ -30,8 +32,9 @@ "js-yaml": "3.10.0", | ||
"scripts": { | ||
"prepublish": "tsc -m commonjs -d", | ||
"test": "istanbul cover _mocha -- tests/ --compilers js:babel-core/register -R spec", | ||
"posttest": "coveralls < coverage/lcov.info", | ||
"dist": "tsc -m es2015 && rollup index.js --output.format umd --name rfc6902 --output.file dist/rfc6902.js && closure-compiler dist/rfc6902.js > dist/rfc6902.min.js", | ||
"prepublish": "tsc -t ES5 -m commonjs -d", | ||
"pretest": "tsc -p tests -t ES5 -m commonjs", | ||
"test": "istanbul cover _mocha -- tests/ -R spec", | ||
"posttest": "coveralls < coverage/lcov.info || true", | ||
"dist": "tsc -t ES2015 -m es2015 && rollup index.js --output.format umd --name rfc6902 --output.file dist/rfc6902.js && closure-compiler dist/rfc6902.js > dist/rfc6902.min.js", | ||
"clean": "tsc -m commonjs -d --listEmittedFiles | sed 's/^TSFILE: //' | xargs rm -v" | ||
} | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const pointer_1 = require("./pointer"); | ||
const equal_1 = require("./equal"); | ||
const errors_1 = require("./errors"); | ||
var pointer_1 = require("./pointer"); | ||
var equal_1 = require("./equal"); | ||
var errors_1 = require("./errors"); | ||
function _add(object, key, value) { | ||
@@ -7,0 +7,0 @@ if (Array.isArray(object)) { |
@@ -40,4 +40,5 @@ "use strict"; | ||
*/ | ||
class Pointer { | ||
constructor(tokens = ['']) { | ||
var Pointer = /** @class */ (function () { | ||
function Pointer(tokens) { | ||
if (tokens === void 0) { tokens = ['']; } | ||
this.tokens = tokens; | ||
@@ -48,11 +49,11 @@ } | ||
*/ | ||
static fromJSON(path) { | ||
Pointer.fromJSON = function (path) { | ||
var tokens = path.split('/').map(unescape); | ||
if (tokens[0] !== '') | ||
throw new Error(`Invalid JSON Pointer: ${path}`); | ||
throw new Error("Invalid JSON Pointer: " + path); | ||
return new Pointer(tokens); | ||
} | ||
toString() { | ||
}; | ||
Pointer.prototype.toString = function () { | ||
return this.tokens.map(escape).join('/'); | ||
} | ||
}; | ||
/** | ||
@@ -63,3 +64,3 @@ Returns an object with 'parent', 'key', and 'value' properties. | ||
*/ | ||
evaluate(object) { | ||
Pointer.prototype.evaluate = function (object) { | ||
var parent = null; | ||
@@ -73,8 +74,8 @@ var token = null; | ||
} | ||
return { parent, key: token, value: object }; | ||
} | ||
get(object) { | ||
return { parent: parent, key: token, value: object }; | ||
}; | ||
Pointer.prototype.get = function (object) { | ||
return this.evaluate(object).value; | ||
} | ||
set(object, value) { | ||
}; | ||
Pointer.prototype.set = function (object, value) { | ||
for (var i = 1, l = this.tokens.length - 1, token = this.tokens[i]; i < l; i++) { | ||
@@ -87,7 +88,7 @@ // not sure if this the best way to handle non-existant paths... | ||
} | ||
} | ||
push(token) { | ||
}; | ||
Pointer.prototype.push = function (token) { | ||
// mutable | ||
this.tokens.push(token); | ||
} | ||
}; | ||
/** | ||
@@ -98,7 +99,8 @@ `token` should be a String. It'll be coerced to one anyway. | ||
*/ | ||
add(token) { | ||
Pointer.prototype.add = function (token) { | ||
var tokens = this.tokens.concat(String(token)); | ||
return new Pointer(tokens); | ||
} | ||
} | ||
}; | ||
return Pointer; | ||
}()); | ||
exports.Pointer = Pointer; |
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
46435
932
10