Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit 84c8982

Browse files
committed
Merge pull request #6 from uber/defaults
Add defaults
2 parents bbca337 + 458ba90 commit 84c8982

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ node_js:
33
- "0.8"
44
- "0.10"
55
- "0.11"
6-
before_install: npm i npm@latest -g
6+
before_install: npm i npm@~1.4.6 -g
77
script: npm run travis

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ zero-config := (dirname: String, opts?: {
6060
dc?: String,
6161
blackList?: Array<String>,
6262
env?: Object<String, String>,
63-
seed?: Object<String, Any>
63+
seed?: Object<String, Any>,
64+
defaults?: Object<String, Any>
6465
}) => {
6566
get: (keypath?: Keypath) => Any,
6667
set: ((keypath: Keypath, value: Any) => void) &
@@ -107,6 +108,8 @@ Below are the sources it reads in order of least precendence.
107108
you pass `--foo='bar' --bar.baz='bob'` you will get
108109
`{ "foo": "bar", "bar": { "baz": "bob" } }`
109110
- a seed object of manual overwrites for testing purposes.
111+
- a defaults object that populates values that have
112+
not been set by any other means.
110113

111114
The config loader also uses `config-chain` for the actual
112115
loading logic so you can read [their docs][config-chain]
@@ -203,6 +206,17 @@ The `seed` option is very useful for testing purposes, it allows
203206
This is an alternative to the `NODE_ENV=test `pattern, we highly
204207
recommend that you do not have a `test.json` file at all.
205208

209+
#### `opts.defaults`
210+
211+
`opts.defaults` is optional, it can be set to an object.
212+
213+
If it exists, it will populate all the values that are unset
214+
(but not undefined) in the loaded config with those in
215+
`opts.defaults`.
216+
217+
The difference between `defaults` and `seed` is that `seed` over-
218+
writes set values, while `defaults` does not.
219+
206220
#### `var value = config.get(keypath)`
207221

208222
`config.get(keypath)` will return the value at a keypath. The

get-config-state.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function getConfigState(dirname, opts) {
4343
// load ./config/NODE_ENV.json
4444
NODE_ENV ? join(configFolder, NODE_ENV + '.json') : null,
4545
// load ./config/common.json
46-
join(configFolder, 'common.json')
46+
join(configFolder, 'common.json'),
47+
// load defaults
48+
opts.defaults || null
4749
);
4850

4951
// there is a "bug" in config-chain where it doesn't

test/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,58 @@ test('config({ seed: seed })', function (assert) {
327327
assert.end();
328328
});
329329

330+
test('config({ defaults: defaults })', function (assert) {
331+
var argv = [
332+
'--foo', 'bar',
333+
'--baz.lulz', 'some value',
334+
'--baz.foob', 'thingy'
335+
];
336+
337+
var config = fetchConfig(__dirname, {
338+
argv: argv,
339+
seed: {
340+
baz: {
341+
lulz: 42
342+
},
343+
bar: 'foo',
344+
shouldBeUndefined: undefined,
345+
shouldBeOverwritten: 'overwrittenValue',
346+
shouldBeMerged: {
347+
deep: {
348+
foo: 'bar'
349+
}
350+
}
351+
},
352+
defaults: {
353+
shouldBeUndefined: 'defined',
354+
shouldBeFalse: false,
355+
shouldBeTrue: true,
356+
shouldBeNested: {
357+
key: 'value'
358+
},
359+
shouldBeOverwritten: 'value',
360+
shouldBeMerged: {
361+
deep: {
362+
bar: 'baz'
363+
}
364+
}
365+
}
366+
});
367+
368+
assert.equal(config.get('foo'), 'bar');
369+
assert.equal(config.get('baz.lulz'), 42);
370+
assert.equal(config.get('baz.foob'), 'thingy');
371+
assert.equal(config.get('shouldBeUndefined'), undefined);
372+
assert.equal(config.get('shouldBeFalse'), false);
373+
assert.equal(config.get('shouldBeTrue'), true);
374+
assert.equal(config.get('shouldBeNested.key'), 'value');
375+
assert.equal(config.get('shouldBeOverwritten'), 'overwrittenValue');
376+
assert.equal(config.get('shouldBeMerged.deep.foo'), 'bar');
377+
assert.equal(config.get('shouldBeMerged.deep.bar'), 'baz');
378+
379+
assert.end();
380+
});
381+
330382
test('config.set(entireObj)', function t(assert) {
331383
var config = fetchConfig(__dirname);
332384

0 commit comments

Comments
 (0)