This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +60
-0
lines changed Expand file tree Collapse file tree 5 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 22node_modules
33.nyc_output /
44coverage /
5+ npm-debug.log
Original file line number Diff line number Diff line change 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" ,
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" ,
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import randomInteger from './random-integer'
66import arrayFill from './array-fill'
77import sortObjectsArray from './sort-objects-array'
88import objectValuesToString from './object-values-to-string'
9+ import getObjectSize from './get-object-size'
910
1011
1112export {
@@ -17,4 +18,5 @@ export {
1718 arrayFill ,
1819 sortObjectsArray ,
1920 objectValuesToString ,
21+ getObjectSize ,
2022}
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments