Skip to content

Commit

Permalink
check typeof length
Browse files Browse the repository at this point in the history
collection has also already been compared with null, so that check is
not needed.
  • Loading branch information
Forbes Lindesay committed Nov 16, 2012
1 parent 0a4ed16 commit 4ddcf87
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function toArray(collection) {
if (collection === window) return [window]
if (typeof collection === 'string') return [collection]
if (Array.isArray(collection)) return collection.slice()
if (collection !== null && !collection.length) return [collection]
if (typeof collection.length != 'number') return [collection]
if (typeof collection === 'function') return [collection]
var arr = []
for (var i = 0; i < collection.length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions test/to-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ describe('non-collections', function() {
it('single element is empty for undefined', function() {
assert.deepEqual([], toArray(undefined));
})
it('detects `{length: true}` is not a collection', function() {
assert.deepEqual([{length: true}], toArray({length: true}));
})
})

describe('identifying collections', function() {
Expand Down

0 comments on commit 4ddcf87

Please sign in to comment.