-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling of input objects with 'length' property (#2893)
- Loading branch information
1 parent
3bce13f
commit 7e5f567
Showing
8 changed files
with
188 additions
and
132 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,91 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import identityFunc from '../identityFunc'; | ||
import safeArrayFrom from '../safeArrayFrom'; | ||
|
||
describe('safeArrayFrom', () => { | ||
it('should convert collections into arrays', () => { | ||
expect(safeArrayFrom([])).to.deep.equal([]); | ||
expect(safeArrayFrom(new Set([1, 2, 3]))).to.deep.equal([1, 2, 3]); | ||
expect(safeArrayFrom(new Int8Array([1, 2, 3]))).to.deep.equal([1, 2, 3]); | ||
|
||
// eslint-disable-next-line no-new-wrappers | ||
expect(safeArrayFrom(new String('ABC'))).to.deep.equal(['A', 'B', 'C']); | ||
|
||
function getArguments() { | ||
return arguments; | ||
} | ||
expect(safeArrayFrom(getArguments())).to.deep.equal([]); | ||
|
||
const arrayLike = {}; | ||
arrayLike[0] = 'Alpha'; | ||
arrayLike[1] = 'Bravo'; | ||
arrayLike[2] = 'Charlie'; | ||
arrayLike.length = 3; | ||
|
||
expect(safeArrayFrom(arrayLike)).to.deep.equal([ | ||
'Alpha', | ||
'Bravo', | ||
'Charlie', | ||
]); | ||
|
||
const iteratable = { | ||
[Symbol.iterator]() { | ||
const values = [1, 2, 3]; | ||
return { | ||
next() { | ||
const done = values.length === 0; | ||
const value = values.shift(); | ||
|
||
return { done, value }; | ||
}, | ||
}; | ||
}, | ||
}; | ||
expect(safeArrayFrom(iteratable)).to.deep.equal([1, 2, 3]); | ||
|
||
// istanbul ignore next (Never called and use just as a placeholder) | ||
function* generatorFunc() { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
expect(safeArrayFrom(generatorFunc())).to.deep.equal([1, 2, 3]); | ||
|
||
// But generator function itself is not iteratable | ||
expect(safeArrayFrom(generatorFunc)).to.equal(null); | ||
}); | ||
|
||
it('should return `null` for non-collections', () => { | ||
expect(safeArrayFrom(null)).to.equal(null); | ||
expect(safeArrayFrom(undefined)).to.equal(null); | ||
|
||
expect(safeArrayFrom('ABC')).to.equal(null); | ||
expect(safeArrayFrom('0')).to.equal(null); | ||
expect(safeArrayFrom('')).to.equal(null); | ||
|
||
expect(safeArrayFrom(1)).to.equal(null); | ||
expect(safeArrayFrom(0)).to.equal(null); | ||
expect(safeArrayFrom(NaN)).to.equal(null); | ||
// eslint-disable-next-line no-new-wrappers | ||
expect(safeArrayFrom(new Number(123))).to.equal(null); | ||
|
||
expect(safeArrayFrom(true)).to.equal(null); | ||
expect(safeArrayFrom(false)).to.equal(null); | ||
// eslint-disable-next-line no-new-wrappers | ||
expect(safeArrayFrom(new Boolean(true))).to.equal(null); | ||
|
||
expect(safeArrayFrom({})).to.equal(null); | ||
expect(safeArrayFrom({ length: 3 })).to.equal(null); | ||
expect(safeArrayFrom({ iterable: true })).to.equal(null); | ||
|
||
const iteratorWithoutSymbol = { next: identityFunc }; | ||
expect(safeArrayFrom(iteratorWithoutSymbol)).to.equal(null); | ||
|
||
const invalidIteratable = { | ||
[Symbol.iterator]: { next: identityFunc }, | ||
}; | ||
expect(safeArrayFrom(invalidIteratable)).to.equal(null); | ||
}); | ||
}); |
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,58 @@ | ||
import { SYMBOL_ITERATOR } from '../polyfills/symbols'; | ||
|
||
/** | ||
* Safer version of `Array.from` that return `null` if value isn't convertible to array. | ||
* Also protects against Array-like objects without items. | ||
* | ||
* @example | ||
* | ||
* safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3] | ||
* safeArrayFrom('ABC') // null | ||
* safeArrayFrom({ length: 1 }) // null | ||
* safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha'] | ||
* safeArrayFrom({ key: 'value' }) // null | ||
* safeArrayFrom(new Map()) // [] | ||
* | ||
*/ | ||
export default function safeArrayFrom<T>( | ||
collection: mixed, | ||
mapFn: (elem: mixed, index: number) => T = (item) => ((item: any): T), | ||
): Array<T> | null { | ||
if (collection == null || typeof collection !== 'object') { | ||
return null; | ||
} | ||
|
||
if (Array.isArray(collection)) { | ||
return collection.map(mapFn); | ||
} | ||
|
||
// Is Iterable? | ||
const iteratorMethod = collection[SYMBOL_ITERATOR]; | ||
if (typeof iteratorMethod === 'function') { | ||
// $FlowFixMe[incompatible-use] | ||
const iterator = iteratorMethod.call(collection); | ||
const result = []; | ||
let step; | ||
|
||
for (let i = 0; !(step = iterator.next()).done; ++i) { | ||
result.push(mapFn(step.value, i)); | ||
} | ||
return result; | ||
} | ||
|
||
// Is Array like? | ||
const length = collection.length; | ||
if (typeof length === 'number' && length >= 0 && length % 1 === 0) { | ||
const result = []; | ||
for (let i = 0; i < length; ++i) { | ||
if (!Object.prototype.hasOwnProperty.call(collection, i)) { | ||
return null; | ||
} | ||
result.push(mapFn(collection[String(i)], i)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return null; | ||
} |
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
Oops, something went wrong.