uniforms
Version:
Core package of uniforms.
42 lines (34 loc) • 1.05 kB
text/typescript
import isEqual from 'lodash/isEqual';
import { joinName } from './joinName';
import { UnknownObject } from './types';
function isObject(value: unknown): value is UnknownObject {
return !!value && value === Object(value) && !(value instanceof Date);
}
// eslint-disable-next-line complexity
export function changedKeys(root: string, valueA?: unknown, valueB?: unknown) {
if (!isObject(valueA) || (valueB && typeof valueA !== typeof valueB)) {
return isEqual(valueA, valueB) ? [] : [root];
}
const changed = [root];
if (isObject(valueB)) {
for (const key in valueA) {
if (!(key in valueB) || !isEqual(valueA[key], valueB[key])) {
changed.push(joinName(root, key));
}
}
for (const key in valueB) {
if (!(key in valueA)) {
changed.push(joinName(root, key));
}
}
if (changed.length === 1) {
changed.pop();
}
} else {
// eslint-disable-next-line guard-for-in
for (const key in valueA) {
changed.push(joinName(root, key));
}
}
return changed;
}