Comparing version 1.3.0 to 2.0.0
{ | ||
"name": "rfc6902", | ||
"description": "Complete implementation of RFC6902 (patch and diff)", | ||
"main": "./rfc6902.js", | ||
"main": "./dist/rfc6902.js", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Christopher Brown <io@henrian.com> (http://henrian.com)" |
32
diff.js
@@ -1,9 +0,12 @@ | ||
import { compare } from './equal'; | ||
export function isDestructive({ op }) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const equal_1 = require("./equal"); | ||
function isDestructive({ op }) { | ||
return op === 'remove' || op === 'replace' || op === 'copy' || op === 'move'; | ||
} | ||
exports.isDestructive = isDestructive; | ||
/** | ||
subtract(a, b) returns the keys in `a` that are not in `b`. | ||
*/ | ||
export function subtract(a, b) { | ||
function subtract(a, b) { | ||
const obj = {}; | ||
@@ -18,6 +21,7 @@ for (let add_key in a) { | ||
} | ||
exports.subtract = subtract; | ||
/** | ||
intersection(objects) returns the keys that shared by all given `objects`. | ||
*/ | ||
export function intersection(objects) { | ||
function intersection(objects) { | ||
// initialize like union() | ||
@@ -39,3 +43,4 @@ const key_counts = {}; | ||
} | ||
export function objectType(object) { | ||
exports.intersection = intersection; | ||
function objectType(object) { | ||
if (object === undefined) { | ||
@@ -52,2 +57,3 @@ return 'undefined'; | ||
} | ||
exports.objectType = objectType; | ||
function isArrayAdd(array_operation) { | ||
@@ -86,3 +92,3 @@ return array_operation.op === 'add'; | ||
*/ | ||
export function diffArrays(input, output, ptr) { | ||
function diffArrays(input, output, ptr) { | ||
// set up cost matrix (very simple initialization: just a map) | ||
@@ -106,3 +112,3 @@ const memo = { | ||
if (memoized === undefined) { | ||
if (compare(input[i - 1], output[j - 1])) { | ||
if (equal_1.compare(input[i - 1], output[j - 1])) { | ||
// equal (no operations => no cost) | ||
@@ -198,3 +204,4 @@ memoized = dist(i - 1, j - 1); | ||
} | ||
export function diffObjects(input, output, ptr) { | ||
exports.diffArrays = diffArrays; | ||
function diffObjects(input, output, ptr) { | ||
// if a key is in input but not output -> remove it | ||
@@ -215,4 +222,5 @@ const operations = []; | ||
} | ||
export function diffValues(input, output, ptr) { | ||
if (!compare(input, output)) { | ||
exports.diffObjects = diffObjects; | ||
function diffValues(input, output, ptr) { | ||
if (!equal_1.compare(input, output)) { | ||
return [{ op: 'replace', path: ptr.toString(), value: output }]; | ||
@@ -222,3 +230,4 @@ } | ||
} | ||
export function diffAny(input, output, ptr) { | ||
exports.diffValues = diffValues; | ||
function diffAny(input, output, ptr) { | ||
const input_type = objectType(input); | ||
@@ -236,1 +245,2 @@ const output_type = objectType(output); | ||
} | ||
exports.diffAny = diffAny; |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -51,3 +53,3 @@ zip(a, b) assumes that a.length === b.length. | ||
*/ | ||
export function compare(left, right) { | ||
function compare(left, right) { | ||
// strict equality handles literals, numbers, and strings (a sufficient but not necessary cause) | ||
@@ -67,1 +69,2 @@ if (left === right) | ||
} | ||
exports.compare = compare; |
@@ -1,16 +0,20 @@ | ||
export class MissingError extends Error { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class MissingError extends Error { | ||
constructor(path) { | ||
super(`Value required at path: ${path}`); | ||
this.path = path; | ||
this.name = this.constructor.name; | ||
this.name = 'MissingError'; | ||
} | ||
} | ||
export class InvalidOperationError extends Error { | ||
exports.MissingError = MissingError; | ||
class InvalidOperationError extends Error { | ||
constructor(op) { | ||
super(`Invalid operation: ${op}`); | ||
this.op = op; | ||
this.name = this.constructor.name; | ||
this.name = 'InvalidOperationError'; | ||
} | ||
} | ||
export class TestError extends Error { | ||
exports.InvalidOperationError = InvalidOperationError; | ||
class TestError extends Error { | ||
constructor(actual, expected) { | ||
@@ -20,3 +24,3 @@ super(`Test failed: ${actual} != ${expected}`); | ||
this.expected = expected; | ||
this.name = this.constructor.name; | ||
this.name = 'TestError'; | ||
this.actual = actual; | ||
@@ -26,1 +30,2 @@ this.expected = expected; | ||
} | ||
exports.TestError = TestError; |
29
index.js
@@ -1,5 +0,7 @@ | ||
import { InvalidOperationError } from './errors'; | ||
import { Pointer } from './pointer'; | ||
import * as operationFunctions from './patch'; | ||
import { diffAny, isDestructive } from './diff'; | ||
"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"); | ||
/** | ||
@@ -22,3 +24,3 @@ Apply a 'application/json-patch+json'-type patch to an object. | ||
*/ | ||
export function applyPatch(object, patch) { | ||
function applyPatch(object, patch) { | ||
return patch.map(operation => { | ||
@@ -28,3 +30,3 @@ const operationFunction = operationFunctions[operation.op]; | ||
if (operationFunction === undefined) { | ||
return new InvalidOperationError(operation.op); | ||
return new errors_1.InvalidOperationError(operation.op); | ||
} | ||
@@ -34,2 +36,3 @@ return operationFunction(object, operation); | ||
} | ||
exports.applyPatch = applyPatch; | ||
/** | ||
@@ -44,9 +47,10 @@ Produce a 'application/json-patch+json'-type patch to get from one object to | ||
*/ | ||
export function createPatch(input, output) { | ||
const ptr = new Pointer(); | ||
function createPatch(input, output) { | ||
const ptr = new pointer_1.Pointer(); | ||
// a new Pointer gets a default path of [''] if not specified | ||
return diffAny(input, output, ptr); | ||
return diff_1.diffAny(input, output, ptr); | ||
} | ||
exports.createPatch = createPatch; | ||
function createTest(input, path) { | ||
const endpoint = Pointer.fromJSON(path).evaluate(input); | ||
const endpoint = pointer_1.Pointer.fromJSON(path).evaluate(input); | ||
if (endpoint !== undefined) { | ||
@@ -66,5 +70,5 @@ return { op: 'test', path, value: endpoint.value }; | ||
*/ | ||
export function createTests(input, patch) { | ||
function createTests(input, patch) { | ||
const tests = new Array(); | ||
patch.filter(isDestructive).forEach(operation => { | ||
patch.filter(diff_1.isDestructive).forEach(operation => { | ||
const pathTest = createTest(input, operation.path); | ||
@@ -81,1 +85,2 @@ if (pathTest) | ||
} | ||
exports.createTests = createTests; |
{ | ||
"name": "rfc6902", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Complete implementation of RFC6902 (patch and diff)", | ||
@@ -18,18 +18,17 @@ "keywords": [ | ||
"license": "MIT", | ||
"main": "./rfc6902.js", | ||
"devDependencies": { | ||
"babel-core": "^5.0.0", | ||
"babelify": "^5.0.0", | ||
"browserify": "12.0.1", | ||
"coveralls": "*", | ||
"derequire": "2.0.3", | ||
"istanbul": "*", | ||
"js-yaml": "*", | ||
"mocha": "*", | ||
"mocha-lcov-reporter": "*", | ||
"typescript": "*" | ||
"coveralls": "^3.0.0", | ||
"istanbul": "0.4.5", | ||
"js-yaml": "3.10.0", | ||
"mocha": "^4.0.1", | ||
"mocha-lcov-reporter": "1.3.0", | ||
"rollup": "^0.50.0", | ||
"typescript": "^2.5.3" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
"prepare": "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" | ||
} | ||
} |
60
patch.js
@@ -1,4 +0,6 @@ | ||
import { Pointer } from './pointer'; | ||
import { compare } from './equal'; | ||
import { MissingError, TestError } from './errors'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const pointer_1 = require("./pointer"); | ||
const equal_1 = require("./equal"); | ||
const errors_1 = require("./errors"); | ||
function _add(object, key, value) { | ||
@@ -36,7 +38,7 @@ if (Array.isArray(object)) { | ||
*/ | ||
export function add(object, operation) { | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
function add(object, operation) { | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
// it's not exactly a "MissingError" in the same way that `remove` is -- more like a MissingParent, or something | ||
if (endpoint.parent === undefined) { | ||
return new MissingError(operation.path); | ||
return new errors_1.MissingError(operation.path); | ||
} | ||
@@ -46,2 +48,3 @@ _add(endpoint.parent, endpoint.key, operation.value); | ||
} | ||
exports.add = add; | ||
/** | ||
@@ -51,7 +54,7 @@ > The "remove" operation removes the value at the target location. | ||
*/ | ||
export function remove(object, operation) { | ||
function remove(object, operation) { | ||
// endpoint has parent, key, and value properties | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
if (endpoint.value === undefined) { | ||
return new MissingError(operation.path); | ||
return new errors_1.MissingError(operation.path); | ||
} | ||
@@ -62,2 +65,3 @@ // not sure what the proper behavior is when path = '' | ||
} | ||
exports.remove = remove; | ||
/** | ||
@@ -75,9 +79,10 @@ > The "replace" operation replaces the value at the target location | ||
*/ | ||
export function replace(object, operation) { | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
function replace(object, operation) { | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
if (endpoint.value === undefined) | ||
return new MissingError(operation.path); | ||
return new errors_1.MissingError(operation.path); | ||
endpoint.parent[endpoint.key] = operation.value; | ||
return null; | ||
} | ||
exports.replace = replace; | ||
/** | ||
@@ -98,9 +103,9 @@ > The "move" operation removes the value at a specified location and | ||
*/ | ||
export function move(object, operation) { | ||
var from_endpoint = Pointer.fromJSON(operation.from).evaluate(object); | ||
function move(object, operation) { | ||
var from_endpoint = pointer_1.Pointer.fromJSON(operation.from).evaluate(object); | ||
if (from_endpoint.value === undefined) | ||
return new MissingError(operation.from); | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
return new errors_1.MissingError(operation.from); | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
if (endpoint.parent === undefined) | ||
return new MissingError(operation.path); | ||
return new errors_1.MissingError(operation.path); | ||
_remove(from_endpoint.parent, from_endpoint.key); | ||
@@ -110,2 +115,3 @@ _add(endpoint.parent, endpoint.key, from_endpoint.value); | ||
} | ||
exports.move = move; | ||
/** | ||
@@ -124,9 +130,9 @@ > The "copy" operation copies the value at a specified location to the | ||
*/ | ||
export function copy(object, operation) { | ||
var from_endpoint = Pointer.fromJSON(operation.from).evaluate(object); | ||
function copy(object, operation) { | ||
var from_endpoint = pointer_1.Pointer.fromJSON(operation.from).evaluate(object); | ||
if (from_endpoint.value === undefined) | ||
return new MissingError(operation.from); | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
return new errors_1.MissingError(operation.from); | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
if (endpoint.parent === undefined) | ||
return new MissingError(operation.path); | ||
return new errors_1.MissingError(operation.path); | ||
_remove(from_endpoint.parent, from_endpoint.key); | ||
@@ -136,2 +142,3 @@ _add(endpoint.parent, endpoint.key, from_endpoint.value); | ||
} | ||
exports.copy = copy; | ||
/** | ||
@@ -145,8 +152,9 @@ > The "test" operation tests that a value at the target location is | ||
*/ | ||
export function test(object, operation) { | ||
var endpoint = Pointer.fromJSON(operation.path).evaluate(object); | ||
var result = compare(endpoint.value, operation.value); | ||
function test(object, operation) { | ||
var endpoint = pointer_1.Pointer.fromJSON(operation.path).evaluate(object); | ||
var result = equal_1.compare(endpoint.value, operation.value); | ||
if (!result) | ||
return new TestError(endpoint.value, operation.value); | ||
return new errors_1.TestError(endpoint.value, operation.value); | ||
return null; | ||
} | ||
exports.test = test; |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -38,3 +40,3 @@ Unescape token part of a JSON Pointer string | ||
*/ | ||
export class Pointer { | ||
class Pointer { | ||
constructor(tokens = ['']) { | ||
@@ -89,1 +91,2 @@ this.tokens = tokens; | ||
} | ||
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
7
21
0
0
67096
1488