mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
assert,util: lazy load comparison functions
PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
parent
e0c71ca3eb
commit
17d95ea1f0
@ -21,10 +21,6 @@
|
||||
'use strict';
|
||||
|
||||
const { Buffer } = require('buffer');
|
||||
const {
|
||||
isDeepEqual,
|
||||
isDeepStrictEqual
|
||||
} = require('internal/util/comparisons');
|
||||
const { codes: {
|
||||
ERR_AMBIGUOUS_ARGUMENT,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
@ -37,6 +33,15 @@ const { inspect, types: { isPromise, isRegExp } } = require('util');
|
||||
const { EOL } = require('internal/constants');
|
||||
const { NativeModule } = require('internal/bootstrap/loaders');
|
||||
|
||||
let isDeepEqual;
|
||||
let isDeepStrictEqual;
|
||||
|
||||
function lazyLoadComparison() {
|
||||
const comparison = require('internal/util/comparisons');
|
||||
isDeepEqual = comparison.isDeepEqual;
|
||||
isDeepStrictEqual = comparison.isDeepStrictEqual;
|
||||
}
|
||||
|
||||
// Escape control characters but not \n and \t to keep the line breaks and
|
||||
// indentation intact.
|
||||
// eslint-disable-next-line no-control-regex
|
||||
@ -285,6 +290,7 @@ assert.notEqual = function notEqual(actual, expected, message) {
|
||||
|
||||
// The equivalence assertion tests a deep equality relation.
|
||||
assert.deepEqual = function deepEqual(actual, expected, message) {
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (!isDeepEqual(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
@ -298,6 +304,7 @@ assert.deepEqual = function deepEqual(actual, expected, message) {
|
||||
|
||||
// The non-equivalence assertion tests for any deep inequality.
|
||||
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (isDeepEqual(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
@ -311,6 +318,7 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
||||
/* eslint-enable */
|
||||
|
||||
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (!isDeepStrictEqual(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
@ -324,6 +332,7 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
||||
|
||||
assert.notDeepStrictEqual = notDeepStrictEqual;
|
||||
function notDeepStrictEqual(actual, expected, message) {
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
if (isDeepStrictEqual(actual, expected)) {
|
||||
innerFail({
|
||||
actual,
|
||||
@ -437,6 +446,7 @@ function expectedException(actual, expected, msg) {
|
||||
throw new ERR_INVALID_ARG_VALUE('error',
|
||||
expected, 'may not be an empty object');
|
||||
}
|
||||
if (isDeepEqual === undefined) lazyLoadComparison();
|
||||
for (const key of keys) {
|
||||
if (typeof actual[key] === 'string' &&
|
||||
isRegExp(expected[key]) &&
|
||||
|
13
lib/util.js
13
lib/util.js
@ -64,10 +64,6 @@ const {
|
||||
isTypedArray
|
||||
} = types;
|
||||
|
||||
const {
|
||||
isDeepStrictEqual
|
||||
} = require('internal/util/comparisons');
|
||||
|
||||
const {
|
||||
customInspectSymbol,
|
||||
deprecate,
|
||||
@ -95,6 +91,7 @@ const dateToISOString = Date.prototype.toISOString;
|
||||
const errorToString = Error.prototype.toString;
|
||||
|
||||
let CIRCULAR_ERROR_MESSAGE;
|
||||
let internalDeepEqual;
|
||||
|
||||
/* eslint-disable */
|
||||
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
|
||||
@ -1261,7 +1258,13 @@ module.exports = exports = {
|
||||
isArray: Array.isArray,
|
||||
isBoolean,
|
||||
isBuffer,
|
||||
isDeepStrictEqual,
|
||||
isDeepStrictEqual(a, b) {
|
||||
if (internalDeepEqual === undefined) {
|
||||
internalDeepEqual = require('internal/util/comparisons')
|
||||
.isDeepStrictEqual;
|
||||
}
|
||||
return internalDeepEqual(a, b);
|
||||
},
|
||||
isNull,
|
||||
isNullOrUndefined,
|
||||
isNumber,
|
||||
|
Loading…
Reference in New Issue
Block a user