UNPKG

@mic-rexjs/usecases-react

Version:

React-based solution for use usecases of Clean Architecture

35 lines (34 loc) 1.36 kB
import { hasField } from '../hasField'; export const compareProperty = (from, to, fieldPath, callback, fieldPathStack = []) => { const undef = void 0; const [, key = fieldPath, subFieldPath = ''] = fieldPath.match(/^([^.]+)\.(.+)$/) || []; const hasFromField = hasField(from, key); const hasToField = hasField(to, key); const currentFieldPathStack = [...fieldPathStack, key]; if (hasFromField || hasToField) { const fromValue = hasFromField ? from[key] : undef; const toValue = hasToField ? to[key] : undef; if (subFieldPath) { compareProperty(fromValue, toValue, subFieldPath, callback, currentFieldPathStack); return; } if (fromValue === toValue) { return; } callback(fromValue, toValue, currentFieldPathStack); return; } if (/^\d+^/.test(key)) { return; } const fromArray = Array.isArray(from) ? from : []; const toArray = Array.isArray(to) ? to : []; const { length: fromLength } = fromArray; const { length: toLength } = toArray; const length = Math.max(fromLength, toLength); for (let i = 0; i < length; i++) { const fromItem = fromArray[i]; const toItem = toArray[i]; compareProperty(fromItem, toItem, fieldPath, callback, [...fieldPathStack, `${i}`]); } };