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

Commit 666c5fa

Browse files
DdZ-FredKent C. Dodds
authored and
Kent C. Dodds
committed
feat(objectValuesToString): add 'objectValuesToString' method with tests
To close #33: I've added a function that takes in parameter an object and returns a comma-separated string that is composed of all its values. The tests make sure the value passed is really an object and that its undefined or null properties are skipped Closes #33 Ref #35
1 parent ed6245b commit 666c5fa

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import padLeft from './pad-left'
55
import randomInteger from './random-integer'
66
import arrayFill from './array-fill'
77
import sortObjectsArray from './sort-objects-array'
8+
import objectValuesToString from './object-values-to-string'
9+
810

911
export {
1012
flatten,
@@ -14,4 +16,5 @@ export {
1416
randomInteger,
1517
arrayFill,
1618
sortObjectsArray,
19+
objectValuesToString,
1720
}

src/object-values-to-string.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default objectValuesToString
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/31017540
5+
*
6+
* This method will take an object in parameter and return a
7+
* comma-separated String composed of all its properties' values
8+
*
9+
* @param {Object} obj - The object from which the values are taken
10+
* @return {String} A comma-separated String that contains all the object values
11+
*/
12+
function objectValuesToString(obj) {
13+
if (!obj) {
14+
return ''
15+
} else if (typeof obj !== 'object' || obj instanceof Date) {
16+
return ''
17+
}
18+
19+
return Object.keys(obj).map(key => obj[key])
20+
.filter(val => val || val === 0 || val === '0' || val === false)
21+
.join(', ')
22+
}

test/object-values-to-string.test.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import test from 'ava'
2+
import {objectValuesToString} from '../src'
3+
4+
test('returns a comma-separated string with all the object values', t => {
5+
const object = {
6+
id: 187,
7+
title: 'egghead.io is awesome',
8+
subtitle: 'Contribute on GitHub',
9+
author: 'Patata Patati',
10+
}
11+
const expected = '187, egghead.io is awesome, Contribute on GitHub, Patata Patati'
12+
const actual = objectValuesToString(object)
13+
t.deepEqual(actual, expected)
14+
})
15+
16+
test('returns an empty string if value in param is falsy: false', t => {
17+
const object = false
18+
const expected = ''
19+
const actual = objectValuesToString(object)
20+
t.deepEqual(actual, expected)
21+
})
22+
23+
test('returns an empty string if value in param is falsy: zero(Number)', t => {
24+
const object = 0
25+
const expected = ''
26+
const actual = objectValuesToString(object)
27+
t.deepEqual(actual, expected)
28+
})
29+
30+
test('returns an empty string if value in param is falsy: zero(String)', t => {
31+
const object = '0'
32+
const expected = ''
33+
const actual = objectValuesToString(object)
34+
t.deepEqual(actual, expected)
35+
})
36+
37+
test('returns an empty string if value in param is falsy: empty string', t => {
38+
const object = ''
39+
const expected = ''
40+
const actual = objectValuesToString(object)
41+
t.deepEqual(actual, expected)
42+
})
43+
44+
test('returns an empty string if value in param is falsy: null', t => {
45+
const object = null
46+
const expected = ''
47+
const actual = objectValuesToString(object)
48+
t.deepEqual(actual, expected)
49+
})
50+
51+
test('returns an empty string if value in param is falsy: undefined', t => {
52+
const object = undefined
53+
const expected = ''
54+
const actual = objectValuesToString(object)
55+
t.deepEqual(actual, expected)
56+
})
57+
58+
test('returns an empty string if value in param is falsy: NaN', t => {
59+
const object = NaN
60+
const expected = ''
61+
const actual = objectValuesToString(object)
62+
t.deepEqual(actual, expected)
63+
})
64+
65+
test('returns an empty string if value in param is not an object: function', t => {
66+
function object() {
67+
return 'JavaScript, ES6, Github!'
68+
}
69+
const expected = ''
70+
const actual = objectValuesToString(object)
71+
t.deepEqual(actual, expected)
72+
})
73+
74+
test('returns an empty string if value in param is not an object: number', t => {
75+
const object = 878565
76+
const expected = ''
77+
const actual = objectValuesToString(object)
78+
t.deepEqual(actual, expected)
79+
})
80+
81+
test('returns an empty string if value in param is not an object: string', t => {
82+
const object = 'This is a string'
83+
const expected = ''
84+
const actual = objectValuesToString(object)
85+
t.deepEqual(actual, expected)
86+
})
87+
88+
test('returns an empty string if value in param is not an object: boolean', t => {
89+
const object = true
90+
const expected = ''
91+
const actual = objectValuesToString(object)
92+
t.deepEqual(actual, expected)
93+
})
94+
95+
test('returns an empty string if value in param is a Date', t => {
96+
const object = new Date()
97+
const expected = ''
98+
const actual = objectValuesToString(object)
99+
t.deepEqual(actual, expected)
100+
})
101+
102+
test('skips falsy object values excluding zero and false', t => {
103+
const object = {
104+
id: 0,
105+
position: '0',
106+
title: undefined,
107+
subtitle: 'JavaScript is magic',
108+
author: null,
109+
error: NaN,
110+
visible: false,
111+
otherProp: '',
112+
}
113+
const expected = '0, 0, JavaScript is magic, false'
114+
const actual = objectValuesToString(object)
115+
t.deepEqual(actual, expected)
116+
})

0 commit comments

Comments
 (0)