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

Commit 93a04cf

Browse files
mtheoryxKent C. Dodds
authored and
Kent C. Dodds
committed
feat(getObjectSize): Add getObjectSize method and tests (#37)
Closes #36
1 parent 666c5fa commit 93a04cf

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist
22
node_modules
33
.nyc_output/
44
coverage/
5+
npm-debug.log

Diff for: package.json

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"devDependencies": {
3232
"ava": "0.16.0",
3333
"babel-cli": "6.11.4",
34+
"babel-eslint": "6.1.2",
3435
"babel-plugin-istanbul": "1.0.3",
3536
"babel-preset-es2015": "6.13.2",
3637
"babel-preset-stage-2": "6.13.0",
@@ -41,6 +42,9 @@
4142
"cz-conventional-changelog": "1.1.7",
4243
"eslint": "3.2.2",
4344
"eslint-config-kentcdodds": "9.0.3",
45+
"eslint-plugin-ava": "3.0.0",
46+
"eslint-plugin-babel": "3.3.0",
47+
"eslint-plugin-import": "1.13.0",
4448
"ghooks": "1.3.2",
4549
"nodemon": "1.10.0",
4650
"npm-run-all": "2.3.0",

Diff for: src/get-object-size.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default getObjectSize
2+
3+
/**
4+
* @module getObjectSize
5+
* @description Returns the size of an object
6+
*
7+
* Original Source: http://stackoverflow.com/a/6700/4038230
8+
*
9+
* @param {Object} obj - The Object to check size of
10+
* @return {Number} - The size of the Object
11+
* */
12+
13+
function getObjectSize(obj) {
14+
if (Array.isArray(obj)) {
15+
return obj.length
16+
} else {
17+
return Object.keys(obj).length
18+
}
19+
}

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import randomInteger from './random-integer'
66
import arrayFill from './array-fill'
77
import sortObjectsArray from './sort-objects-array'
88
import objectValuesToString from './object-values-to-string'
9+
import getObjectSize from './get-object-size'
910

1011

1112
export {
@@ -17,4 +18,5 @@ export {
1718
arrayFill,
1819
sortObjectsArray,
1920
objectValuesToString,
21+
getObjectSize,
2022
}

Diff for: test/get-object-size.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava';
2+
import { getObjectSize } from '../src'
3+
4+
test('returns zero for empty object', t=> {
5+
const testObject = {}
6+
const expected = 0
7+
const actual = getObjectSize(testObject)
8+
t.deepEqual(actual, expected)
9+
})
10+
test('returns 5 for object with 5 keys', t=> {
11+
const testObject = {
12+
prop1: 'prop1',
13+
prop2: 'prop2',
14+
prop3: 'prop3',
15+
prop4: 'prop4',
16+
prop5: 'prop5',
17+
}
18+
const expected = 5
19+
const actual = getObjectSize(testObject)
20+
t.deepEqual(actual, expected)
21+
})
22+
test('returns size of empty array', t => {
23+
const testArray = []
24+
const expected = 0
25+
const actual = getObjectSize(testArray)
26+
t.deepEqual(actual, expected)
27+
})
28+
test('returns size of array with items', t => {
29+
const testArray = ['test', 'test2']
30+
const expected = 2
31+
const actual = getObjectSize(testArray)
32+
t.deepEqual(actual, expected)
33+
})
34+

0 commit comments

Comments
 (0)