1 | /*───────────────────────────────────────────────────────────────────────────*\
|
2 | │ Copyright (C) 2015 eBay Software Foundation │
|
3 | │ │
|
4 | │ Licensed under the Apache License, Version 2.0 (the "License"); │
|
5 | │ you may not use this file except in compliance with the License. │
|
6 | │ You may obtain a copy of the License at │
|
7 | │ │
|
8 | │ http://www.apache.org/licenses/LICENSE-2.0 │
|
9 | │ │
|
10 | │ Unless required by applicable law or agreed to in writing, software │
|
11 | │ distributed under the License is distributed on an "AS IS" BASIS, │
|
12 | │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
13 | │ See the License for the specific language governing permissions and │
|
14 | │ limitations under the License. │
|
15 | \*───────────────────────────────────────────────────────────────────────────*/
|
16 | import Thing from 'core-util-is';
|
17 | import Common from './common.js';
|
18 |
|
19 |
|
20 | export default class Config {
|
21 | constructor(data) {
|
22 | this._store = data;
|
23 | }
|
24 |
|
25 | get(key) {
|
26 | var obj;
|
27 |
|
28 | if (Thing.isString(key) && key.length) {
|
29 |
|
30 | key = key.split(':');
|
31 | obj = this._store;
|
32 |
|
33 | while (obj && key.length) {
|
34 | if (obj.constructor !== Object) {
|
35 | // Do not allow traversal into complex types,
|
36 | // such as Buffer, Date, etc. So, this type
|
37 | // of key will fail: 'foo:mystring:length'
|
38 | return undefined;
|
39 | }
|
40 | obj = obj[key.shift()];
|
41 | }
|
42 |
|
43 | return obj;
|
44 |
|
45 | }
|
46 |
|
47 | return undefined;
|
48 | }
|
49 |
|
50 | set(key, value) {
|
51 | var obj, prop;
|
52 |
|
53 | if (Thing.isString(key) && key.length) {
|
54 |
|
55 | key = key.split(':');
|
56 | obj = this._store;
|
57 |
|
58 | while (key.length - 1) {
|
59 | prop = key.shift();
|
60 |
|
61 | // Create new object for property, if nonexistent
|
62 | if (!obj.hasOwnProperty(prop)) {
|
63 | obj[prop] = {};
|
64 | }
|
65 |
|
66 | obj = obj[prop];
|
67 | if (obj && obj.constructor !== Object) {
|
68 | // Do not allow traversal into complex types,
|
69 | // such as Buffer, Date, etc. So, this type
|
70 | // of key will fail: 'foo:mystring:length'
|
71 | return undefined;
|
72 | }
|
73 | }
|
74 |
|
75 | return (obj[key.shift()] = value);
|
76 | }
|
77 |
|
78 | return undefined;
|
79 | }
|
80 |
|
81 | use(obj) {
|
82 | Common.merge(obj, this._store);
|
83 | }
|
84 |
|
85 | merge(config) {
|
86 | this.use(config._store);
|
87 | }
|
88 | } |
\ | No newline at end of file |