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

Commit 077239a

Browse files
committed
implement multiSet()
1 parent 1424de2 commit 077239a

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

config-wrapper.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ function ConfigWrapper(configObject) {
2121
}
2222

2323
function setKey(keyPath, value) {
24-
if (typeof keyPath !== 'string' && !Array.isArray(keyPath)) {
24+
if (arguments.length === 1) {
25+
return multiSet(keyPath);
26+
}
27+
28+
if (!isValidKeyPath(keyPath)) {
2529
throw errors.InvalidKeyPath({
2630
keyPath: keyPath
2731
});
@@ -37,4 +41,24 @@ function ConfigWrapper(configObject) {
3741

3842
return putPath(configObject, keyPath, v);
3943
}
44+
45+
function multiSet(obj) {
46+
if (obj === null || typeof obj !== 'object') {
47+
throw errors.InvalidMultiSetArgument({
48+
objStr: JSON.stringify(obj),
49+
obj: obj
50+
});
51+
}
52+
53+
Object.keys(obj).forEach(setEachKey);
54+
55+
function setEachKey(key) {
56+
setKey([key], obj[key]);
57+
}
58+
}
59+
}
60+
61+
function isValidKeyPath(keyPath) {
62+
return typeof keyPath === 'string' ||
63+
Array.isArray(keyPath);
4064
}

errors.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,21 @@ var InvalidKeyPath = TypedError({
4040
'SUGGESTED FIX: update the `config.set()` callsite.\n'
4141
});
4242

43+
var InvalidMultiSetArgument = TypedError({
44+
type: 'invalid.multi.set',
45+
message: 'Invalid `config.set(obj)` argument.\n' +
46+
'expected an object but instead got {objStr}.\n' +
47+
'SUGGESTED FIX: update the `config.set()` callsite to ' +
48+
'be a valid object.\n',
49+
objStr: null,
50+
obj: null
51+
});
52+
4353
module.exports = {
4454
InvalidDirname: InvalidDirname,
4555
MissingDatacenter: MissingDatacenter,
4656
DatacenterRequired: DatacenterRequired,
4757
DatacenterFileRequired: DatacenterFileRequired,
48-
InvalidKeyPath: InvalidKeyPath
58+
InvalidKeyPath: InvalidKeyPath,
59+
InvalidMultiSetArgument: InvalidMultiSetArgument
4960
};

test/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ test('config.set(undefined) throws', function (assert) {
295295
var config = fetchConfig(__dirname);
296296

297297
assert.throws(function () {
298-
config.set(undefined);
298+
config.set(undefined, 42);
299299
}, /invalid keypath/);
300300

301301
assert.end();
@@ -326,3 +326,35 @@ test('config({ seed: seed })', function (assert) {
326326

327327
assert.end();
328328
});
329+
330+
test('config.set(entireObj)', function t(assert) {
331+
var config = fetchConfig(__dirname);
332+
333+
config.set('foo', 'bar');
334+
config.set('baz', { 42: 42 });
335+
336+
assert.equal(config.get('foo'), 'bar');
337+
assert.deepEqual(config.get('baz'), { '42': 42 });
338+
339+
config.set({
340+
foo: 'new-key',
341+
baz: { 50: 50 },
342+
other: 'thing'
343+
});
344+
345+
assert.equal(config.get('foo'), 'new-key');
346+
assert.deepEqual(config.get('baz'), { '42': 42, '50': 50 });
347+
assert.equal(config.get('other'), 'thing');
348+
349+
assert.end();
350+
});
351+
352+
test('config.set(weirdValue)', function t(assert) {
353+
var config = fetchConfig(__dirname);
354+
355+
assert.throws(function () {
356+
config.set(42);
357+
}, /Invalid `config\.set\(obj\)` argument/);
358+
359+
assert.end();
360+
});

0 commit comments

Comments
 (0)