1 | /*
|
2 | Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
|
3 |
|
4 | Redistribution and use in source and binary forms, with or without
|
5 | modification, are permitted provided that the following conditions are met:
|
6 |
|
7 | * Redistributions of source code must retain the above copyright
|
8 | notice, this list of conditions and the following disclaimer.
|
9 | * Redistributions in binary form must reproduce the above copyright
|
10 | notice, this list of conditions and the following disclaimer in the
|
11 | documentation and/or other materials provided with the distribution.
|
12 |
|
13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
16 | ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
17 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
22 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
23 | */
|
24 |
|
25 | /*jslint bitwise:true */
|
26 | (function () {
|
27 | ;
|
28 |
|
29 | var common;
|
30 |
|
31 | common = require('./common');
|
32 |
|
33 | function extend(result, update) {
|
34 | var prop, lhs, rhs;
|
35 |
|
36 | for (prop in update) {
|
37 | if (!common.Object.has(update, prop)) {
|
38 | continue;
|
39 | }
|
40 |
|
41 | if (prop in result) {
|
42 | lhs = result[prop];
|
43 | rhs = update[prop];
|
44 | if (common.Object.isObject(rhs) && common.Object.isObject(lhs)) {
|
45 | result[prop] = extend(lhs, rhs);
|
46 | } else {
|
47 | result[prop] = update[prop];
|
48 | }
|
49 | } else {
|
50 | result[prop] = update[prop];
|
51 | }
|
52 | }
|
53 |
|
54 | return result;
|
55 | }
|
56 |
|
57 | function Options(override) {
|
58 | var defaults = {
|
59 | destructive: true,
|
60 | preserveCompletionValue: false
|
61 | };
|
62 |
|
63 | if (override == null) {
|
64 | this.data = defaults;
|
65 | return;
|
66 | }
|
67 |
|
68 | this.data = extend(defaults, override);
|
69 | }
|
70 |
|
71 | // options.get(name, {
|
72 | // pathName: pathName
|
73 | // });
|
74 | Options.prototype.get = function get(name, details) {
|
75 | var local;
|
76 | if (details != null) {
|
77 | if (common.Object.has(details, 'pathName')) {
|
78 | local = this.data[details.pathName];
|
79 | if (local != null && common.Object.has(local, name)) {
|
80 | return local[name];
|
81 | }
|
82 | }
|
83 | }
|
84 | return this.data[name];
|
85 | };
|
86 |
|
87 | module.exports = Options;
|
88 | }());
|
89 | /* vim: set sw=4 ts=4 et tw=80 : */
|