UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

78 lines (65 loc) 2.16 kB
/* eslint no-console: off */ import * as R from "ramda"; export const isParentFocusEqual = ( prevParentFocus: ParentFocus, nextParentFocus: ParentFocus ): boolean => { return ( prevParentFocus.nextFocusUp === nextParentFocus.nextFocusUp && prevParentFocus.nextFocusDown === nextParentFocus.nextFocusDown && prevParentFocus.nextFocusRight === nextParentFocus.nextFocusRight && prevParentFocus.nextFocusLeft === nextParentFocus.nextFocusLeft ); }; export const isEqual = ({ byValue = [], byReference = [], byComparator = { comparator: R.always(true), keys: [] }, debug = false, compareRemainingKeysBySameValue = false, }: { byValue?: string[]; byReference?: string[]; byComparator?: { comparator: (a: any, b: any) => boolean; keys: string[] }; debug?: boolean; compareRemainingKeysBySameValue?: boolean; }) => (prev, next) => { const isEqualByValue = R.isEmpty(byValue) ? true : R.all((key) => R.equals(prev[key], next[key]), byValue); const isEqualByReference = R.isEmpty(byReference) ? true : R.all((key) => prev[key] === next[key], byReference); const isEqualByComparator = R.isEmpty(byComparator.keys) ? true : R.all( (key) => byComparator.comparator(prev[key], next[key]), byComparator.keys ); if (debug) { console.log({ isEqualByValue, isEqualByReference, isEqualByComparator }); } if (compareRemainingKeysBySameValue) { const allKeys = new Set([...Object.keys(prev), ...Object.keys(next)]); const declaredKeys = new Set([ ...byValue, ...byReference, ...byComparator.keys, ]); const undeclaredKeys = Array.from(allKeys).filter( (key) => !declaredKeys.has(key) ); const isRestKeysEqualAllBySameValue = undeclaredKeys.every((key) => Object.is(prev[key], next[key]) ); return ( isEqualByValue && isEqualByReference && isEqualByComparator && isRestKeysEqualAllBySameValue ); } return isEqualByValue && isEqualByReference && isEqualByComparator; };