Replace isArray helper with native Array.isArray

This commit is contained in:
Steve Gravrock
2025-11-28 08:09:51 -08:00
parent 4371081763
commit f5e9b61f73
12 changed files with 27 additions and 63 deletions

View File

@@ -903,7 +903,7 @@ jasmineRequire.htmlReporterUtils = function(j$) {
const el = document.createElement(type); const el = document.createElement(type);
let children; let children;
if (j$.private.isArray(childrenArrayOrVarArgs)) { if (Array.isArray(childrenArrayOrVarArgs)) {
children = childrenArrayOrVarArgs; children = childrenArrayOrVarArgs;
} else { } else {
children = []; children = [];

View File

@@ -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) { j$.private.isObject = function(value) {
return ( return (
value !== undefined && value !== null && j$.private.isA('Object', value) value !== undefined && value !== null && j$.private.isA('Object', value)
@@ -2332,7 +2328,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) {
} }
ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) { ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
if (!j$.private.isArray(this.sample)) { if (!Array.isArray(this.sample)) {
throw new Error( throw new Error(
'You must provide an array to arrayContaining, not ' + 'You must provide an array to arrayContaining, not ' +
j$.private.basicPrettyPrinter(this.sample) + 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 // 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 // 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 // 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; return false;
} }
@@ -2374,7 +2370,7 @@ getJasmineRequireObj().ArrayWithExactContents = function(j$) {
other, other,
matchersUtil matchersUtil
) { ) {
if (!j$.private.isArray(this.sample)) { if (!Array.isArray(this.sample)) {
throw new Error( throw new Error(
'You must provide an array to arrayWithExactContents, not ' + 'You must provide an array to arrayWithExactContents, not ' +
j$.private.basicPrettyPrinter(this.sample) + j$.private.basicPrettyPrinter(this.sample) +
@@ -2410,7 +2406,7 @@ getJasmineRequireObj().Empty = function(j$) {
Empty.prototype.asymmetricMatch = function(other) { Empty.prototype.asymmetricMatch = function(other) {
if ( if (
j$.private.isString(other) || j$.private.isString(other) ||
j$.private.isArray(other) || Array.isArray(other) ||
j$.private.isTypedArray(other) j$.private.isTypedArray(other)
) { ) {
return other.length === 0; return other.length === 0;
@@ -2525,7 +2521,7 @@ getJasmineRequireObj().NotEmpty = function(j$) {
NotEmpty.prototype.asymmetricMatch = function(other) { NotEmpty.prototype.asymmetricMatch = function(other) {
if ( if (
j$.private.isString(other) || j$.private.isString(other) ||
j$.private.isArray(other) || Array.isArray(other) ||
j$.private.isTypedArray(other) j$.private.isTypedArray(other)
) { ) {
return other.length !== 0; return other.length !== 0;
@@ -8263,7 +8259,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if ( } else if (
value.toString && value.toString &&
typeof value === 'object' && typeof value === 'object' &&
!j$.private.isArray(value) && !Array.isArray(value) &&
hasCustomToString(value) hasCustomToString(value)
) { ) {
try { try {
@@ -8275,15 +8271,12 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if (this.seen.includes(value)) { } else if (this.seen.includes(value)) {
this.emitScalar( this.emitScalar(
'<circular reference: ' + '<circular reference: ' +
(j$.private.isArray(value) ? 'Array' : 'Object') + (Array.isArray(value) ? 'Array' : 'Object') +
'>' '>'
); );
} else if ( } else if (Array.isArray(value) || j$.private.isA('Object', value)) {
j$.private.isArray(value) ||
j$.private.isA('Object', value)
) {
this.seen.push(value); this.seen.push(value);
if (j$.private.isArray(value)) { if (Array.isArray(value)) {
this.emitArray(value); this.emitArray(value);
} else { } else {
this.emitObject(value); this.emitObject(value);
@@ -8306,10 +8299,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} }
iterateObject(obj, fn) { iterateObject(obj, fn) {
const objKeys = j$.private.MatchersUtil.keys( const objKeys = j$.private.MatchersUtil.keys(obj, Array.isArray(obj));
obj,
j$.private.isArray(obj)
);
const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
@@ -10237,7 +10227,7 @@ getJasmineRequireObj().SpyFactory = function(j$) {
this.createSpyObj = function(baseName, methodNames, propertyNames) { this.createSpyObj = function(baseName, methodNames, propertyNames) {
const baseNameIsCollection = const baseNameIsCollection =
j$.private.isObject(baseName) || j$.private.isArray(baseName); j$.private.isObject(baseName) || Array.isArray(baseName);
if (baseNameIsCollection) { if (baseNameIsCollection) {
propertyNames = methodNames; propertyNames = methodNames;
@@ -10283,7 +10273,7 @@ getJasmineRequireObj().SpyFactory = function(j$) {
function normalizeKeyValues(object) { function normalizeKeyValues(object) {
const result = []; const result = [];
if (j$.private.isArray(object)) { if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) { for (let i = 0; i < object.length; i++) {
result.push([object[i]]); result.push([object[i]]);
} }

View File

@@ -87,7 +87,7 @@ describe('Runner', function() {
function arrayNotContaining(item) { function arrayNotContaining(item) {
return { return {
asymmetricMatch(other, matchersUtil) { asymmetricMatch(other, matchersUtil) {
if (!jasmine.private.isArray(other)) { if (!Array.isArray(other)) {
return false; return false;
} }

View File

@@ -1,20 +1,4 @@
describe('util', function() { 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() { describe('isObject', function() {
it('should return true if the argument is an object', function() { it('should return true if the argument is an object', function() {
expect(privateUnderTest.isObject({})).toBe(true); expect(privateUnderTest.isObject({})).toBe(true);

View File

@@ -59,7 +59,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if ( } else if (
value.toString && value.toString &&
typeof value === 'object' && typeof value === 'object' &&
!j$.private.isArray(value) && !Array.isArray(value) &&
hasCustomToString(value) hasCustomToString(value)
) { ) {
try { try {
@@ -71,15 +71,12 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} else if (this.seen.includes(value)) { } else if (this.seen.includes(value)) {
this.emitScalar( this.emitScalar(
'<circular reference: ' + '<circular reference: ' +
(j$.private.isArray(value) ? 'Array' : 'Object') + (Array.isArray(value) ? 'Array' : 'Object') +
'>' '>'
); );
} else if ( } else if (Array.isArray(value) || j$.private.isA('Object', value)) {
j$.private.isArray(value) ||
j$.private.isA('Object', value)
) {
this.seen.push(value); this.seen.push(value);
if (j$.private.isArray(value)) { if (Array.isArray(value)) {
this.emitArray(value); this.emitArray(value);
} else { } else {
this.emitObject(value); this.emitObject(value);
@@ -102,10 +99,7 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
} }
iterateObject(obj, fn) { iterateObject(obj, fn) {
const objKeys = j$.private.MatchersUtil.keys( const objKeys = j$.private.MatchersUtil.keys(obj, Array.isArray(obj));
obj,
j$.private.isArray(obj)
);
const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); const length = Math.min(objKeys.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH);
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {

View File

@@ -21,7 +21,7 @@ getJasmineRequireObj().SpyFactory = function(j$) {
this.createSpyObj = function(baseName, methodNames, propertyNames) { this.createSpyObj = function(baseName, methodNames, propertyNames) {
const baseNameIsCollection = const baseNameIsCollection =
j$.private.isObject(baseName) || j$.private.isArray(baseName); j$.private.isObject(baseName) || Array.isArray(baseName);
if (baseNameIsCollection) { if (baseNameIsCollection) {
propertyNames = methodNames; propertyNames = methodNames;
@@ -67,7 +67,7 @@ getJasmineRequireObj().SpyFactory = function(j$) {
function normalizeKeyValues(object) { function normalizeKeyValues(object) {
const result = []; const result = [];
if (j$.private.isArray(object)) { if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) { for (let i = 0; i < object.length; i++) {
result.push([object[i]]); result.push([object[i]]);
} }

View File

@@ -6,7 +6,7 @@ getJasmineRequireObj().ArrayContaining = function(j$) {
} }
ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) { ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
if (!j$.private.isArray(this.sample)) { if (!Array.isArray(this.sample)) {
throw new Error( throw new Error(
'You must provide an array to arrayContaining, not ' + 'You must provide an array to arrayContaining, not ' +
j$.private.basicPrettyPrinter(this.sample) + 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 // 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 // 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 // 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; return false;
} }

View File

@@ -9,7 +9,7 @@ getJasmineRequireObj().ArrayWithExactContents = function(j$) {
other, other,
matchersUtil matchersUtil
) { ) {
if (!j$.private.isArray(this.sample)) { if (!Array.isArray(this.sample)) {
throw new Error( throw new Error(
'You must provide an array to arrayWithExactContents, not ' + 'You must provide an array to arrayWithExactContents, not ' +
j$.private.basicPrettyPrinter(this.sample) + j$.private.basicPrettyPrinter(this.sample) +

View File

@@ -6,7 +6,7 @@ getJasmineRequireObj().Empty = function(j$) {
Empty.prototype.asymmetricMatch = function(other) { Empty.prototype.asymmetricMatch = function(other) {
if ( if (
j$.private.isString(other) || j$.private.isString(other) ||
j$.private.isArray(other) || Array.isArray(other) ||
j$.private.isTypedArray(other) j$.private.isTypedArray(other)
) { ) {
return other.length === 0; return other.length === 0;

View File

@@ -6,7 +6,7 @@ getJasmineRequireObj().NotEmpty = function(j$) {
NotEmpty.prototype.asymmetricMatch = function(other) { NotEmpty.prototype.asymmetricMatch = function(other) {
if ( if (
j$.private.isString(other) || j$.private.isString(other) ||
j$.private.isArray(other) || Array.isArray(other) ||
j$.private.isTypedArray(other) j$.private.isTypedArray(other)
) { ) {
return other.length !== 0; return other.length !== 0;

View File

@@ -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) { j$.private.isObject = function(value) {
return ( return (
value !== undefined && value !== null && j$.private.isA('Object', value) value !== undefined && value !== null && j$.private.isA('Object', value)

View File

@@ -5,7 +5,7 @@ jasmineRequire.htmlReporterUtils = function(j$) {
const el = document.createElement(type); const el = document.createElement(type);
let children; let children;
if (j$.private.isArray(childrenArrayOrVarArgs)) { if (Array.isArray(childrenArrayOrVarArgs)) {
children = childrenArrayOrVarArgs; children = childrenArrayOrVarArgs;
} else { } else {
children = []; children = [];