From f5e9b61f7374150bdf5314c1060d3d98db9202b0 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Fri, 28 Nov 2025 08:09:51 -0800 Subject: [PATCH] Replace isArray helper with native Array.isArray --- lib/jasmine-core/jasmine-html.js | 2 +- lib/jasmine-core/jasmine.js | 34 +++++++------------ spec/core/RunnerSpec.js | 2 +- spec/core/UtilSpec.js | 16 --------- src/core/PrettyPrinter.js | 16 +++------ src/core/SpyFactory.js | 4 +-- .../asymmetric_equality/ArrayContaining.js | 4 +-- .../ArrayWithExactContents.js | 2 +- src/core/asymmetric_equality/Empty.js | 2 +- src/core/asymmetric_equality/NotEmpty.js | 2 +- src/core/base.js | 4 --- src/html/htmlReporterUtils.js | 2 +- 12 files changed, 27 insertions(+), 63 deletions(-) diff --git a/lib/jasmine-core/jasmine-html.js b/lib/jasmine-core/jasmine-html.js index 454061b2..f6125b1a 100644 --- a/lib/jasmine-core/jasmine-html.js +++ b/lib/jasmine-core/jasmine-html.js @@ -903,7 +903,7 @@ jasmineRequire.htmlReporterUtils = function(j$) { const el = document.createElement(type); let children; - if (j$.private.isArray(childrenArrayOrVarArgs)) { + if (Array.isArray(childrenArrayOrVarArgs)) { children = childrenArrayOrVarArgs; } else { children = []; diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 63b9ee84..08d7c19f 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -275,10 +275,6 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { } }); - j$.private.isArray = function(value) { - return j$.private.isA('Array', value); - }; - j$.private.isObject = function(value) { return ( value !== undefined && value !== null && j$.private.isA('Object', value) @@ -2332,7 +2328,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) { } ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) { - if (!j$.private.isArray(this.sample)) { + if (!Array.isArray(this.sample)) { throw new Error( 'You must provide an array to arrayContaining, not ' + j$.private.basicPrettyPrinter(this.sample) + @@ -2343,7 +2339,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) { // If the actual parameter is not an array, we can fail immediately, since it couldn't // possibly be an "array containing" anything. However, we also want an empty sample // array to match anything, so we need to double-check we aren't in that case - if (!j$.private.isArray(other) && this.sample.length > 0) { + if (!Array.isArray(other) && this.sample.length > 0) { return false; } @@ -2374,7 +2370,7 @@ getJasmineRequireObj().ArrayWithExactContents = function(j$) { other, matchersUtil ) { - if (!j$.private.isArray(this.sample)) { + if (!Array.isArray(this.sample)) { throw new Error( 'You must provide an array to arrayWithExactContents, not ' + j$.private.basicPrettyPrinter(this.sample) + @@ -2410,7 +2406,7 @@ getJasmineRequireObj().Empty = function(j$) { Empty.prototype.asymmetricMatch = function(other) { if ( j$.private.isString(other) || - j$.private.isArray(other) || + Array.isArray(other) || j$.private.isTypedArray(other) ) { return other.length === 0; @@ -2525,7 +2521,7 @@ getJasmineRequireObj().NotEmpty = function(j$) { NotEmpty.prototype.asymmetricMatch = function(other) { if ( j$.private.isString(other) || - j$.private.isArray(other) || + Array.isArray(other) || j$.private.isTypedArray(other) ) { return other.length !== 0; @@ -8263,7 +8259,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if ( value.toString && typeof value === 'object' && - !j$.private.isArray(value) && + !Array.isArray(value) && hasCustomToString(value) ) { try { @@ -8275,15 +8271,12 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if (this.seen.includes(value)) { this.emitScalar( '' ); - } else if ( - j$.private.isArray(value) || - j$.private.isA('Object', value) - ) { + } else if (Array.isArray(value) || j$.private.isA('Object', value)) { this.seen.push(value); - if (j$.private.isArray(value)) { + if (Array.isArray(value)) { this.emitArray(value); } else { this.emitObject(value); @@ -8306,10 +8299,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } iterateObject(obj, fn) { - const objKeys = j$.private.MatchersUtil.keys( - obj, - j$.private.isArray(obj) - ); + const objKeys = j$.private.MatchersUtil.keys(obj, Array.isArray(obj)); const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); for (let i = 0; i < length; i++) { @@ -10237,7 +10227,7 @@ getJasmineRequireObj().SpyFactory = function(j$) { this.createSpyObj = function(baseName, methodNames, propertyNames) { const baseNameIsCollection = - j$.private.isObject(baseName) || j$.private.isArray(baseName); + j$.private.isObject(baseName) || Array.isArray(baseName); if (baseNameIsCollection) { propertyNames = methodNames; @@ -10283,7 +10273,7 @@ getJasmineRequireObj().SpyFactory = function(j$) { function normalizeKeyValues(object) { const result = []; - if (j$.private.isArray(object)) { + if (Array.isArray(object)) { for (let i = 0; i < object.length; i++) { result.push([object[i]]); } diff --git a/spec/core/RunnerSpec.js b/spec/core/RunnerSpec.js index 84667e53..2b2bcfe2 100644 --- a/spec/core/RunnerSpec.js +++ b/spec/core/RunnerSpec.js @@ -87,7 +87,7 @@ describe('Runner', function() { function arrayNotContaining(item) { return { asymmetricMatch(other, matchersUtil) { - if (!jasmine.private.isArray(other)) { + if (!Array.isArray(other)) { return false; } diff --git a/spec/core/UtilSpec.js b/spec/core/UtilSpec.js index 6a0abc0a..0ab7d919 100644 --- a/spec/core/UtilSpec.js +++ b/spec/core/UtilSpec.js @@ -1,20 +1,4 @@ describe('util', function() { - describe('isArray', function() { - it('should return true if the argument is an array', function() { - expect(privateUnderTest.isArray([])).toBe(true); - expect(privateUnderTest.isArray(['a'])).toBe(true); - }); - - it('should return false if the argument is not an array', function() { - expect(privateUnderTest.isArray(undefined)).toBe(false); - expect(privateUnderTest.isArray({})).toBe(false); - expect(privateUnderTest.isArray(function() {})).toBe(false); - expect(privateUnderTest.isArray('foo')).toBe(false); - expect(privateUnderTest.isArray(5)).toBe(false); - expect(privateUnderTest.isArray(null)).toBe(false); - }); - }); - describe('isObject', function() { it('should return true if the argument is an object', function() { expect(privateUnderTest.isObject({})).toBe(true); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index 8c358680..9e506bce 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -59,7 +59,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if ( value.toString && typeof value === 'object' && - !j$.private.isArray(value) && + !Array.isArray(value) && hasCustomToString(value) ) { try { @@ -71,15 +71,12 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } else if (this.seen.includes(value)) { this.emitScalar( '' ); - } else if ( - j$.private.isArray(value) || - j$.private.isA('Object', value) - ) { + } else if (Array.isArray(value) || j$.private.isA('Object', value)) { this.seen.push(value); - if (j$.private.isArray(value)) { + if (Array.isArray(value)) { this.emitArray(value); } else { this.emitObject(value); @@ -102,10 +99,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) { } iterateObject(obj, fn) { - const objKeys = j$.private.MatchersUtil.keys( - obj, - j$.private.isArray(obj) - ); + const objKeys = j$.private.MatchersUtil.keys(obj, Array.isArray(obj)); const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); for (let i = 0; i < length; i++) { diff --git a/src/core/SpyFactory.js b/src/core/SpyFactory.js index 26a768be..dfb669d9 100644 --- a/src/core/SpyFactory.js +++ b/src/core/SpyFactory.js @@ -21,7 +21,7 @@ getJasmineRequireObj().SpyFactory = function(j$) { this.createSpyObj = function(baseName, methodNames, propertyNames) { const baseNameIsCollection = - j$.private.isObject(baseName) || j$.private.isArray(baseName); + j$.private.isObject(baseName) || Array.isArray(baseName); if (baseNameIsCollection) { propertyNames = methodNames; @@ -67,7 +67,7 @@ getJasmineRequireObj().SpyFactory = function(j$) { function normalizeKeyValues(object) { const result = []; - if (j$.private.isArray(object)) { + if (Array.isArray(object)) { for (let i = 0; i < object.length; i++) { result.push([object[i]]); } diff --git a/src/core/asymmetric_equality/ArrayContaining.js b/src/core/asymmetric_equality/ArrayContaining.js index 7560df19..e2408e19 100644 --- a/src/core/asymmetric_equality/ArrayContaining.js +++ b/src/core/asymmetric_equality/ArrayContaining.js @@ -6,7 +6,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) { } ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) { - if (!j$.private.isArray(this.sample)) { + if (!Array.isArray(this.sample)) { throw new Error( 'You must provide an array to arrayContaining, not ' + j$.private.basicPrettyPrinter(this.sample) + @@ -17,7 +17,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) { // If the actual parameter is not an array, we can fail immediately, since it couldn't // possibly be an "array containing" anything. However, we also want an empty sample // array to match anything, so we need to double-check we aren't in that case - if (!j$.private.isArray(other) && this.sample.length > 0) { + if (!Array.isArray(other) && this.sample.length > 0) { return false; } diff --git a/src/core/asymmetric_equality/ArrayWithExactContents.js b/src/core/asymmetric_equality/ArrayWithExactContents.js index 2d5f5172..104049b2 100644 --- a/src/core/asymmetric_equality/ArrayWithExactContents.js +++ b/src/core/asymmetric_equality/ArrayWithExactContents.js @@ -9,7 +9,7 @@ getJasmineRequireObj().ArrayWithExactContents = function(j$) { other, matchersUtil ) { - if (!j$.private.isArray(this.sample)) { + if (!Array.isArray(this.sample)) { throw new Error( 'You must provide an array to arrayWithExactContents, not ' + j$.private.basicPrettyPrinter(this.sample) + diff --git a/src/core/asymmetric_equality/Empty.js b/src/core/asymmetric_equality/Empty.js index 097238ad..7cab0544 100644 --- a/src/core/asymmetric_equality/Empty.js +++ b/src/core/asymmetric_equality/Empty.js @@ -6,7 +6,7 @@ getJasmineRequireObj().Empty = function(j$) { Empty.prototype.asymmetricMatch = function(other) { if ( j$.private.isString(other) || - j$.private.isArray(other) || + Array.isArray(other) || j$.private.isTypedArray(other) ) { return other.length === 0; diff --git a/src/core/asymmetric_equality/NotEmpty.js b/src/core/asymmetric_equality/NotEmpty.js index 088a053f..b4c8660b 100644 --- a/src/core/asymmetric_equality/NotEmpty.js +++ b/src/core/asymmetric_equality/NotEmpty.js @@ -6,7 +6,7 @@ getJasmineRequireObj().NotEmpty = function(j$) { NotEmpty.prototype.asymmetricMatch = function(other) { if ( j$.private.isString(other) || - j$.private.isArray(other) || + Array.isArray(other) || j$.private.isTypedArray(other) ) { return other.length !== 0; diff --git a/src/core/base.js b/src/core/base.js index 2680987a..95d5bdef 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -77,10 +77,6 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { } }); - j$.private.isArray = function(value) { - return j$.private.isA('Array', value); - }; - j$.private.isObject = function(value) { return ( value !== undefined && value !== null && j$.private.isA('Object', value) diff --git a/src/html/htmlReporterUtils.js b/src/html/htmlReporterUtils.js index a371413b..6a4e407d 100644 --- a/src/html/htmlReporterUtils.js +++ b/src/html/htmlReporterUtils.js @@ -5,7 +5,7 @@ jasmineRequire.htmlReporterUtils = function(j$) { const el = document.createElement(type); let children; - if (j$.private.isArray(childrenArrayOrVarArgs)) { + if (Array.isArray(childrenArrayOrVarArgs)) { children = childrenArrayOrVarArgs; } else { children = [];