Skip to content

Commit 4e2f993

Browse files
authored
chore: use dequal instead of lodash.isequal (#1626)
* chore: use dequal instead of lodash.isequal * add a reference
1 parent c596263 commit 4e2f993

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

packages/cli/src/commands/join.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'path';
22
import { red, blue, yellow, green } from 'colorette';
33
import { performance } from 'perf_hooks';
4-
const isEqual = require('lodash.isequal');
54
import {
65
Config,
76
SpecVersion,
@@ -12,6 +11,7 @@ import {
1211
bundleDocument,
1312
isRef,
1413
} from '@redocly/openapi-core';
14+
import { dequal } from '@redocly/openapi-core/lib/utils';
1515
import {
1616
getFallbackApisOrExit,
1717
printExecutionTime,
@@ -449,7 +449,7 @@ export async function handleJoin(argv: JoinOptions, config: Config, packageVersi
449449
// Compare properties only if both are reference objects
450450
if (!isRef(pathParameter) && !isRef(parameter)) {
451451
if (pathParameter.name === parameter.name && pathParameter.in === parameter.in) {
452-
if (!isEqual(pathParameter.schema, parameter.schema)) {
452+
if (!dequal(pathParameter.schema, parameter.schema)) {
453453
exitWithError(`Different parameter schemas for (${parameter.name}) in ${path}.`);
454454
}
455455
isFoundParameter = true;
@@ -535,7 +535,7 @@ export async function handleJoin(argv: JoinOptions, config: Config, packageVersi
535535

536536
function isServersEqual(serverOne: Oas3Server, serverTwo: Oas3Server) {
537537
if (serverOne.description === serverTwo.description) {
538-
return isEqual(serverOne.variables, serverTwo.variables);
538+
return dequal(serverOne.variables, serverTwo.variables);
539539
}
540540

541541
return false;
@@ -636,7 +636,7 @@ export async function handleJoin(argv: JoinOptions, config: Config, packageVersi
636636
}
637637

638638
function doesComponentsDiffer(curr: object, next: object) {
639-
return !isEqual(Object.values(curr)[0], Object.values(next)[0]);
639+
return !dequal(Object.values(curr)[0], Object.values(next)[0]);
640640
}
641641

642642
function validateComponentsDifference(files: any) {

packages/cli/src/commands/split/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { red, blue, yellow, green } from 'colorette';
22
import * as fs from 'fs';
33
import { parseYaml, slash, isRef, isTruthy } from '@redocly/openapi-core';
4+
import { dequal } from '@redocly/openapi-core/lib/utils';
45
import * as path from 'path';
56
import { performance } from 'perf_hooks';
6-
const isEqual = require('lodash.isequal');
77
import {
88
printExecutionTime,
99
pathToFilename,
@@ -232,7 +232,7 @@ function findComponentTypes(components: any) {
232232
}
233233

234234
function doesFileDiffer(filename: string, componentData: any) {
235-
return fs.existsSync(filename) && !isEqual(readYaml(filename), componentData);
235+
return fs.existsSync(filename) && !dequal(readYaml(filename), componentData);
236236
}
237237

238238
function removeEmptyComponents(

packages/core/src/bundle.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isEqual = require('lodash.isequal');
21
import { BaseResolver, resolveDocument, makeRefId, makeDocumentFromString } from './resolve';
32
import { normalizeVisitors } from './visitors';
43
import { normalizeTypes } from './types';
@@ -13,7 +12,7 @@ import {
1312
import { isAbsoluteUrl, isRef, Location, refBaseName } from './ref-utils';
1413
import { initRules } from './config/rules';
1514
import { reportUnresolvedRef } from './rules/no-unresolved-refs';
16-
import { isPlainObject, isTruthy } from './utils';
15+
import { dequal, isPlainObject, isTruthy } from './utils';
1716
import { isRedoclyRegistryURL } from './redocly/domains';
1817
import { RemoveUnusedComponents as RemoveUnusedComponentsOas2 } from './decorators/oas2/remove-unused-components';
1918
import { RemoveUnusedComponents as RemoveUnusedComponentsOas3 } from './decorators/oas3/remove-unused-components';
@@ -446,7 +445,7 @@ function makeBundleVisitor(
446445
return true;
447446
}
448447

449-
return isEqual(node, target.node);
448+
return dequal(node, target.node);
450449
}
451450

452451
function getComponentName(

packages/core/src/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,40 @@ export function getProxyAgent() {
284284
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
285285
return proxy ? new HttpsProxyAgent(proxy) : undefined;
286286
}
287+
288+
/**
289+
* Checks if two objects are deeply equal.
290+
* Borrowed the source code from https://github.com/lukeed/dequal.
291+
*/
292+
export function dequal(foo: any, bar: any): boolean {
293+
let ctor, len;
294+
if (foo === bar) return true;
295+
296+
if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
297+
if (ctor === Date) return foo.getTime() === bar.getTime();
298+
if (ctor === RegExp) return foo.toString() === bar.toString();
299+
300+
if (ctor === Array) {
301+
if ((len = foo.length) === bar.length) {
302+
while (len-- && dequal(foo[len], bar[len]));
303+
}
304+
return len === -1;
305+
}
306+
307+
if (!ctor || typeof foo === 'object') {
308+
len = 0;
309+
for (ctor in foo) {
310+
if (
311+
Object.prototype.hasOwnProperty.call(foo, ctor) &&
312+
++len &&
313+
!Object.prototype.hasOwnProperty.call(bar, ctor)
314+
)
315+
return false;
316+
if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
317+
}
318+
return Object.keys(bar).length === len;
319+
}
320+
}
321+
322+
return foo !== foo && bar !== bar;
323+
}

0 commit comments

Comments
 (0)