This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree 5 files changed +60
-0
lines changed
5 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 2
2
node_modules
3
3
.nyc_output /
4
4
coverage /
5
+ npm-debug.log
Original file line number Diff line number Diff line change 31
31
"devDependencies" : {
32
32
"ava" : " 0.16.0" ,
33
33
"babel-cli" : " 6.11.4" ,
34
+ "babel-eslint" : " 6.1.2" ,
34
35
"babel-plugin-istanbul" : " 1.0.3" ,
35
36
"babel-preset-es2015" : " 6.13.2" ,
36
37
"babel-preset-stage-2" : " 6.13.0" ,
41
42
"cz-conventional-changelog" : " 1.1.7" ,
42
43
"eslint" : " 3.2.2" ,
43
44
"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" ,
44
48
"ghooks" : " 1.3.2" ,
45
49
"nodemon" : " 1.10.0" ,
46
50
"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'
6
6
import arrayFill from './array-fill'
7
7
import sortObjectsArray from './sort-objects-array'
8
8
import objectValuesToString from './object-values-to-string'
9
+ import getObjectSize from './get-object-size'
9
10
10
11
11
12
export {
@@ -17,4 +18,5 @@ export {
17
18
arrayFill ,
18
19
sortObjectsArray ,
19
20
objectValuesToString ,
21
+ getObjectSize ,
20
22
}
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