New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tree-changes

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-changes - npm Package Compare versions

Comparing version 0.6.0-1 to 0.6.0-2

esm/base.d.ts

19

esm/helpers.d.ts

@@ -1,11 +0,20 @@

import { Data, Params, GenericObject, Comparator } from './types';
import { Data, Key, Params, PlainObject, Value } from './types';
export declare function canHaveLength(...args: any): boolean;
export declare function compareNumbers(data: Data, nextData: Data, type: string, { actual, key, previous }: Params): boolean;
export declare function getIterables(data: Data, nextData: Data, { exclude, key }: Partial<Params>): Comparator;
export declare function checkEntries(input: any): ([k, v]: [string, any]) => boolean;
export declare function checkItems(input: any): (value: any) => boolean;
export declare function compareNumbers(previousData: Data, data: Data, type: string, { actual, key, previous }: Params): boolean;
export declare function compareValues(left: Data, right: Data, value: Value): boolean;
export declare function getIterables(previousData: Data, data: Data, { filter, includeStrings, key }?: Partial<Params>): any[];
export declare function getDataValues(prevData: any, data: any, options?: Partial<Params>): any[];
export declare function getKeys(key: Key): {
parentKey: string | number;
childKey: string | number;
};
export declare function isArray(value: unknown): value is Array<typeof value>;
export declare function isCompatible(prevData: any, data: any, options?: Partial<Params>): boolean;
export declare function isDefined(value: unknown): boolean;
export declare function isNumber(value: unknown): value is number;
export declare function isPlainObj(value: unknown): value is GenericObject;
export declare function isPlainObject(value: unknown): value is PlainObject;
export declare function isString(value: unknown): value is string;
export declare function isSameType(...args: any): boolean;
export declare function nested(input: GenericObject, property: string | number | undefined): any;
export declare function nested(input: PlainObject | any[], property?: Key): any;

@@ -0,1 +1,2 @@

import * as equal from 'fast-deep-equal';
export function canHaveLength() {

@@ -7,9 +8,32 @@ var args = [];

return args.every(function (d) {
return isString(d) || isArray(d) || isPlainObj(d);
return isString(d) || isArray(d) || isPlainObject(d);
});
}
export function compareNumbers(data, nextData, type, _a) {
export function checkEntries(input) {
return function (_a) {
var k = _a[0], v = _a[1];
if (equal(input, v)) {
return true;
}
if (isArray(input)) {
return input.some(function (d) { return equal(d, v) || (isArray(v) && v.indexOf(d) >= 0); });
}
if (isPlainObject(input)) {
return !!input[k] && equal(input[k], v);
}
return equal(input, v);
};
}
export function checkItems(input) {
return function (value) {
if (isArray(input)) {
return input.some(function (d) { return equal(d, value) || (isArray(value) && value.indexOf(d) >= 0); });
}
return equal(input, value);
};
}
export function compareNumbers(previousData, data, type, _a) {
var actual = _a.actual, key = _a.key, previous = _a.previous;
var left = nested(data, key);
var right = nested(nextData, key);
var left = nested(previousData, key);
var right = nested(data, key);
var hasActual = typeof actual !== 'undefined';

@@ -26,7 +50,23 @@ var hasPrevious = typeof previous !== 'undefined';

}
export function getIterables(data, nextData, _a) {
var exclude = _a.exclude, key = _a.key;
var left = nested(data, key);
var right = nested(nextData, key);
export function compareValues(left, right, value) {
if (!isSameType(left, right)) {
return false;
}
if (!isDefined(left) && right) {
return true;
}
if (isArray(left) && isArray(right)) {
return !left.some(checkItems(value)) && right.some(checkItems(value));
}
/* istanbul ignore else */
if (isPlainObject(left) && isPlainObject(right)) {
return (!Object.entries(left).some(checkEntries(value)) &&
Object.entries(right).some(checkEntries(value)));
}
return right === value;
}
export function getIterables(previousData, data, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.filter, filter = _c === void 0 ? true : _c, includeStrings = _b.includeStrings, key = _b.key;
var _d = getDataValues(previousData, data, { filter: filter, key: key }), left = _d[0], right = _d[1];
if (!isSameType(left, right)) {
throw new TypeError('Inputs have different types');

@@ -37,6 +77,6 @@ }

}
if (exclude === 'string' && [left, right].every(isString)) {
if (!includeStrings && [left, right].every(isString)) {
throw new TypeError('Strings are excluded');
}
if ([left, right].every(isPlainObj)) {
if ([left, right].every(isPlainObject)) {
left = Object.keys(left);

@@ -47,5 +87,48 @@ right = Object.keys(right);

}
export function getDataValues(prevData, data, options) {
var _a = options || {}, filter = _a.filter, key = _a.key;
if (key) {
var leftWithKey = nested(prevData, key);
var rightWithKey = nested(data, key);
if (!filter ||
(isPlainObject(leftWithKey) && isPlainObject(rightWithKey)) ||
(isArray(leftWithKey) && isArray(rightWithKey))) {
return [leftWithKey, rightWithKey];
}
}
return [prevData, data];
}
export function getKeys(key) {
var keys = [];
if (isString(key)) {
keys = key.split('.');
}
else {
keys.push(key);
}
return {
parentKey: keys.slice(-2, 1)[0],
childKey: keys.slice(-1)[0],
};
}
export function isArray(value) {
return Array.isArray(value);
}
export function isCompatible(prevData, data, options) {
var _a = options || {}, _b = _a.filter, filter = _b === void 0 ? true : _b, includeStrings = _a.includeStrings, key = _a.key;
var upperKey;
if (key) {
var _c = getKeys(key), childKey = _c.childKey, parentKey = _c.parentKey;
upperKey = parentKey || childKey;
}
var _d = getDataValues(prevData, data, { filter: filter, key: upperKey }), left = _d[0], right = _d[1];
if (!isSameType(left, right)) {
return false;
}
var checks = [isPlainObject(left) && isPlainObject(right), isArray(left) && isArray(right)];
if (includeStrings) {
checks.push(isString(left) && isString(right));
}
return checks.some(function (d) { return d === true; });
}
export function isDefined(value) {

@@ -57,3 +140,3 @@ return typeof value !== 'undefined';

}
export function isPlainObj(value) {
export function isPlainObject(value) {
if (!value) {

@@ -74,11 +157,14 @@ return false;

}
return (args.every(isArray) || args.every(isNumber) || args.every(isPlainObj) || args.every(isString));
return (args.every(isArray) || args.every(isNumber) || args.every(isPlainObject) || args.every(isString));
}
export function nested(input, property) {
if (isPlainObj(input) || isArray(input)) {
/* istanbul ignore else */
if (isPlainObject(input) || isArray(input)) {
/* 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 (isNumber(property)) {
if (isNumber(property) && isArray(input)) {
return input[property];

@@ -85,0 +171,0 @@ }

@@ -1,3 +0,2 @@

import { TreeChangesFn } from './types';
export declare function usePrevious<T>(value: T): T | undefined;
export default function createTreeChangesHook(treeChanges: TreeChangesFn): (value: any) => import("./types").TreeChanges;
export default function useTreeChanges(value: any): import("./types").TreeChanges;

@@ -1,3 +0,3 @@

import { useEffect, useRef } from 'react';
import { diff } from 'deep-diff';
import { useEffect, useMemo, useRef } from 'react';
import treeChanges from './base';
export function usePrevious(value) {

@@ -10,14 +10,6 @@ var ref = useRef();

}
export default function createTreeChangesHook(treeChanges) {
return function useTreeChanges(value) {
var previousValue = usePrevious(value);
var ref = useRef(treeChanges(previousValue || value, value));
useEffect(function () {
if (previousValue && diff(previousValue, value)) {
ref.current = treeChanges(previousValue, value);
}
}, [previousValue, value]);
return ref.current;
};
export default function useTreeChanges(value) {
var previousValue = usePrevious(value);
return useMemo(function () { return treeChanges(previousValue || value, value); }, [previousValue, value]);
}
//# sourceMappingURL=hook.js.map

@@ -1,3 +0,2 @@

import { Data, TreeChanges } from './types';
export default function treeChanges(data: Data, nextData: Data): TreeChanges;
export declare const useTreeChanges: (value: any) => TreeChanges;
import treeChanges from './base';
export default treeChanges;

@@ -1,110 +0,3 @@

import * as equal from 'fast-deep-equal';
import { compareNumbers, getIterables, isArray, isDefined, isPlainObj, nested } from './helpers';
import createTreeChangesHook from './hook';
export default function treeChanges(data, nextData) {
if (!data || !nextData) {
throw new Error('Missing required parameters');
}
var added = function (key) {
try {
if (key && !(key in data) && key in nextData) {
return true;
}
var _a = getIterables(data, nextData, { key: key, exclude: 'string' }), left = _a[0], right = _a[1];
return left.length < right.length;
}
catch (error) {
return false;
}
};
var changed = function (key, actual, previous) {
var left = nested(data, key);
var right = nested(nextData, 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 = isArray(previous) ? previous.indexOf(left) >= 0 : left === previous;
var rightComparator = isArray(actual) ? actual.indexOf(right) >= 0 : right === actual;
return leftComparator && rightComparator;
}
if ([left, right].every(isArray) || [left, right].every(isPlainObj)) {
return !equal(left, right);
}
return left !== right;
};
var changedFrom = function (key, previous, actual) {
if (!isDefined(key)) {
return false;
}
var left = nested(data, key);
var right = nested(nextData, key);
var hasActual = isDefined(actual);
var leftComparator = isArray(previous) ? previous.indexOf(left) >= 0 : left === previous;
var rightComparator = isArray(actual) ? actual.indexOf(right) >= 0 : right === actual;
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(data, nextData, 'decreased', { key: key, actual: actual, previous: previous });
};
var emptied = function (key) {
try {
var _a = getIterables(data, nextData, { 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(data, nextData, { 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(data, nextData, 'increased', { key: key, actual: actual, previous: previous });
};
var removed = function (key) {
try {
if (key && key in data && !(key in nextData)) {
return true;
}
var _a = getIterables(data, nextData, { key: key, exclude: 'string' }), left = _a[0], right = _a[1];
return left.length > right.length;
}
catch (error) {
return false;
}
};
return { added: added, changed: changed, changedFrom: changedFrom, changedTo: changedTo, decreased: decreased, emptied: emptied, filled: filled, increased: increased, removed: removed };
}
export var useTreeChanges = createTreeChangesHook(treeChanges);
import treeChanges from './base';
export default treeChanges;
//# sourceMappingURL=index.js.map

@@ -1,7 +0,7 @@

export declare type AcceptedTypes = string | boolean | number | GenericObject;
export declare type Data = GenericObject | GenericObject[];
export declare type AcceptedTypes = string | boolean | number | PlainObject;
export declare type Comparator = Array<string | any[]>;
export declare type Data = PlainObject | AcceptedTypes[];
export declare type Key = string | number;
export declare type Value = AcceptedTypes | AcceptedTypes[];
export declare type Comparator = Array<string | any[]>;
export interface GenericObject {
export interface PlainObject {
[key: string]: any;

@@ -11,3 +11,4 @@ }

actual?: Value;
exclude?: string;
includeStrings?: boolean;
filter?: boolean;
key: Key;

@@ -18,3 +19,3 @@ previous?: Value;

export interface TreeChanges {
added: (key?: Key, compare?: any) => boolean;
added: (key?: Key, value?: Value) => boolean;
changed: (key?: Key, actual?: Value, previous?: Value) => boolean;

@@ -27,3 +28,3 @@ changedFrom: (key: Key, previous: Value, actual?: Value) => boolean;

increased: (key: Key, actual?: Value, previous?: Value) => boolean;
removed: (key?: Key) => boolean;
removed: (key?: Key, value?: Value) => boolean;
}

@@ -1,11 +0,20 @@

import { Data, Params, GenericObject, Comparator } from './types';
import { Data, Key, Params, PlainObject, Value } from './types';
export declare function canHaveLength(...args: any): boolean;
export declare function compareNumbers(data: Data, nextData: Data, type: string, { actual, key, previous }: Params): boolean;
export declare function getIterables(data: Data, nextData: Data, { exclude, key }: Partial<Params>): Comparator;
export declare function checkEntries(input: any): ([k, v]: [string, any]) => boolean;
export declare function checkItems(input: any): (value: any) => boolean;
export declare function compareNumbers(previousData: Data, data: Data, type: string, { actual, key, previous }: Params): boolean;
export declare function compareValues(left: Data, right: Data, value: Value): boolean;
export declare function getIterables(previousData: Data, data: Data, { filter, includeStrings, key }?: Partial<Params>): any[];
export declare function getDataValues(prevData: any, data: any, options?: Partial<Params>): any[];
export declare function getKeys(key: Key): {
parentKey: string | number;
childKey: string | number;
};
export declare function isArray(value: unknown): value is Array<typeof value>;
export declare function isCompatible(prevData: any, data: any, options?: Partial<Params>): boolean;
export declare function isDefined(value: unknown): boolean;
export declare function isNumber(value: unknown): value is number;
export declare function isPlainObj(value: unknown): value is GenericObject;
export declare function isPlainObject(value: unknown): value is PlainObject;
export declare function isString(value: unknown): value is string;
export declare function isSameType(...args: any): boolean;
export declare function nested(input: GenericObject, property: string | number | undefined): any;
export declare function nested(input: PlainObject | any[], property?: Key): any;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var equal = require("fast-deep-equal");
function canHaveLength() {

@@ -9,10 +10,35 @@ var args = [];

return args.every(function (d) {
return isString(d) || isArray(d) || isPlainObj(d);
return isString(d) || isArray(d) || isPlainObject(d);
});
}
exports.canHaveLength = canHaveLength;
function compareNumbers(data, nextData, type, _a) {
function checkEntries(input) {
return function (_a) {
var k = _a[0], v = _a[1];
if (equal(input, v)) {
return true;
}
if (isArray(input)) {
return input.some(function (d) { return equal(d, v) || (isArray(v) && v.indexOf(d) >= 0); });
}
if (isPlainObject(input)) {
return !!input[k] && equal(input[k], v);
}
return equal(input, v);
};
}
exports.checkEntries = checkEntries;
function checkItems(input) {
return function (value) {
if (isArray(input)) {
return input.some(function (d) { return equal(d, value) || (isArray(value) && value.indexOf(d) >= 0); });
}
return equal(input, value);
};
}
exports.checkItems = checkItems;
function compareNumbers(previousData, data, type, _a) {
var actual = _a.actual, key = _a.key, previous = _a.previous;
var left = nested(data, key);
var right = nested(nextData, key);
var left = nested(previousData, key);
var right = nested(data, key);
var hasActual = typeof actual !== 'undefined';

@@ -30,7 +56,24 @@ var hasPrevious = typeof previous !== 'undefined';

exports.compareNumbers = compareNumbers;
function getIterables(data, nextData, _a) {
var exclude = _a.exclude, key = _a.key;
var left = nested(data, key);
var right = nested(nextData, key);
function compareValues(left, right, value) {
if (!isSameType(left, right)) {
return false;
}
if (!isDefined(left) && right) {
return true;
}
if (isArray(left) && isArray(right)) {
return !left.some(checkItems(value)) && right.some(checkItems(value));
}
/* istanbul ignore else */
if (isPlainObject(left) && isPlainObject(right)) {
return (!Object.entries(left).some(checkEntries(value)) &&
Object.entries(right).some(checkEntries(value)));
}
return right === value;
}
exports.compareValues = compareValues;
function getIterables(previousData, data, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.filter, filter = _c === void 0 ? true : _c, includeStrings = _b.includeStrings, key = _b.key;
var _d = getDataValues(previousData, data, { filter: filter, key: key }), left = _d[0], right = _d[1];
if (!isSameType(left, right)) {
throw new TypeError('Inputs have different types');

@@ -41,6 +84,6 @@ }

}
if (exclude === 'string' && [left, right].every(isString)) {
if (!includeStrings && [left, right].every(isString)) {
throw new TypeError('Strings are excluded');
}
if ([left, right].every(isPlainObj)) {
if ([left, right].every(isPlainObject)) {
left = Object.keys(left);

@@ -52,2 +95,30 @@ right = Object.keys(right);

exports.getIterables = getIterables;
function getDataValues(prevData, data, options) {
var _a = options || {}, filter = _a.filter, key = _a.key;
if (key) {
var leftWithKey = nested(prevData, key);
var rightWithKey = nested(data, key);
if (!filter ||
(isPlainObject(leftWithKey) && isPlainObject(rightWithKey)) ||
(isArray(leftWithKey) && isArray(rightWithKey))) {
return [leftWithKey, rightWithKey];
}
}
return [prevData, data];
}
exports.getDataValues = getDataValues;
function getKeys(key) {
var keys = [];
if (isString(key)) {
keys = key.split('.');
}
else {
keys.push(key);
}
return {
parentKey: keys.slice(-2, 1)[0],
childKey: keys.slice(-1)[0],
};
}
exports.getKeys = getKeys;
function isArray(value) {

@@ -57,2 +128,20 @@ return Array.isArray(value);

exports.isArray = isArray;
function isCompatible(prevData, data, options) {
var _a = options || {}, _b = _a.filter, filter = _b === void 0 ? true : _b, includeStrings = _a.includeStrings, key = _a.key;
var upperKey;
if (key) {
var _c = getKeys(key), childKey = _c.childKey, parentKey = _c.parentKey;
upperKey = parentKey || childKey;
}
var _d = getDataValues(prevData, data, { filter: filter, key: upperKey }), left = _d[0], right = _d[1];
if (!isSameType(left, right)) {
return false;
}
var checks = [isPlainObject(left) && isPlainObject(right), isArray(left) && isArray(right)];
if (includeStrings) {
checks.push(isString(left) && isString(right));
}
return checks.some(function (d) { return d === true; });
}
exports.isCompatible = isCompatible;
function isDefined(value) {

@@ -66,3 +155,3 @@ return typeof value !== 'undefined';

exports.isNumber = isNumber;
function isPlainObj(value) {
function isPlainObject(value) {
if (!value) {

@@ -75,3 +164,3 @@ return false;

}
exports.isPlainObj = isPlainObj;
exports.isPlainObject = isPlainObject;
function isString(value) {

@@ -86,12 +175,15 @@ return typeof value === 'string';

}
return (args.every(isArray) || args.every(isNumber) || args.every(isPlainObj) || args.every(isString));
return (args.every(isArray) || args.every(isNumber) || args.every(isPlainObject) || args.every(isString));
}
exports.isSameType = isSameType;
function nested(input, property) {
if (isPlainObj(input) || isArray(input)) {
/* istanbul ignore else */
if (isPlainObject(input) || isArray(input)) {
/* 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 (isNumber(property)) {
if (isNumber(property) && isArray(input)) {
return input[property];

@@ -98,0 +190,0 @@ }

@@ -1,3 +0,2 @@

import { TreeChangesFn } from './types';
export declare function usePrevious<T>(value: T): T | undefined;
export default function createTreeChangesHook(treeChanges: TreeChangesFn): (value: any) => import("./types").TreeChanges;
export default function useTreeChanges(value: any): import("./types").TreeChanges;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var deep_diff_1 = require("deep-diff");
var base_1 = require("./base");
function usePrevious(value) {

@@ -13,15 +13,7 @@ var ref = react_1.useRef();

exports.usePrevious = usePrevious;
function createTreeChangesHook(treeChanges) {
return function useTreeChanges(value) {
var previousValue = usePrevious(value);
var ref = react_1.useRef(treeChanges(previousValue || value, value));
react_1.useEffect(function () {
if (previousValue && deep_diff_1.diff(previousValue, value)) {
ref.current = treeChanges(previousValue, value);
}
}, [previousValue, value]);
return ref.current;
};
function useTreeChanges(value) {
var previousValue = usePrevious(value);
return react_1.useMemo(function () { return base_1.default(previousValue || value, value); }, [previousValue, value]);
}
exports.default = createTreeChangesHook;
exports.default = useTreeChanges;
//# sourceMappingURL=hook.js.map

@@ -1,3 +0,2 @@

import { Data, TreeChanges } from './types';
export default function treeChanges(data: Data, nextData: Data): TreeChanges;
export declare const useTreeChanges: (value: any) => TreeChanges;
import treeChanges from './base';
export default treeChanges;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var equal = require("fast-deep-equal");
var helpers_1 = require("./helpers");
var hook_1 = require("./hook");
function treeChanges(data, nextData) {
if (!data || !nextData) {
throw new Error('Missing required parameters');
}
var added = function (key) {
try {
if (key && !(key in data) && key in nextData) {
return true;
}
var _a = helpers_1.getIterables(data, nextData, { key: key, exclude: 'string' }), left = _a[0], right = _a[1];
return left.length < right.length;
}
catch (error) {
return false;
}
};
var changed = function (key, actual, previous) {
var left = helpers_1.nested(data, key);
var right = helpers_1.nested(nextData, 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.isArray(previous) ? previous.indexOf(left) >= 0 : left === previous;
var rightComparator = helpers_1.isArray(actual) ? actual.indexOf(right) >= 0 : right === actual;
return leftComparator && rightComparator;
}
if ([left, right].every(helpers_1.isArray) || [left, right].every(helpers_1.isPlainObj)) {
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(data, key);
var right = helpers_1.nested(nextData, key);
var hasActual = helpers_1.isDefined(actual);
var leftComparator = helpers_1.isArray(previous) ? previous.indexOf(left) >= 0 : left === previous;
var rightComparator = helpers_1.isArray(actual) ? actual.indexOf(right) >= 0 : right === actual;
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(data, nextData, 'decreased', { key: key, actual: actual, previous: previous });
};
var emptied = function (key) {
try {
var _a = helpers_1.getIterables(data, nextData, { 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(data, nextData, { 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(data, nextData, 'increased', { key: key, actual: actual, previous: previous });
};
var removed = function (key) {
try {
if (key && key in data && !(key in nextData)) {
return true;
}
var _a = helpers_1.getIterables(data, nextData, { key: key, exclude: 'string' }), left = _a[0], right = _a[1];
return left.length > right.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;
exports.useTreeChanges = hook_1.default(treeChanges);
var base_1 = require("./base");
exports.default = base_1.default;
//# sourceMappingURL=index.js.map

@@ -1,7 +0,7 @@

export declare type AcceptedTypes = string | boolean | number | GenericObject;
export declare type Data = GenericObject | GenericObject[];
export declare type AcceptedTypes = string | boolean | number | PlainObject;
export declare type Comparator = Array<string | any[]>;
export declare type Data = PlainObject | AcceptedTypes[];
export declare type Key = string | number;
export declare type Value = AcceptedTypes | AcceptedTypes[];
export declare type Comparator = Array<string | any[]>;
export interface GenericObject {
export interface PlainObject {
[key: string]: any;

@@ -11,3 +11,4 @@ }

actual?: Value;
exclude?: string;
includeStrings?: boolean;
filter?: boolean;
key: Key;

@@ -18,3 +19,3 @@ previous?: Value;

export interface TreeChanges {
added: (key?: Key, compare?: any) => boolean;
added: (key?: Key, value?: Value) => boolean;
changed: (key?: Key, actual?: Value, previous?: Value) => boolean;

@@ -27,3 +28,3 @@ changedFrom: (key: Key, previous: Value, actual?: Value) => boolean;

increased: (key: Key, actual?: Value, previous?: Value) => boolean;
removed: (key?: Key) => boolean;
removed: (key?: Key, value?: Value) => boolean;
}
{
"name": "tree-changes",
"version": "0.6.0-1",
"version": "0.6.0-2",
"description": "Get changes between two versions of data with similar shape",

@@ -27,7 +27,6 @@ "author": "Gil Barbara <gilbarbara@gmail.com>",

"license": "MIT",
"peerDependencies": {
"optionalDependencies": {
"react": "^16.8.0"
},
"dependencies": {
"deep-diff": "^1.0.2",
"fast-deep-equal": "^3.1.1"

@@ -37,13 +36,13 @@ },

"@gilbarbara/tsconfig": "^0.1.0",
"@size-limit/preset-small-lib": "^4.3.1",
"@size-limit/preset-small-lib": "^4.4.0",
"@testing-library/jest-dom": "^5.1.1",
"@testing-library/react": "^9.5.0",
"@testing-library/react": "^10.0.1",
"@types/deep-diff": "^1.0.0",
"@types/jest": "^25.1.4",
"@types/node": "^13.9.0",
"@types/node": "^13.9.1",
"@types/react": "^16.9.23",
"@typescript-eslint/eslint-plugin": "^2.22.0",
"@typescript-eslint/parser": "^2.22.0",
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.10.0",

@@ -60,3 +59,3 @@ "eslint-plugin-import": "^2.20.1",

"rimraf": "^3.0.2",
"size-limit": "^4.3.1",
"size-limit": "^4.4.0",
"ts-jest": "^25.2.1",

@@ -90,7 +89,15 @@ "typescript": "^3.8.3"

"path": "./lib/index.js",
"limit": "3 kB"
"limit": "2 kB"
},
{
"path": "./esm/index.js",
"limit": "3 kB"
"limit": "2 kB"
},
{
"path": "./lib/hook.js",
"limit": "5 kB"
},
{
"path": "./esm/hook.js",
"limit": "5 kB"
}

@@ -97,0 +104,0 @@ ],

@@ -19,12 +19,4 @@ tree-changes

const savedData = {
actions: {},
data: { a: 1 },
const previousData = {
hasData: false,
items: [{ name: 'test' }],
messages: [],
missing: 'username',
pristine: true,
ratio: 0.9,
retries: 0,
sort: {

@@ -34,15 +26,6 @@ data: [{ type: 'asc' }, { type: 'desc' }],

},
switch: false,
username: '',
};
const newData = {
actions: { complete: true },
data: {},
hasData: true,
items: [],
messages: ['New Message'],
missing: '',
ratio: 0.5,
retries: 1,
sort: {

@@ -52,16 +35,5 @@ data: [{ type: 'desc' }, { type: 'asc' }],

},
sudo: true,
username: 'John',
};
const {
added,
changed,
changedFrom,
decreased,
emptied,
filled,
increased,
removed,
} = treeChanges(savedData, newData);
const { changed, changedFrom } = treeChanges(previousData, newData);

@@ -73,3 +45,2 @@ changed(); // true

changed('hasData', true, false); // true
changed('actions', { complete: true }, {})

@@ -79,31 +50,4 @@ // support nested match

changedFrom('hasData', false); // true
changedFrom('hasData', false, true); // true
changedFrom('retries', 0); // true
changedFrom('retries', 0, 1); // true
// works with array values too
changedFrom('sort.status', 'idle', ['done', 'success']); // true
added('actions'); // true
added('messages'); // true
added('sudo'); // true
removed(); // true
removed('data'); // true
removed('items'); // true
removed('switch'); // true
filled('actions'); // true
filled('messages'); // true
filled('username'); // true
emptied('data'); // true
emptied('items'); // true
emptied('missing'); // true
decreased('ratio'); // true
increased('retries'); // true
```

@@ -124,9 +68,31 @@

> It uses [deep-diff](https://github.com/flitbit/diff) to compare objects/arrays and [nested-property](https://github.com/cosmosio/nested-property) to get the nested key.
> It uses [fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal) to compare properties.
## API
**added**(`key: Key`)
Check if something was added to the data (length has increased). Works with arrays and objects (using Object.keys).
**added**(`key: Key`, `value?: Value`)
Check if something was added to the data.
Works with arrays and objects (using Object.keys).
```js
import treeChanges from 'tree-changes';
const previousData = {
actions: {},
messages: [],
};
const newData = {
actions: { complete: true },
messages: ['New Message'],
sudo: true,
};
const { added } = treeChanges(previousData, newData);
added('actions'); // true
added('messages'); // true
added('sudo'); // true
```
**changed**(`key?: Key`, `actual?: Value`, `previous?: Value`)

@@ -143,8 +109,71 @@ Check if the data has changed.

```js
import treeChanges from 'tree-changes';
const previousData = {
ratio: 0.9,
retries: 0,
};
const newData = {
ratio: 0.5,
retries: 1,
};
const { decreased } = treeChanges(previousData, newData);
decreased('ratio'); // true
decreased('retries'); // false
```
**emptied**(`key: Key`)
Check if the data was emptied. Works with arrays, objects and strings.
```js
import treeChanges from 'tree-changes';
const previousData = {
data: { a: 1 },
items: [{ name: 'test' }],
missing: 'username',
};
const newData = {
data: {},
items: [],
missing: '',
};
const { emptied } = treeChanges(previousData, newData);
emptied('data'); // true
emptied('items'); // true
emptied('missing'); // true
```
**filled**(`key: Key`)
Check if the data was filled (from a previous empty value). Works with arrays, objects and strings.
```js
import treeChanges from 'tree-changes';
const previousData = {
actions: {},
messages: [],
username: '',
};
const newData = {
actions: { complete: true },
messages: ['New Message'],
username: 'John',
};
const { filled } = treeChanges(previousData, newData);
filled('actions'); // true
filled('messages'); // true
filled('username'); // true
```
**increased**(`key: Key`, `actual?: Value`, `previous?: Value`)

@@ -154,5 +183,47 @@ Check if both values are numbers and the value has increased.

**removed**(`key: Key`)
Check if something was removed from the data (length has decreased). Works with arrays and objects (using Object.keys).
```js
import treeChanges from 'tree-changes';
const previousData = {
ratio: 0.9,
retries: 0,
};
const newData = {
ratio: 0.5,
retries: 1,
};
const { increased } = treeChanges(previousData, newData);
increased('retries'); // true
increased('ratio'); // false
```
**removed**(`key: Key`, `value?: Value`)
Check if something was removed from the data.
Works with arrays and objects (using Object.keys).
```js
import treeChanges from 'tree-changes';
const previousData = {
data: { a: 1 },
items: [{ name: 'test' }],
switch: false,
};
const newData = {
data: {},
items: [],
};
const { removed } = treeChanges(previousData, newData);
removed(); // true
removed('data'); // true
removed('items'); // true
removed('switch'); // true
```
> **Types**

@@ -159,0 +230,0 @@ > type Key = string | number;

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc