-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (122 loc) · 3.1 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
/*!
* dush-methods <https://github.com/tunnckoCore/dush-methods>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
/**
* > Plugin for [dush][], [minibase][], [base][] and anything based on them.
* It adds `.define` and `.delegate` methods on the `app` instance.
*
* **Example**
*
* ```js
* const dush = require('dush')
* const methods = require('dush-methods')
*
* const app = dush()
* app.use(methods())
*
* console.log(app.define) // => function
* console.log(app.delegate) // => function
* ```
*
* @name methods()
* @return {Function} a plugin function that should be passed to `.use` method
* @api public
*/
module.exports = function dushMethods () {
return function dushMethods (app) {
/* istanbul ignore next */
if (app.isRegistered && app.isRegistered('dush-methods')) {
return
}
/**
* > Add non-enumerable `prop` with a `value`.
* It also emits a `define` event.
*
* **Example**
*
* ```js
* app.define('foo', 123)
* console.log(app.foo) // => 123
*
* app.on('define', (key, value) => {
* console.log('key:', key) // => 'key: foo'
* console.log('value:', value) // => 'value: 123'
* })
*
* // or inside plugin
* app.use((app) => {
* app.define('hello', (place) => console.log(`Hello ${place}!`))
* })
*
* app.hello('world') // => 'Hello world!'
* ```
*
* @name .define
* @param {String} `prop` a name of the property
* @param {any} `value` any type of value
* @return {Object} instance for chaining
* @api public
*/
defineProp(app, 'define', function defineProp_ (prop, value) {
app.emit('define', prop, value)
defineProp(app, prop, value)
return app
})
/**
* > Calls the `.define` method for each property on `props` object.
* It also emits `delegate` event.
*
* **Example**
*
* ```js
* // called two times
* app.on('define', (key, value) => {
* console.log(key) // => `foo`, then `qux`
* console.log(value) // => `bar`, then `123`
* })
*
* // called one time
* app.on('delegate', (props) => {
* console.log('props:', props) // => { foo: 'bar', qux: 123 }
* })
*
* app.delegate({
* foo: 'bar',
* qux: 123
* })
* ```
*
* @name .delegate
* @param {Object} `props` an object of properties
* @return {Object} instance for chaining
* @api public
*/
defineProp(app, 'delegate', function delegateProps_ (props) {
app.emit('delegate', props)
for (var key in props) {
app.define(key, props[key])
}
return app
})
}
}
/**
* > Add non-enumerable `prop` with `val` to an `obj`.
*
* @param {Object} `obj`
* @param {String} `prop`
* @param {any} `val`
* @return {Object}
*/
function defineProp (obj, prop, val) {
return Object.defineProperty(obj, prop, {
configurable: true,
enumerable: false,
writable: true,
value: val
})
}