1 | //- JavaScript source code
|
2 |
|
3 | //- configure.js ~~
|
4 | // ~~ (c) SRW, 17 Dec 2012
|
5 | // ~~ last updated 14 Mar 2014
|
6 |
|
7 | (function () {
|
8 | ;
|
9 |
|
10 | // Pragmas
|
11 |
|
12 | /*jshint maxparams: 2, quotmark: single, strict: true */
|
13 |
|
14 | /*jslint indent: 4, maxlen: 80, node: true */
|
15 |
|
16 | /*properties exports, hasOwnProperty */
|
17 |
|
18 | // Out-of-scope definitions
|
19 |
|
20 | module.exports = function configure(user_input, default_values) {
|
21 | // This function deep-copies the properties of the `default_values` object
|
22 | // onto a fresh output object `y` recursively, but it will prefer to copy
|
23 | // a property from the `user_input` object when available.
|
24 | if ((user_input instanceof Object) === false) {
|
25 | user_input = {};
|
26 | }
|
27 | var key, y;
|
28 | y = (default_values instanceof Array) ? [] : user_input;
|
29 | for (key in default_values) {
|
30 | if (default_values.hasOwnProperty(key)) {
|
31 | if ((default_values[key] instanceof Object) &&
|
32 | (typeof default_values[key] !== 'function')) {
|
33 | y[key] = configure(user_input[key], default_values[key]);
|
34 | } else if (user_input.hasOwnProperty(key)) {
|
35 | y[key] = user_input[key];
|
36 | } else {
|
37 | y[key] = default_values[key];
|
38 | }
|
39 | }
|
40 | }
|
41 | return y;
|
42 | };
|
43 |
|
44 | // That's all, folks!
|
45 |
|
46 | return;
|
47 |
|
48 | }());
|
49 |
|
50 | //- vim:set syntax=javascript:
|