Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit c37e6eb

Browse files
rheaditiKent C. Dodds
authored and
Kent C. Dodds
committed
feat(isArray): Add isArray and tests #38 (#39)
Added a method `isArray` to check if an object passed as a parameter is a javascript array. Includes 6 tests and 100% coverage.
1 parent 93a04cf commit c37e6eb

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import arrayFill from './array-fill'
77
import sortObjectsArray from './sort-objects-array'
88
import objectValuesToString from './object-values-to-string'
99
import getObjectSize from './get-object-size'
10-
10+
import isArray from './is-array'
1111

1212
export {
1313
flatten,
@@ -19,4 +19,5 @@ export {
1919
sortObjectsArray,
2020
objectValuesToString,
2121
getObjectSize,
22+
isArray,
2223
}

src/is-array.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default isArray
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/4775737
5+
*
6+
* This method checks whether an object passed as an
7+
* argument is a javascript Array or not.
8+
*
9+
* @param {Object} someObject - the object to check
10+
* @return {Boolean} - True if someObject is an array else false
11+
*/
12+
13+
function isArray(someObject) {
14+
const objectToString = Object.prototype.toString.call(someObject)
15+
return objectToString === '[object Array]'
16+
}

test/is-array.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import test from 'ava'
2+
import {isArray} from '../src'
3+
4+
test('check an actual array of strings', t => {
5+
const object = ['one','two','three']
6+
const expected = true
7+
const actual = isArray(object)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('check a nested array', t => {
12+
const object = ['one',[2,3,4],'three']
13+
const expected = true
14+
const actual = isArray(object)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('check a strings', t => {
19+
const object = 'hello world'
20+
const expected = false
21+
const actual = isArray(object)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('check a JSON object', t => {
26+
const object = { keyOne: 'value1', keyTwo: 'value2'}
27+
const expected = false
28+
const actual = isArray(object)
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('check null', t => {
33+
const object = null
34+
const expected = false
35+
const actual = isArray(object)
36+
t.deepEqual(actual, expected)
37+
})
38+
39+
test('check undefined', t => {
40+
const object = undefined
41+
const expected = false
42+
const actual = isArray(object)
43+
t.deepEqual(actual, expected)
44+
})

0 commit comments

Comments
 (0)