tree-changes
Advanced tools
Comparing version 0.6.0-2 to 0.6.0-3
@@ -13,2 +13,3 @@ import { Data, Key, Params, PlainObject, Value } from './types'; | ||
}; | ||
export declare function includesOrEqualsTo<T>(previousValue: T | T[], value: T): boolean; | ||
export declare function isArray(value: unknown): value is Array<typeof value>; | ||
@@ -21,2 +22,2 @@ export declare function isCompatible(prevData: any, data: any, options?: Partial<Params>): boolean; | ||
export declare function isSameType(...args: any): boolean; | ||
export declare function nested(input: PlainObject | any[], property?: Key): any; | ||
export declare function nested(object: PlainObject, property?: Key): any; |
@@ -110,2 +110,5 @@ import * as equal from 'fast-deep-equal'; | ||
} | ||
export function includesOrEqualsTo(previousValue, value) { | ||
return isArray(previousValue) ? previousValue.includes(value) : value === previousValue; | ||
} | ||
export function isArray(value) { | ||
@@ -155,18 +158,17 @@ return Array.isArray(value); | ||
} | ||
export function nested(input, property) { | ||
export function nested(object, property) { | ||
/* istanbul ignore else */ | ||
if (isPlainObject(input) || isArray(input)) { | ||
if (isPlainObject(object) || isArray(object)) { | ||
/* istanbul ignore else */ | ||
if (isString(property) && property !== '') { | ||
var split = property.split('.'); | ||
// @ts-ignore | ||
return split.reduce(function (obj, prop) { return obj && obj[prop]; }, input); | ||
if (isString(property) && property) { | ||
var props = property.split('.'); | ||
return props.reduce(function (acc, d) { return acc && acc[d]; }, object); | ||
} | ||
if (isNumber(property) && isArray(input)) { | ||
return input[property]; | ||
if (isNumber(property)) { | ||
return object[property]; | ||
} | ||
return input; | ||
return object; | ||
} | ||
return input; | ||
return object; | ||
} | ||
//# sourceMappingURL=helpers.js.map |
@@ -1,3 +0,4 @@ | ||
import { useEffect, useMemo, useRef } from 'react'; | ||
import treeChanges from './base'; | ||
import { useEffect, useRef } from 'react'; | ||
import * as equal from 'fast-deep-equal'; | ||
import treeChanges from './index'; | ||
export function usePrevious(value) { | ||
@@ -12,4 +13,11 @@ var ref = useRef(); | ||
var previousValue = usePrevious(value); | ||
return useMemo(function () { return treeChanges(previousValue || value, value); }, [previousValue, value]); | ||
var isEqual = equal(previousValue || value, value); | ||
var isEqualRef = useRef(isEqual); | ||
var instance = useRef(treeChanges(previousValue || value, value)); | ||
if (isEqualRef.current !== isEqual || !isEqual) { | ||
isEqualRef.current = isEqual; | ||
instance.current = treeChanges(previousValue || value, value); | ||
} | ||
return instance.current; | ||
} | ||
//# sourceMappingURL=hook.js.map |
@@ -1,2 +0,2 @@ | ||
import treeChanges from './base'; | ||
export default treeChanges; | ||
import { Data, TreeChanges } from './types'; | ||
export default function treeChanges(previousData: Data, data: Data): TreeChanges; |
141
esm/index.js
@@ -1,3 +0,140 @@ | ||
import treeChanges from './base'; | ||
export default treeChanges; | ||
import * as equal from 'fast-deep-equal'; | ||
import { compareNumbers, compareValues, getIterables, getDataValues, includesOrEqualsTo, isArray, isCompatible, isDefined, isPlainObject, nested, } from './helpers'; | ||
export default function treeChanges(previousData, data) { | ||
if (!previousData || !data) { | ||
throw new Error('Missing required parameters'); | ||
} | ||
var added = function (key, value) { | ||
try { | ||
// skip if immediate parent isn't an array or object | ||
if (!isCompatible(previousData, data, { key: key })) { | ||
return false; | ||
} | ||
var _a = getDataValues(previousData, data, { filter: true, key: key }), left = _a[0], right = _a[1]; | ||
if (value) { | ||
// skip if key exists in the narrowed data | ||
if (key && left[key] && right[key]) { | ||
return false; | ||
} | ||
return compareValues(left, right, value); | ||
} | ||
if (key && !(key in left) && key in right) { | ||
return true; | ||
} | ||
var _b = getIterables(previousData, data, { | ||
key: key, | ||
}), leftIterable = _b[0], rightIterable = _b[1]; | ||
return leftIterable.length < rightIterable.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var changed = function (key, actual, previous) { | ||
var left = nested(previousData, key); | ||
var right = nested(data, key); | ||
var hasActual = isDefined(actual); | ||
var hasPrevious = isDefined(previous); | ||
if (hasActual && !hasPrevious) { | ||
var leftComparator = isArray(actual) ? actual.indexOf(left) < 0 : left !== actual; | ||
var rightComparator = isArray(actual) ? actual.indexOf(right) >= 0 : right === actual; | ||
return leftComparator && rightComparator; | ||
} | ||
if (hasPrevious) { | ||
var leftComparator = includesOrEqualsTo(previous, left); | ||
var rightComparator = includesOrEqualsTo(actual, right); | ||
return leftComparator && rightComparator; | ||
} | ||
if ([left, right].every(isArray) || [left, right].every(isPlainObject)) { | ||
return !equal(left, right); | ||
} | ||
return left !== right; | ||
}; | ||
var changedFrom = function (key, previous, actual) { | ||
if (!isDefined(key)) { | ||
return false; | ||
} | ||
var left = nested(previousData, key); | ||
var right = nested(data, key); | ||
var hasActual = isDefined(actual); | ||
var leftComparator = includesOrEqualsTo(previous, left); | ||
var rightComparator = includesOrEqualsTo(actual, right); | ||
return leftComparator && (hasActual ? rightComparator : !hasActual); | ||
}; | ||
/** | ||
* @deprecated | ||
*/ | ||
var changedTo = function (key, actual) { | ||
if (!isDefined(key)) { | ||
return false; | ||
} | ||
/* istanbul ignore next */ | ||
if (process.env.NODE_ENV === 'development') { | ||
// eslint-disable-next-line no-console | ||
console.warn('`changedTo` is deprecated! Replace it with `change`'); | ||
} | ||
return changed(key, actual); | ||
}; | ||
var decreased = function (key, actual, previous) { | ||
if (!isDefined(key)) { | ||
return false; | ||
} | ||
return compareNumbers(previousData, data, 'decreased', { key: key, actual: actual, previous: previous }); | ||
}; | ||
var emptied = function (key) { | ||
try { | ||
var _a = getIterables(previousData, data, { includeStrings: true, key: key }), left = _a[0], right = _a[1]; | ||
return !!left.length && !right.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var filled = function (key) { | ||
try { | ||
var _a = getIterables(previousData, data, { | ||
filter: false, | ||
includeStrings: true, | ||
key: key, | ||
}), left = _a[0], right = _a[1]; | ||
return !left.length && !!right.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var increased = function (key, actual, previous) { | ||
if (!isDefined(key)) { | ||
return false; | ||
} | ||
return compareNumbers(previousData, data, 'increased', { key: key, actual: actual, previous: previous }); | ||
}; | ||
var removed = function (key, value) { | ||
try { | ||
// skip if immediate parent isn't an array or object | ||
if (!isCompatible(previousData, data, { key: key })) { | ||
return false; | ||
} | ||
var _a = getDataValues(previousData, data, { filter: true, key: key }), left = _a[0], right = _a[1]; | ||
if (value) { | ||
// skip if key exists in the narrowed data | ||
if (key && left[key] && right[key]) { | ||
return false; | ||
} | ||
return compareValues(right, left, value); | ||
} | ||
if (key && key in left && !(key in right)) { | ||
return true; | ||
} | ||
var _b = getIterables(data, previousData, { | ||
key: key, | ||
}), leftIterable = _b[0], rightIterable = _b[1]; | ||
return leftIterable.length < rightIterable.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
return { added: added, changed: changed, changedFrom: changedFrom, changedTo: changedTo, decreased: decreased, emptied: emptied, filled: filled, increased: increased, removed: removed }; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
export {}; | ||
//# sourceMappingURL=types.js.map |
@@ -13,2 +13,3 @@ import { Data, Key, Params, PlainObject, Value } from './types'; | ||
}; | ||
export declare function includesOrEqualsTo<T>(previousValue: T | T[], value: T): boolean; | ||
export declare function isArray(value: unknown): value is Array<typeof value>; | ||
@@ -21,2 +22,2 @@ export declare function isCompatible(prevData: any, data: any, options?: Partial<Params>): boolean; | ||
export declare function isSameType(...args: any): boolean; | ||
export declare function nested(input: PlainObject | any[], property?: Key): any; | ||
export declare function nested(object: PlainObject, property?: Key): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.nested = exports.isSameType = exports.isString = exports.isPlainObject = exports.isNumber = exports.isDefined = exports.isCompatible = exports.isArray = exports.includesOrEqualsTo = exports.getKeys = exports.getDataValues = exports.getIterables = exports.compareValues = exports.compareNumbers = exports.checkItems = exports.checkEntries = exports.canHaveLength = void 0; | ||
var equal = require("fast-deep-equal"); | ||
@@ -120,2 +121,6 @@ function canHaveLength() { | ||
exports.getKeys = getKeys; | ||
function includesOrEqualsTo(previousValue, value) { | ||
return isArray(previousValue) ? previousValue.includes(value) : value === previousValue; | ||
} | ||
exports.includesOrEqualsTo = includesOrEqualsTo; | ||
function isArray(value) { | ||
@@ -172,19 +177,18 @@ return Array.isArray(value); | ||
exports.isSameType = isSameType; | ||
function nested(input, property) { | ||
function nested(object, property) { | ||
/* istanbul ignore else */ | ||
if (isPlainObject(input) || isArray(input)) { | ||
if (isPlainObject(object) || isArray(object)) { | ||
/* istanbul ignore else */ | ||
if (isString(property) && property !== '') { | ||
var split = property.split('.'); | ||
// @ts-ignore | ||
return split.reduce(function (obj, prop) { return obj && obj[prop]; }, input); | ||
if (isString(property) && property) { | ||
var props = property.split('.'); | ||
return props.reduce(function (acc, d) { return acc && acc[d]; }, object); | ||
} | ||
if (isNumber(property) && isArray(input)) { | ||
return input[property]; | ||
if (isNumber(property)) { | ||
return object[property]; | ||
} | ||
return input; | ||
return object; | ||
} | ||
return input; | ||
return object; | ||
} | ||
exports.nested = nested; | ||
//# sourceMappingURL=helpers.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.usePrevious = void 0; | ||
var react_1 = require("react"); | ||
var base_1 = require("./base"); | ||
var equal = require("fast-deep-equal"); | ||
var index_1 = require("./index"); | ||
function usePrevious(value) { | ||
@@ -15,5 +17,12 @@ var ref = react_1.useRef(); | ||
var previousValue = usePrevious(value); | ||
return react_1.useMemo(function () { return base_1.default(previousValue || value, value); }, [previousValue, value]); | ||
var isEqual = equal(previousValue || value, value); | ||
var isEqualRef = react_1.useRef(isEqual); | ||
var instance = react_1.useRef(index_1.default(previousValue || value, value)); | ||
if (isEqualRef.current !== isEqual || !isEqual) { | ||
isEqualRef.current = isEqual; | ||
instance.current = index_1.default(previousValue || value, value); | ||
} | ||
return instance.current; | ||
} | ||
exports.default = useTreeChanges; | ||
//# sourceMappingURL=hook.js.map |
@@ -1,2 +0,2 @@ | ||
import treeChanges from './base'; | ||
export default treeChanges; | ||
import { Data, TreeChanges } from './types'; | ||
export default function treeChanges(previousData: Data, data: Data): TreeChanges; |
142
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var base_1 = require("./base"); | ||
exports.default = base_1.default; | ||
var equal = require("fast-deep-equal"); | ||
var helpers_1 = require("./helpers"); | ||
function treeChanges(previousData, data) { | ||
if (!previousData || !data) { | ||
throw new Error('Missing required parameters'); | ||
} | ||
var added = function (key, value) { | ||
try { | ||
// skip if immediate parent isn't an array or object | ||
if (!helpers_1.isCompatible(previousData, data, { key: key })) { | ||
return false; | ||
} | ||
var _a = helpers_1.getDataValues(previousData, data, { filter: true, key: key }), left = _a[0], right = _a[1]; | ||
if (value) { | ||
// skip if key exists in the narrowed data | ||
if (key && left[key] && right[key]) { | ||
return false; | ||
} | ||
return helpers_1.compareValues(left, right, value); | ||
} | ||
if (key && !(key in left) && key in right) { | ||
return true; | ||
} | ||
var _b = helpers_1.getIterables(previousData, data, { | ||
key: key, | ||
}), leftIterable = _b[0], rightIterable = _b[1]; | ||
return leftIterable.length < rightIterable.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var changed = function (key, actual, previous) { | ||
var left = helpers_1.nested(previousData, key); | ||
var right = helpers_1.nested(data, key); | ||
var hasActual = helpers_1.isDefined(actual); | ||
var hasPrevious = helpers_1.isDefined(previous); | ||
if (hasActual && !hasPrevious) { | ||
var leftComparator = helpers_1.isArray(actual) ? actual.indexOf(left) < 0 : left !== actual; | ||
var rightComparator = helpers_1.isArray(actual) ? actual.indexOf(right) >= 0 : right === actual; | ||
return leftComparator && rightComparator; | ||
} | ||
if (hasPrevious) { | ||
var leftComparator = helpers_1.includesOrEqualsTo(previous, left); | ||
var rightComparator = helpers_1.includesOrEqualsTo(actual, right); | ||
return leftComparator && rightComparator; | ||
} | ||
if ([left, right].every(helpers_1.isArray) || [left, right].every(helpers_1.isPlainObject)) { | ||
return !equal(left, right); | ||
} | ||
return left !== right; | ||
}; | ||
var changedFrom = function (key, previous, actual) { | ||
if (!helpers_1.isDefined(key)) { | ||
return false; | ||
} | ||
var left = helpers_1.nested(previousData, key); | ||
var right = helpers_1.nested(data, key); | ||
var hasActual = helpers_1.isDefined(actual); | ||
var leftComparator = helpers_1.includesOrEqualsTo(previous, left); | ||
var rightComparator = helpers_1.includesOrEqualsTo(actual, right); | ||
return leftComparator && (hasActual ? rightComparator : !hasActual); | ||
}; | ||
/** | ||
* @deprecated | ||
*/ | ||
var changedTo = function (key, actual) { | ||
if (!helpers_1.isDefined(key)) { | ||
return false; | ||
} | ||
/* istanbul ignore next */ | ||
if (process.env.NODE_ENV === 'development') { | ||
// eslint-disable-next-line no-console | ||
console.warn('`changedTo` is deprecated! Replace it with `change`'); | ||
} | ||
return changed(key, actual); | ||
}; | ||
var decreased = function (key, actual, previous) { | ||
if (!helpers_1.isDefined(key)) { | ||
return false; | ||
} | ||
return helpers_1.compareNumbers(previousData, data, 'decreased', { key: key, actual: actual, previous: previous }); | ||
}; | ||
var emptied = function (key) { | ||
try { | ||
var _a = helpers_1.getIterables(previousData, data, { includeStrings: true, key: key }), left = _a[0], right = _a[1]; | ||
return !!left.length && !right.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var filled = function (key) { | ||
try { | ||
var _a = helpers_1.getIterables(previousData, data, { | ||
filter: false, | ||
includeStrings: true, | ||
key: key, | ||
}), left = _a[0], right = _a[1]; | ||
return !left.length && !!right.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
var increased = function (key, actual, previous) { | ||
if (!helpers_1.isDefined(key)) { | ||
return false; | ||
} | ||
return helpers_1.compareNumbers(previousData, data, 'increased', { key: key, actual: actual, previous: previous }); | ||
}; | ||
var removed = function (key, value) { | ||
try { | ||
// skip if immediate parent isn't an array or object | ||
if (!helpers_1.isCompatible(previousData, data, { key: key })) { | ||
return false; | ||
} | ||
var _a = helpers_1.getDataValues(previousData, data, { filter: true, key: key }), left = _a[0], right = _a[1]; | ||
if (value) { | ||
// skip if key exists in the narrowed data | ||
if (key && left[key] && right[key]) { | ||
return false; | ||
} | ||
return helpers_1.compareValues(right, left, value); | ||
} | ||
if (key && key in left && !(key in right)) { | ||
return true; | ||
} | ||
var _b = helpers_1.getIterables(data, previousData, { | ||
key: key, | ||
}), leftIterable = _b[0], rightIterable = _b[1]; | ||
return leftIterable.length < rightIterable.length; | ||
} | ||
catch (error) { | ||
return false; | ||
} | ||
}; | ||
return { added: added, changed: changed, changedFrom: changedFrom, changedTo: changedTo, decreased: decreased, emptied: emptied, filled: filled, increased: increased, removed: removed }; | ||
} | ||
exports.default = treeChanges; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "tree-changes", | ||
"version": "0.6.0-2", | ||
"version": "0.6.0-3", | ||
"description": "Get changes between two versions of data with similar shape", | ||
"author": "Gil Barbara <gilbarbara@gmail.com>", | ||
"keywords": [ | ||
"comparison", | ||
"tree" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
@@ -18,44 +23,40 @@ "type": "git", | ||
"esm", | ||
"lib" | ||
"lib", | ||
"src" | ||
], | ||
"types": "./lib", | ||
"types": "esm", | ||
"sideEffects": false, | ||
"keywords": [ | ||
"comparison", | ||
"tree" | ||
], | ||
"license": "MIT", | ||
"optionalDependencies": { | ||
"react": "^16.8.0" | ||
"react": "^16.8.0 || ^17.0.0" | ||
}, | ||
"dependencies": { | ||
"fast-deep-equal": "^3.1.1" | ||
"fast-deep-equal": "^3.1.3" | ||
}, | ||
"devDependencies": { | ||
"@gilbarbara/tsconfig": "^0.1.0", | ||
"@size-limit/preset-small-lib": "^4.4.0", | ||
"@testing-library/jest-dom": "^5.1.1", | ||
"@testing-library/react": "^10.0.1", | ||
"@types/deep-diff": "^1.0.0", | ||
"@types/jest": "^25.1.4", | ||
"@types/node": "^13.9.1", | ||
"@types/react": "^16.9.23", | ||
"@typescript-eslint/eslint-plugin": "^2.23.0", | ||
"@typescript-eslint/parser": "^2.23.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"husky": "^4.2.3", | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1", | ||
"react": "^16.13.0", | ||
"react-dom": "^16.13.0", | ||
"@size-limit/preset-small-lib": "^4.9.1", | ||
"@testing-library/jest-dom": "^5.11.6", | ||
"@testing-library/react": "^11.2.2", | ||
"@types/jest": "^26.0.16", | ||
"@types/node": "^14.14.10", | ||
"@types/react": "^17.0.0", | ||
"@typescript-eslint/eslint-plugin": "^4.9.0", | ||
"@typescript-eslint/parser": "^4.9.0", | ||
"del-cli": "^3.0.1", | ||
"eslint": "^7.14.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-prettier": "^3.2.0", | ||
"husky": "^4.3.0", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.1", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"repo-tools": "^0.2.0", | ||
"rimraf": "^3.0.2", | ||
"size-limit": "^4.4.0", | ||
"ts-jest": "^25.2.1", | ||
"typescript": "^3.8.3" | ||
"size-limit": "^4.9.1", | ||
"ts-jest": "^26.4.4", | ||
"ts-node": "^9.1.0", | ||
"typescript": "^4.1.2" | ||
}, | ||
@@ -66,8 +67,9 @@ "scripts": { | ||
"build:esm": "tsc -m es6 --outDir esm", | ||
"watch": "npm run build:esm && npm run build:cjs -- -w", | ||
"clean": "rimraf lib && rimraf esm", | ||
"watch:cjs": "npm run build:cjs -- -w", | ||
"watch:esm": "npm run build:esm -- -w", | ||
"lint": "eslint --ext .ts,.tsx src test", | ||
"clean": "del lib/* && del esm/*", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage --bail", | ||
"test:watch": "jest --watch --verbose", | ||
"test:watch": "jest --watchAll --verbose", | ||
"format": "prettier \"**/*.{js,jsx,json,yml,yaml,css,less,scss,ts,tsx,md,graphql,mdx}\" --write", | ||
@@ -74,0 +76,0 @@ "validate": "npm run lint && npm run test:coverage && npm run build && npm run size", |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
79931
1171
31
+ Addedreact@17.0.2(transitive)
- Removedprop-types@15.8.1(transitive)
- Removedreact@16.14.0(transitive)
- Removedreact-is@16.13.1(transitive)
Updatedfast-deep-equal@^3.1.3