From 00dcb6469a0e9046c171a5a43e3b59046587333e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bert?= <63123542+m-bert@users.noreply.github.com> Date: Fri, 17 May 2024 11:28:39 +0200 Subject: [PATCH] Remove lodash (#2916) ## Description Since `lodash` is used only in one place in our codebase, we can simply replace it with our own implementation on `deepEqual`. Closes #2613 ## Test plan Run example apps. --------- Co-authored-by: Jakub Piasecki --- package.json | 1 - src/handlers/createHandler.tsx | 4 +--- src/utils.ts | 39 ++++++++++++++++++++++++++++++++++ yarn.lock | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 96f9b80bac..a692ccb3a5 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "@egjs/hammerjs": "^2.0.17", "hoist-non-react-statics": "^3.3.0", "invariant": "^2.2.4", - "lodash": "^4.17.21", "prop-types": "^15.7.2" }, "jest": { diff --git a/src/handlers/createHandler.tsx b/src/handlers/createHandler.tsx index 83bdefb048..9803d32943 100644 --- a/src/handlers/createHandler.tsx +++ b/src/handlers/createHandler.tsx @@ -6,8 +6,6 @@ import { EmitterSubscription, } from 'react-native'; import { customDirectEventTypes } from './customDirectEventTypes'; -// @ts-ignore - it isn't typed by TS & don't have definitelyTyped types -import deepEqual from 'lodash/isEqual'; import RNGestureHandlerModule from '../RNGestureHandlerModule'; import { State } from '../State'; import { @@ -25,7 +23,7 @@ import { scheduleFlushOperations, } from './gestureHandlerCommon'; import { ValueOf } from '../typeUtils'; -import { isFabric, isJestEnv, tagMessage } from '../utils'; +import { deepEqual, isFabric, isJestEnv, tagMessage } from '../utils'; import { ActionType } from '../ActionType'; import { PressabilityDebugView } from './PressabilityDebugView'; import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext'; diff --git a/src/utils.ts b/src/utils.ts index 6ca523c2da..6352d2135d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -59,3 +59,42 @@ export function isRemoteDebuggingEnabled(): boolean { !localGlobal.RN$Bridgeless ); } + +/** + * Recursively compares two objects for deep equality. + * + * **Note:** This function does not support cyclic references. + * + * @param obj1 - The first object to compare. + * @param obj2 - The second object to compare. + * @returns `true` if the objects are deeply equal, `false` otherwise. + */ +export function deepEqual(obj1: any, obj2: any) { + if (obj1 === obj2) { + return true; + } + + if ( + typeof obj1 !== 'object' || + typeof obj2 !== 'object' || + obj1 === null || + obj2 === null + ) { + return false; + } + + const keys1 = Object.keys(obj1); + const keys2 = Object.keys(obj2); + + if (keys1.length !== keys2.length) { + return false; + } + + for (const key of keys1) { + if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) { + return false; + } + } + + return true; +} diff --git a/yarn.lock b/yarn.lock index 9f4fb48677..09da07dab2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9046,7 +9046,7 @@ lodash@4.17.19: version "4.17.19" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b" -lodash@^4.17.11, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.5.0, lodash@^4.6.0, lodash@^4.6.1, lodash@^4.7.0: +lodash@^4.17.11, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.5.0, lodash@^4.6.0, lodash@^4.6.1, lodash@^4.7.0: version "4.17.21" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==