Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop support for non-iteratable ArrayLike objects (#2917)
- Loading branch information
1 parent
33b14c5
commit 31e8484
Showing
8 changed files
with
119 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { identityFunc } from '../identityFunc'; | ||
import { isIteratableObject } from '../isIteratableObject'; | ||
|
||
describe('isIteratableObject', () => { | ||
it('should return `true` for collections', () => { | ||
expect(isIteratableObject([])).to.equal(true); | ||
expect(isIteratableObject(new Int8Array(1))).to.equal(true); | ||
|
||
// eslint-disable-next-line no-new-wrappers | ||
expect(isIteratableObject(new String('ABC'))).to.equal(true); | ||
|
||
function getArguments() { | ||
return arguments; | ||
} | ||
expect(isIteratableObject(getArguments())).to.equal(true); | ||
|
||
const iterator = { [Symbol.iterator]: identityFunc }; | ||
expect(isIteratableObject(iterator)).to.equal(true); | ||
|
||
// istanbul ignore next (Never called and use just as a placeholder) | ||
function* generatorFunc() { | ||
/* do nothing */ | ||
} | ||
expect(isIteratableObject(generatorFunc())).to.equal(true); | ||
|
||
// But generator function itself is not iteratable | ||
expect(isIteratableObject(generatorFunc)).to.equal(false); | ||
}); | ||
|
||
it('should return `false` for non-collections', () => { | ||
expect(isIteratableObject(null)).to.equal(false); | ||
expect(isIteratableObject(undefined)).to.equal(false); | ||
|
||
expect(isIteratableObject('ABC')).to.equal(false); | ||
expect(isIteratableObject('0')).to.equal(false); | ||
expect(isIteratableObject('')).to.equal(false); | ||
|
||
expect(isIteratableObject(1)).to.equal(false); | ||
expect(isIteratableObject(0)).to.equal(false); | ||
expect(isIteratableObject(NaN)).to.equal(false); | ||
// eslint-disable-next-line no-new-wrappers | ||
expect(isIteratableObject(new Number(123))).to.equal(false); | ||
|
||
expect(isIteratableObject(true)).to.equal(false); | ||
expect(isIteratableObject(false)).to.equal(false); | ||
// eslint-disable-next-line no-new-wrappers | ||
expect(isIteratableObject(new Boolean(true))).to.equal(false); | ||
|
||
expect(isIteratableObject({})).to.equal(false); | ||
expect(isIteratableObject({ iterable: true })).to.equal(false); | ||
|
||
const iteratorWithoutSymbol = { next: identityFunc }; | ||
expect(isIteratableObject(iteratorWithoutSymbol)).to.equal(false); | ||
|
||
const invalidIteratable = { | ||
[Symbol.iterator]: { next: identityFunc }, | ||
}; | ||
expect(isIteratableObject(invalidIteratable)).to.equal(false); | ||
|
||
const arrayLike = {}; | ||
arrayLike[0] = 'Alpha'; | ||
arrayLike[1] = 'Bravo'; | ||
arrayLike[2] = 'Charlie'; | ||
arrayLike.length = 3; | ||
|
||
expect(isIteratableObject(arrayLike)).to.equal(false); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Returns true if the provided object is an Object (i.e. not a string literal) | ||
* and implements the Iterator protocol. | ||
* | ||
* This may be used in place of [Array.isArray()][isArray] to determine if | ||
* an object should be iterated-over e.g. Array, Map, Set, Int8Array, | ||
* TypedArray, etc. but excludes string literals. | ||
* | ||
* @example | ||
* | ||
* isIteratableObject([ 1, 2, 3 ]) // true | ||
* isIteratableObject(new Map()) // true | ||
* isIteratableObject('ABC') // false | ||
* isIteratableObject({ key: 'value' }) // false | ||
* isIteratableObject({ length: 1, 0: 'Alpha' }) // false | ||
*/ | ||
declare function isIteratableObject( | ||
value: mixed, | ||
): boolean %checks(value instanceof Iterable); | ||
|
||
// eslint-disable-next-line no-redeclare | ||
export function isIteratableObject(maybeIteratable: mixed): boolean { | ||
return ( | ||
typeof maybeIteratable === 'object' && | ||
typeof maybeIteratable?.[Symbol.iterator] === 'function' | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters