-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
270 lines (240 loc) · 7.74 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
const assert = require('assert');
const typeOf = require('kind-of');
const define = require('define-property');
const pascal = require('pascalcase');
const merge = require('mixin-deep');
const Cache = require('cache-base');
/**
* Optionally define a custom `cache` namespace to use.
*/
function namespace(name) {
const fns = [];
/**
* Create an instance of `Base` with the given `cache` and `options`.
* Learn about the [cache object](#cache-object).
*
* ```js
* // initialize with `cache` and `options`
* const app = new Base({isApp: true}, {abc: true});
* app.set('foo', 'bar');
*
* // values defined with the given `cache` object will be on the root of the instance
* console.log(app.baz); //=> undefined
* console.log(app.foo); //=> 'bar'
* // or use `.get`
* console.log(app.get('isApp')); //=> true
* console.log(app.get('foo')); //=> 'bar'
*
* // values defined with the given `options` object will be on `app.options
* console.log(app.options.abc); //=> true
* ```
*
* @name Base
* @param {Object} `cache` If supplied, this object is passed to [cache-base][] to merge onto the the instance.
* @param {Object} `options` If supplied, this object is used to initialize the `base.options` object.
* @api public
*/
class Base extends Cache {
constructor(cache, options) {
super(name, cache);
this.is('base');
this.is('app');
this.options = merge({}, this.options, options);
this.cache = this.cache || {};
this.define('registered', {});
if (fns.length) {
this.use(fns);
}
}
/**
* Set the given `name` on `app._name` and `app.is*` properties. Used for doing
* lookups in plugins.
*
* ```js
* app.is('collection');
* console.log(app.type);
* //=> 'collection'
* console.log(app.isCollection);
* //=> true
* ```
* @name .is
* @param {String} `name`
* @return {Boolean}
* @api public
*/
is(type) {
assert.equal(typeof type, 'string', 'expected "type" to be a string');
if (type !== 'app') delete this.isApp;
this.define('type', type.toLowerCase());
this.define('is' + pascal(type), true);
return this;
}
/**
* Returns true if a plugin has already been registered on an instance.
*
* Plugin implementors are encouraged to use this first thing in a plugin
* to prevent the plugin from being called more than once on the same
* instance.
*
* ```js
* const base = new Base();
* base.use(function(app) {
* if (app.isRegistered('myPlugin')) return;
* // do stuff to `app`
* });
*
* // to also record the plugin as being registered
* base.use(function(app) {
* if (app.isRegistered('myPlugin', true)) return;
* // do stuff to `app`
* });
* ```
* @name .isRegistered
* @emits `plugin` Emits the name of the plugin being registered. Useful for unit tests, to ensure plugins are only registered once.
* @param {String} `name` The plugin name.
* @param {Boolean} `register` If the plugin if not already registered, to record it as being registered pass `true` as the second argument.
* @return {Boolean} Returns true if a plugin is already registered.
* @api public
*/
isRegistered(name, register) {
assert.equal(typeof name, 'string', 'expected name to be a string');
if (this.registered.hasOwnProperty(name)) {
return true;
}
if (register !== false) {
this.registered[name] = true;
this.emit('plugin', name);
}
return false;
}
/**
* Call a plugin function or array of plugin functions on the instance. Plugins
* are called with an instance of base, and options (if defined).
*
* ```js
* const app = new Base()
* .use([foo, bar])
* .use(baz)
* ```
* @name .use
* @param {String|Function|Array} `name` (optional) plugin name
* @param {Function|Array} `plugin` plugin function, or array of functions, to call.
* @param {...rest} Any additional arguments to pass to plugins(s).
* @return {Object} Returns the item instance for chaining.
* @api public
*/
use(...rest) {
let name = null;
let fns = null;
if (typeof rest[0] === 'string') {
name = rest.shift();
}
if (typeof rest[0] === 'function' || Array.isArray(rest[0])) {
fns = rest.shift();
}
if (Array.isArray(fns)) return fns.forEach(fn => this.use(fn, ...rest));
assert.equal(typeof fns, 'function', 'expected plugin to be a function');
const key = name;
if (key && typeof key === 'string' && this.isRegistered(key)) {
return this;
}
fns.call(this, this, ...rest);
return this;
}
/**
* The `.define` method is used for adding non-enumerable property on the instance.
* Dot-notation is **not supported** with `define`.
*
* ```js
* // example of a custom arbitrary `render` function created with lodash's `template` method
* app.define('render', (str, locals) => _.template(str)(locals));
* ```
* @name .define
* @param {String} `key` The name of the property to define.
* @param {any} `value`
* @return {Object} Returns the instance for chaining.
* @api public
*/
define(key, val) {
if (typeOf(key) === 'object') {
return this.visit('define', key);
}
define(this, key, val);
return this;
}
/**
* Getter/setter used when creating nested instances of `Base`, for storing a reference
* to the first ancestor instance. This works by setting an instance of `Base` on the `parent`
* property of a "child" instance. The `base` property defaults to the current instance if
* no `parent` property is defined.
*
* ```js
* // create an instance of `Base`, this is our first ("base") instance
* const first = new Base();
* first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later
*
* // create another instance
* const second = new Base();
* // create a reference to the first instance (`first`)
* second.parent = first;
*
* // create another instance
* const third = new Base();
* // create a reference to the previous instance (`second`)
* // repeat this pattern every time a "child" instance is created
* third.parent = second;
*
* // we can always access the first instance using the `base` property
* console.log(first.base.foo);
* //=> 'bar'
* console.log(second.base.foo);
* //=> 'bar'
* console.log(third.base.foo);
* //=> 'bar'
* ```
* @name .base
* @api public
*/
get base() {
return this.parent ? this.parent.base : this;
}
/**
* Static method for adding global plugin functions that will
* be added to an instance when created.
*
* ```js
* Base.use(function(app) {
* app.foo = 'bar';
* });
* const app = new Base();
* console.log(app.foo);
* //=> 'bar'
* ```
* @name #use
* @param {Function} `fn` Plugin function to use on each instance.
* @return {Object} Returns the `Base` constructor for chaining
* @api public
*/
static use(fn) {
assert.equal(typeof fn, 'function', 'expected plugin to be a function');
fns.push(fn);
return this;
}
/**
* Delete static mixin method from cache-base, JIT
*/
static get mixin() {
return undefined;
}
}
return Base;
}
/**
* Expose `Base` with default settings
*/
module.exports = namespace();
/**
* Allow users to define a namespace
*/
module.exports.namespace = namespace;