1 |
- {"version":3,"sources":["../src/index.ts","../src/helpers.ts"],"sourcesContent":["import equal from '@gilbarbara/deep-equal';\nimport is from 'is-lite';\n\nimport { compareNumbers, compareValues, getIterables, includesOrEqualsTo, nested } from './helpers';\nimport { Data, KeyType, TreeChanges, Value } from './types';\n\nexport default function treeChanges<P extends Data, D extends Data, K = KeyType<P, D>>(\n previousData: P,\n data: D,\n): TreeChanges<K> {\n if ([previousData, data].some(is.nullOrUndefined)) {\n throw new Error('Missing required parameters');\n }\n\n if (![previousData, data].every(d => is.plainObject(d) || is.array(d))) {\n throw new Error('Expected plain objects or array');\n }\n\n const added = (key?: K, value?: Value): boolean => {\n try {\n return compareValues<K>(previousData, data, { key, type: 'added', value });\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const changed = (key?: K | string, actual?: Value, previous?: Value): boolean => {\n try {\n const left = nested(previousData, key);\n const right = nested(data, key);\n const hasActual = is.defined(actual);\n const hasPrevious = is.defined(previous);\n\n if (hasActual || hasPrevious) {\n const leftComparator = hasPrevious\n ? includesOrEqualsTo(previous, left)\n : !includesOrEqualsTo(actual, left);\n const rightComparator = includesOrEqualsTo(actual, right);\n\n return leftComparator && rightComparator;\n }\n\n if ([left, right].every(is.array) || [left, right].every(is.plainObject)) {\n return !equal(left, right);\n }\n\n return left !== right;\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const changedFrom = (key: K | string, previous: Value, actual?: Value): boolean => {\n if (!is.defined(key)) {\n return false;\n }\n\n try {\n const left = nested(previousData, key);\n const right = nested(data, key);\n const hasActual = is.defined(actual);\n\n return (\n includesOrEqualsTo(previous, left) &&\n (hasActual ? includesOrEqualsTo(actual, right) : !hasActual)\n );\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const decreased = (key: K, actual?: Value, previous?: Value): boolean => {\n if (!is.defined(key)) {\n return false;\n }\n\n try {\n return compareNumbers<K>(previousData, data, { key, actual, previous, type: 'decreased' });\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const emptied = (key?: K): boolean => {\n try {\n const [left, right] = getIterables(previousData, data, { key });\n\n return !!left.length && !right.length;\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const filled = (key?: K): boolean => {\n try {\n const [left, right] = getIterables(previousData, data, { key });\n\n return !left.length && !!right.length;\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const increased = (key: K, actual?: Value, previous?: Value): boolean => {\n if (!is.defined(key)) {\n return false;\n }\n\n try {\n return compareNumbers<K>(previousData, data, { key, actual, previous, type: 'increased' });\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n const removed = (key?: K, value?: Value): boolean => {\n try {\n return compareValues<K>(previousData, data, { key, type: 'removed', value });\n } catch {\n /* istanbul ignore next */\n return false;\n }\n };\n\n return { added, changed, changedFrom, decreased, emptied, filled, increased, removed };\n}\n\nexport type { Data, KeyType, TreeChanges, Value } from './types';\n","import equal from '@gilbarbara/deep-equal';\nimport is from 'is-lite';\n\nimport { CompareValuesOptions, Data, Key, Options, ValidTypes, Value } from './types';\n\nexport function canHaveLength(...arguments_: any): boolean {\n return arguments_.every((d: unknown) => is.string(d) || is.array(d) || is.plainObject(d));\n}\n\nexport function checkEquality(left: Data, right: Data, value: Value) {\n if (!isSameType(left, right)) {\n return false;\n }\n\n if ([left, right].every(is.array)) {\n return !left.some(hasValue(value)) && right.some(hasValue(value));\n }\n\n /* istanbul ignore else */\n if ([left, right].every(is.plainObject)) {\n return (\n !Object.entries(left).some(hasEntry(value)) && Object.entries(right).some(hasEntry(value))\n );\n }\n\n return right === value;\n}\n\nexport function compareNumbers<K = Key>(\n previousData: Data,\n data: Data,\n options: Options<K>,\n): boolean {\n const { actual, key, previous, type } = options;\n const left = nested(previousData, key);\n const right = nested(data, key);\n\n let changed =\n [left, right].every(is.number) && (type === 'increased' ? left < right : left > right);\n\n if (!is.undefined(actual)) {\n changed = changed && right === actual;\n }\n\n if (!is.undefined(previous)) {\n changed = changed && left === previous;\n }\n\n return changed;\n}\n\nexport function compareValues<K = Key>(\n previousData: Data,\n data: Data,\n options: CompareValuesOptions<K>,\n) {\n const { key, type, value } = options;\n\n const left = nested(previousData, key);\n const right = nested(data, key);\n const primary = type === 'added' ? left : right;\n const secondary = type === 'added' ? right : left;\n\n if (!is.nullOrUndefined(value)) {\n if (is.defined(primary)) {\n // check if nested data matches\n if (is.array(primary) || is.plainObject(primary)) {\n return checkEquality(primary, secondary, value);\n }\n } else {\n return equal(secondary, value);\n }\n\n return false;\n }\n\n if ([left, right].every(is.array)) {\n return !secondary.every(isEqualPredicate(primary));\n }\n\n if ([left, right].every(is.plainObject)) {\n return hasExtraKeys(Object.keys(primary), Object.keys(secondary));\n }\n\n return (\n ![left, right].every(d => is.primitive(d) && is.defined(d)) &&\n (type === 'added'\n ? !is.defined(left) && is.defined(right)\n : is.defined(left) && !is.defined(right))\n );\n}\n\nexport function getIterables<K = Key>(previousData: Data, data: Data, { key }: Options<K> = {}) {\n let left = nested(previousData, key);\n let right = nested(data, key);\n\n if (!isSameType(left, right)) {\n throw new TypeError('Inputs have different types');\n }\n\n if (!canHaveLength(left, right)) {\n throw new TypeError(\"Inputs don't have length\");\n }\n\n if ([left, right].every(is.plainObject)) {\n left = Object.keys(left);\n right = Object.keys(right);\n }\n\n return [left, right];\n}\n\nexport function hasEntry(input: Value) {\n return ([key, value]: [string, Value]) => {\n if (is.array(input)) {\n return (\n equal(input, value) ||\n input.some(d => equal(d, value) || (is.array(value) && isEqualPredicate(value)(d)))\n );\n }\n\n /* istanbul ignore else */\n if (is.plainObject(input) && input[key]) {\n return !!input[key] && equal(input[key], value);\n }\n\n return equal(input, value);\n };\n}\n\nexport function hasExtraKeys(left: string[], right: string[]): boolean {\n return right.some(d => !left.includes(d));\n}\n\nexport function hasValue(input: Value) {\n return (value: Value) => {\n if (is.array(input)) {\n return input.some(d => equal(d, value) || (is.array(value) && isEqualPredicate(value)(d)));\n }\n\n return equal(input, value);\n };\n}\n\nexport function includesOrEqualsTo<T>(previousValue: T | T[], value: T): boolean {\n return is.array(previousValue)\n ? previousValue.some(d => equal(d, value))\n : equal(previousValue, value);\n}\n\nexport function isEqualPredicate(data: unknown[]) {\n return (value: unknown) => data.some(d => equal(d, value));\n}\n\nexport function isSameType(...arguments_: ValidTypes[]): boolean {\n return (\n arguments_.every(is.array) ||\n arguments_.every(is.number) ||\n arguments_.every(is.plainObject) ||\n arguments_.every(is.string)\n );\n}\n\nexport function nested<T extends Data, K = Key>(data: T, property?: K) {\n /* istanbul ignore else */\n if (is.plainObject(data) || is.array(data)) {\n /* istanbul ignore else */\n if (is.string(property)) {\n const props: Array<any> = property.split('.');\n\n return props.reduce((acc, d) => acc && acc[d], data);\n }\n\n /* istanbul ignore else */\n if (is.number(property)) {\n return data[property];\n }\n\n return data;\n }\n\n return data;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,qBAAkB;AAClB,IAAAC,kBAAe;;;ACDf,wBAAkB;AAClB,qBAAe;AAIR,SAAS,iBAAiB,YAA0B;AACzD,SAAO,WAAW,MAAM,CAAC,MAAe,eAAAC,QAAG,OAAO,CAAC,KAAK,eAAAA,QAAG,MAAM,CAAC,KAAK,eAAAA,QAAG,YAAY,CAAC,CAAC;AAC1F;AAEO,SAAS,cAAc,MAAY,OAAa,OAAc;AACnE,MAAI,CAAC,WAAW,MAAM,KAAK,GAAG;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,MAAM,KAAK,EAAE,MAAM,eAAAA,QAAG,KAAK,GAAG;AACjC,WAAO,CAAC,KAAK,KAAK,SAAS,KAAK,CAAC,KAAK,MAAM,KAAK,SAAS,KAAK,CAAC;AAAA,EAClE;AAGA,MAAI,CAAC,MAAM,KAAK,EAAE,MAAM,eAAAA,QAAG,WAAW,GAAG;AACvC,WACE,CAAC,OAAO,QAAQ,IAAI,EAAE,KAAK,SAAS,KAAK,CAAC,KAAK,OAAO,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