Skip to content

Commit

Permalink
Change semantics to convert non-array items to arrays with a single e…
Browse files Browse the repository at this point in the history
…lement.
  • Loading branch information
timoxley committed Nov 6, 2012
1 parent 4bd370c commit 4b2dad6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -8,13 +8,19 @@
*/

module.exports = function toArray(collection) {
if (typeof collection === 'undefined') return []
if (collection === null) return [null]
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 === 'function') return [collection]
var arr = []
if (!collection || !collection.length) return arr
for (var i = 0; i < collection.length; i++) {
if (collection.hasOwnProperty(i) || i in collection) {
arr.push(collection[i])
}
}
if (!arr.length) return [collection]
return arr
}
46 changes: 33 additions & 13 deletions test/to-array.js
Expand Up @@ -2,24 +2,43 @@ var toArray = require('to-array')
var assert = require('timoxley-assert')
var domify = require('component-domify')

describe('identifying non-collections', function() {
it('correctly identifies JS primitives as non-collections', function () {
assert.deepEqual([], toArray(function(){})); // functions should fail
assert.deepEqual([], toArray(12345));
assert.deepEqual([], toArray(null));
assert.deepEqual([], toArray(undefined));
assert.deepEqual([], toArray(/regexp/));
assert.deepEqual([], toArray(new Date()));
describe('non-collections', function() {
describe('converting into arrays', function () {
it('single element is the function for Functions', function() {
var func = function(){}
assert.deepEqual([func], toArray(func));
})
it('single element is the number for Numbers', function() {
assert.deepEqual([12345], toArray(12345));
})
it('single element is null for null', function() {
assert.deepEqual([null], toArray(null));
})
it('single element is the regex for Regexps', function() {
assert.deepEqual([/regexp/], toArray(/regexp/));
})
it('single element is the date for Dates', function() {
var date = new Date()
assert.deepEqual([date], toArray(date));
})
it('single element is the string for Strings', function() {
assert.deepEqual(['string'], toArray('string'));
})
})

it('single element array is empty for nothing', function() {
assert.deepEqual([], toArray());
})
it('detects window is not a collection', function() {
assert.deepEqual([], toArray(window));
assert.deepEqual([window], toArray(window));
})
it('single element is empty for undefined', function() {
assert.deepEqual([], toArray(undefined));
})
})

describe('identifying collections', function() {
it('correctly identifies arrays', function() {
assert.deepEqual(['s','t','r','i','n','g'], toArray('string'));
assert.deepEqual([1, 2], toArray([1, 2]));
assert.deepEqual([], toArray([]));
})
Expand All @@ -33,7 +52,8 @@ describe('identifying collections', function() {
it('won\'t grab numeric indicies if they do not exist', function() {
var A = function() {}
A.prototype.length = 4
assert.deepEqual([], toArray(new A));
var a = new A
assert.deepEqual([a], toArray(a));
})
})

Expand All @@ -54,8 +74,8 @@ describe('testing against dom', function() {
document.body.removeChild(blink2)
})

it('fails for single nodes', function() {
assert.deepEqual([], toArray(blink1)); // single nodes should fail
it('arrayifys single nodes', function() {
assert.deepEqual([blink1], toArray(blink1));
})

it('detects htmlelement collections are array-like', function() {
Expand Down

0 comments on commit 4b2dad6

Please sign in to comment.