-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (89 loc) · 2.46 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
/*!
* minibase-is-registered <https://github.com/node-minibase/minibase-is-registered>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var methods = require('dush-methods')
/**
* > Adds `.isRegistered` method to your application. That
* `opts` option is optional and does nothing. It is just convention
* each plugin to export function that returns a plugin.
*
* **Example**
*
* ```js
* var isRegistered = require('minibase-is-registered')
*
* var MiniBase = require('minibase').MiniBase
* var app = new MiniBase()
* app.use(isRegistered())
*
* // or as Base plugin
*
* var Base = require('base')
* var base = new Base()
* base.use(isRegistered())
* ```
*
* @param {Object} `opts` optional, no options currently
* @return {Function} plugin that can be pass to [base][]/[minibase][]'s `.use` method
* @api public
*/
module.exports = function minibaseIsRegistered (opts) {
return function minibaseIsRegistered (self) {
/* istanbul ignore next */
if (self.isRegistered && self.isRegistered('is-registered')) {
return
}
self.use(methods())
var registered = self.registered && typeof self.registered === 'object'
? self.registered
: {}
self.define('registered', registered)
/**
* > Checks if given `name` exists in `app.registered`
* cache object, to detect if should call the plugin or not.
* It also returns `false` if not a string passed to it.
*
* **Example**
*
* ```js
* app.use(isRegistered())
*
* var called = 0
*
* function fakePlugin () {
* return function foo (app) {
* if (app.isRegistered('foo')) return
* called = called + 22
* }
* }
*
* app.use(fakePlugin())
* app.use(fakePlugin())
*
* // the plugin `fakePlugin` is called only once
* // if it was called two times `called` will be 44
* console.log(called) // => 22
* ```
*
* @name .isRegistered
* @param {String} `name` name of the plugin
* @return {Boolean} always boolean `true` or `false`
* @api public
*/
self.define('isRegistered', function isRegistered (name) {
if (typeof name !== 'string') {
return false
}
if (self.registered.hasOwnProperty(name)) {
return true
}
self._pluginName = name
self.registered[name] = true
return false
})
}
}