-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (192 loc) · 4.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
/**
* Module dependencies
*/
var Emitter = require('component-emitter');
var lazy = require('lazy-cache')(require);
/**
* Lazily required module dependencies
*/
lazy('object.omit', 'omit');
lazy('kind-of', 'typeOf');
lazy('extend-shallow', 'extend');
lazy('arr-flatten', 'flatten');
lazy('array-union', 'union');
lazy('set-value', 'set');
lazy('get-value', 'get');
/**
* Initialize a new `Config`, optionally passing an object
* to initialize with.
*
* ```js
* var config = new Config();
* ```
* @param {Object} `cache`
* @api public
*/
function Config(cache) {
Emitter.call(this);
this.cache = cache || {};
}
Emitter(Config.prototype);
/**
* Static method for mixing `Config` prototype properties onto `obj`.
*
* ```js
* function App() {
* Config.call(this);
* }
* Config.mixin(App.prototype);
* ```
*
* @param {Object} `obj`
* @return {Object}
* @api public
*/
Config.mixin = function(receiver, provider) {
provider = provider || this;
for (var key in provider) {
receiver.constructor[key] = provider[key];
}
receiver.constructor.prototype = Object.create(provider.prototype);
for (var prop in receiver) {
receiver.constructor.prototype[prop] = receiver[prop];
}
receiver.constructor.__super__ = provider.prototype;
return receiver.constructor;
};
/**
* Assign `value` to `key` or return the value of `key`.
*
* ```js
* config.set(key, value);
* ```
*
* @param {String} `key`
* @param {*} `value`
* @return {Object} `Config` to enable chaining
* @api public
*/
Config.prototype.set = function(key, value) {
if (arguments.length === 1 && lazy.typeOf(key) === 'object') {
this.extend(key);
} else {
lazy.set(this.cache, key, value);
}
this.emit('set', key, value);
return this;
};
/**
* Return the stored value of `key`. Dot notation may be used
* to get [nested property values][get-value].
*
* ```js
* config.set('foo', 'bar');
* config.get('foo');
* // => "bar"
*
* // also takes an array or list of property paths
* config.set({data: {name: 'Jon'}})
* config.get('data', 'name');
* //=> 'Jon'
* ```
*
* @param {*} `key`
* @param {Boolean} `escape`
* @return {*}
* @api public
*/
Config.prototype.get = function(key, escape) {
return key ? lazy.get(this.cache, key, escape) : this.cache;
};
/**
* Add values to an array on the `cache`.
*
* ```js
* // config.cache['foo'] => ['a.hbs', 'b.hbs']
* config
* .union('foo', ['b.hbs', 'c.hbs'], ['d.hbs']);
* .union('foo', ['e.hbs', 'f.hbs']);
*
* // config.cache['foo'] => ['a.hbs', 'b.hbs', 'c.hbs', 'd.hbs', 'e.hbs', 'f.hbs']
* ```
* @chainable
* @return {Object} `Config` to enable chaining
* @api public
*/
Config.prototype.union = function(key/*, array*/) {
if (typeof key !== 'string') {
throw new Error('config-cache#union expects `key` to be a string.');
}
var arr = this.get(key) || [];
var len = arguments.length - 1;
var args = new Array(len);
for (var i = 0; i < len; i++) {
args[i] = arguments[i + 1];
}
this.set(key, lazy.union(arr, lazy.flatten(args)));
this.emit('union', key);
return this;
};
/**
* Extend the `cache` with the given object.
* This method is chainable.
*
* ```js
* config
* .extend({foo: 'bar'}, {baz: 'quux'});
* .extend({fez: 'bang'});
* ```
*
* Or define the property to extend:
*
* ```js
* config
* // extend `cache.a`
* .extend('a', {foo: 'bar'}, {baz: 'quux'})
* // extend `cache.b`
* .extend('b', {fez: 'bang'})
* // extend `cache.a.b.c`
* .extend('a.b.c', {fez: 'bang'});
* ```
* @chainable
* @return {Object} `Config` to enable chaining
* @api public
*/
Config.prototype.extend = function() {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) {
args[i] = arguments[i];
}
var extend = lazy.extend;
if (typeof args[0] === 'string') {
var o = this.get(args[0]) || {};
o = extend.apply(extend, lazy.union([o], args.slice(1)));
this.set(args[0], o);
this.emit('extend');
return this;
}
extend.apply(extend, lazy.union([this.cache], args));
this.emit('extend');
return this;
};
/**
* Remove `keys` from the cache. If no value is
* specified the entire cache is reset.
*
* ```js
* config.del();
* ```
* @chainable
* @api public
*/
Config.prototype.del = function(keys) {
this.cache = keys ? lazy.omit(this.cache, keys) : {};
this.emit('del', keys);
return this;
};
/**
* Expose `Config`
*/
module.exports = Config;