Skip to content

Commit 227661b

Browse files
gjgauravtiwari
authored andcommitted
Allow setting a literal false for configuration properties (#1585)
1 parent 80d509d commit 227661b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

package/utils/__tests__/deep_assign.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@
22

33
const deepAssign = require('../deep_assign')
44

5-
test('deep assign property', () => {
6-
const object = { foo: { bar: { } } }
7-
const path = 'foo.bar'
8-
const value = { x: 1, y: 2 }
9-
const expectation = { foo: { bar: { x: 1, y: 2 } } }
10-
expect(deepAssign(object, path, value)).toEqual(expectation)
5+
describe('deepAssign()', () => {
6+
test('deeply assigns nested properties', () => {
7+
const object = { foo: { bar: { } } }
8+
const path = 'foo.bar'
9+
const value = { x: 1, y: 2 }
10+
const expectation = { foo: { bar: { x: 1, y: 2 } } }
11+
expect(deepAssign(object, path, value)).toEqual(expectation)
12+
})
13+
14+
test('allows assignment of a literal false', () => {
15+
const object = { foo: { bar: { } } }
16+
const path = 'foo.bar'
17+
const value = false
18+
const expectation = { foo: { bar: false } }
19+
expect(deepAssign(object, path, value)).toEqual(expectation)
20+
})
21+
22+
test('does not allow assignment of other falsy values', () => {
23+
const object = { foo: { bar: { } } }
24+
const path = 'foo.bar'
25+
const values = [undefined, null, 0, '']
26+
27+
values.forEach(value => {
28+
const expectation = new Error(`Value can't be ${value}`)
29+
expect(() => deepAssign(object, path, value)).toThrow(expectation)
30+
})
31+
})
1132
})

package/utils/deep_assign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { canMerge, prettyPrint } = require('./helpers')
22
const deepMerge = require('./deep_merge')
33

44
const deepAssign = (obj, path, value) => {
5-
if (!value) throw new Error(`Value can't be ${value}`)
5+
if (!value && value !== false) throw new Error(`Value can't be ${value}`)
66

77
const keys = path.split('.')
88
const key = keys.pop()

0 commit comments

Comments
 (0)