@clipchamp/json-delta
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -1,2 +0,2 @@ | ||
export declare type AnyJson = boolean | number | string | null | JsonArray | JsonMap; | ||
declare type AnyJson = boolean | number | string | null | JsonArray | JsonMap; | ||
export interface JsonMap { | ||
@@ -8,17 +8,15 @@ [key: string]: AnyJson; | ||
export declare type ObjPath = (string | number)[]; | ||
export declare type DiffInsert = [ObjPath, any]; | ||
export declare type DiffPart = DiffInsert | ObjPath; | ||
export declare type Diff = DiffPart[]; | ||
export declare function isInsert(d: DiffPart): d is DiffInsert; | ||
export declare function isObj(o: any): o is { | ||
export declare type DiffInsert<T> = [ObjPath, T]; | ||
export declare type DiffPart<T> = DiffInsert<T> | ObjPath; | ||
export declare type Diff<T> = DiffPart<T>[]; | ||
export declare function isObject(o: any): o is { | ||
[k: string]: any; | ||
}; | ||
export declare function isArr(o: any): o is Array<any>; | ||
export declare function shallowCopy(o: AnyJson): AnyJson; | ||
export declare function getContainer(orig: AnyJson, result: AnyJson, path: ObjPath): AnyJson | void; | ||
export declare function shallowCopy<T>(o: T): T; | ||
export declare function getContainer<T>(orig: T, result: T, path: ObjPath): AnyJson | void; | ||
export declare function getVal(container: AnyJson, path: ObjPath): AnyJson; | ||
export declare function applyDiff(o: AnyJson, d: Diff | void): AnyJson; | ||
export declare function applyInsert(orig: AnyJson, result: AnyJson, insert: DiffInsert): AnyJson; | ||
export declare function applyDelete(orig: AnyJson, result: AnyJson, path: ObjPath): AnyJson; | ||
export declare function diff(a: AnyJson, b: AnyJson, tolerance?: number): Diff | null; | ||
export declare function applyDiff<T>(o: T, d: Diff<T> | void): T; | ||
export declare function applyInsert<T>(orig: T, result: T, insert: DiffInsert<T>): T; | ||
export declare function applyDelete<T>(orig: T, result: T, path: ObjPath): T; | ||
export declare function diff<T>(a: T, b: T, tolerance?: number): Diff<T> | null; | ||
export declare function deepEqual(a: AnyJson, b: AnyJson): boolean; | ||
@@ -33,1 +31,2 @@ /** | ||
export declare function lcs(a: AnyJson[], b: AnyJson[], tolerance?: number): AnyJson[] | void; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isInsert(d) { | ||
return isArr(d[0]); | ||
} | ||
exports.isInsert = isInsert; | ||
function isObj(o) { | ||
const isArray_1 = require("./util/isArray"); | ||
const isInsert_1 = require("./util/isInsert"); | ||
function isObject(o) { | ||
return o instanceof Object && !(o instanceof Array); | ||
} | ||
exports.isObj = isObj; | ||
function isArr(o) { | ||
return o instanceof Array; | ||
} | ||
exports.isArr = isArr; | ||
exports.isObject = isObject; | ||
function shallowCopy(o) { | ||
if (isObj(o)) | ||
if (isObject(o)) { | ||
return Object.assign({}, o); | ||
if (isArr(o)) | ||
} | ||
if (isArray_1.isArray(o)) { | ||
return o.slice(); | ||
} | ||
return o; | ||
@@ -33,3 +29,3 @@ } | ||
let seg = path[i]; | ||
if (typeof seg === "number" && isArr(origContainer) && isArr(container)) { | ||
if (typeof seg === 'number' && isArray_1.isArray(origContainer) && isArray_1.isArray(container)) { | ||
origContainer = origContainer[seg]; | ||
@@ -43,3 +39,3 @@ if (container[seg] === origContainer) { | ||
} | ||
if (typeof seg === "string" && isObj(origContainer) && isObj(container)) { | ||
if (typeof seg === 'string' && isObject(origContainer) && isObject(container)) { | ||
origContainer = origContainer[seg]; | ||
@@ -61,6 +57,6 @@ if (container[seg] === origContainer) { | ||
let seg = path[i]; | ||
if (typeof seg === "number" && isArr(container)) { | ||
if (typeof seg === 'number' && isArray_1.isArray(container)) { | ||
container = container[seg]; | ||
} | ||
if (typeof seg === "string" && isObj(container)) { | ||
if (typeof seg === 'string' && isObject(container)) { | ||
container = container[seg]; | ||
@@ -77,3 +73,3 @@ } | ||
d.forEach(p => { | ||
if (isInsert(p)) | ||
if (isInsert_1.isInsert(p)) | ||
result = applyInsert(o, result, p); | ||
@@ -92,6 +88,6 @@ else | ||
let key = path[path.length - 1]; | ||
if (typeof key === "number" && isArr(container)) { | ||
if (typeof key === 'number' && isArray_1.isArray(container)) { | ||
container.splice(key, 0, val); | ||
} | ||
if (typeof key === "string" && isObj(container)) { | ||
if (typeof key === 'string' && isObject(container)) { | ||
container[key] = val; | ||
@@ -107,7 +103,7 @@ } | ||
let key = path[path.length - 1]; | ||
if (typeof key === "number" && isArr(container)) { | ||
if (typeof key === 'number' && isArray_1.isArray(container)) { | ||
container.splice(key, 1); | ||
return result; | ||
} | ||
if (typeof key === "string" && isObj(container)) { | ||
if (typeof key === 'string' && isObject(container)) { | ||
delete container[key]; | ||
@@ -133,5 +129,5 @@ return result; | ||
b = null; | ||
if (typeof a === "number" && isNaN(a)) | ||
if (typeof a === 'number' && isNaN(a)) | ||
a = null; | ||
if (typeof b === "number" && isNaN(b)) | ||
if (typeof b === 'number' && isNaN(b)) | ||
b = null; | ||
@@ -170,3 +166,3 @@ if (a === b) | ||
for (var k in a) { | ||
if (!(k in b)) { | ||
if (!(k in (b))) { | ||
result.push(path.concat([k])); | ||
@@ -211,5 +207,3 @@ if (result.length > tolerance) { | ||
let result = []; | ||
return arrDiff(a, b, tolerance, aIdx => result.push(a[aIdx])) | ||
? result.reverse() | ||
: null; | ||
return arrDiff(a, b, tolerance, aIdx => result.push(a[aIdx])) ? result.reverse() : null; | ||
} | ||
@@ -236,5 +230,3 @@ exports.lcs = lcs; | ||
let bIdx = aIdx - k; | ||
while (aIdx < aLen && | ||
bIdx < bLen && | ||
deepEqual(a[aIdx], b[bIdx])) { | ||
while (aIdx < aLen && bIdx < bLen && deepEqual(a[aIdx], b[bIdx])) { | ||
aIdx++; | ||
@@ -241,0 +233,0 @@ bIdx++; |
{ | ||
"name": "@clipchamp/json-delta", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Json object diff / patching with configurable short-circuit tolerance. Forked from: github.com/corps", | ||
"keywords": [ | ||
"json", | ||
"diff", | ||
"delta", | ||
"patching", | ||
"fast" | ||
], | ||
"homepage": "https://github.com/clipchamp/json-delta#readme", | ||
"bugs": { | ||
"url": "https://github.com/clipchamp/json-delta/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/clipchamp/json-delta.git" | ||
}, | ||
"main": "dist/index.js", | ||
@@ -21,2 +36,3 @@ "types": "dist/index.d.ts", | ||
"karma-webpack": "^2.0.6", | ||
"prettier": "1.17.0", | ||
"quick_check": "^0.7.0", | ||
@@ -26,5 +42,5 @@ "qunitjs": "^2.4.1", | ||
"ts-loader": "^3.1.1", | ||
"typescript": "2.5.3", | ||
"typescript": "^3.4.5", | ||
"webpack": "^3.8.1" | ||
} | ||
} |
@@ -35,3 +35,3 @@ # json-delta | ||
``` | ||
```javascript | ||
var jd = require("json-delta"); | ||
@@ -47,1 +47,40 @@ var diff = jd.diff([1, 2, 3], [1, 2, 3, 4]); | ||
``` | ||
## Using `json-delta` | ||
`json-delta` supplies two pretty main/important functions, at least for clipchamp. | ||
### `diff<T>` | ||
`diff<T>` takes two objects, and will return to you a `Diff<T>`. This diff | ||
object can only be applied to the type of object (supplied by `T`). For example: | ||
```typescript | ||
const coordinates1: Coordinates = { | ||
x: 1, | ||
y: 0 | ||
}; | ||
const coordinates2: Coordinates = { | ||
x: 1, | ||
y: 1 | ||
}; | ||
// This will result in the type of Diff<Coordinates> | ||
const diffBetweenCoordinates = diff(coordinates1, coordinates2); | ||
``` | ||
The resulting diff will be something like: | ||
```typescript | ||
const someDiff = { | ||
y: 0 => 1 | ||
} | ||
``` | ||
### `applyDiff<T>` | ||
`applyDiff<T>` will allow you to apply a diff you previous collected, back onto | ||
the object (that follows the same generic typing of `T`). | ||
```typescript | ||
const coordinates1As2 = applyDiff(coordiantes1, diffBetweenCoordinates); | ||
``` | ||
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
218995
12
314
1
0
85
0
10