-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (188 loc) · 5.54 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
/*!
* dush-options <https://github.com/tunnckoCore/dush-options>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var isObject = require('isobject')
var setValue = require('set-value')
var getValue = require('get-value')
var mixin = require('mixin-deep')
/**
* > A plugin for [dush][]/[minibase][]/[base][] that adds `.option`, `.enable` and `.disable`
* methods to your app. You can pass `options` to be merged with `app.options`
*
* **Example**
*
* ```js
* var dush = require('dush')
* var options = require('dush-options')
*
* var app = dush()
*
* // some initial options
* var opts = { foo: 'bar' }
*
* app.use(options(opts))
*
* console.log(app.options) // => { foo: 'bar' }
*
* console.log(app.option()) // => { foo: 'bar' }
* console.log(app.option) // => function
*
* console.log(app.enable) // => function
* console.log(app.disable) // => function
* ```
*
* @param {Object} `options` optional, initial options to set to `app.options` property
* @return {Function} a plugin function, pass it to `.use` method of [dush][]/[minibase][]/[base][]
* @api public
*/
module.exports = function dushOptions (options) {
return function (app) {
if (app.isRegistered && app.isRegistered('dush-options')) {
return
}
app.options = mixin({}, app.options, options)
/**
* > Set or get an option(s). Support dot notation syntax too.
* If there are no arguments it returns `app.options`.
* If `key` is string and no `value` argument, it gets that property
* from the `app.options` object - using [get-value][],
* so `app.option('foo.bar.qux')`. If `key` is object it is merged
* with `app.options` using [mixin-deep][]. If both `key` and `value` is given
* then it sets `value` to `key` property, using [set-value][] library.
*
* **Example**
*
* ```js
* var app = dush()
* app.use(options({ initial: 'props' }))
*
* console.log(app.options) // => { initial: 'props' }
* console.log(app.option()) // => { initial: 'props' }
*
* app.option({ foo: 'bar' })
* console.log(app.options)
* // => { initial: 'props', foo: 'bar' }
*
* app.option('qux', 123)
* console.log(app.options)
* // => { initial: 'props', foo: 'bar', qux: 123 }
*
* app.option('aa.bb.cc', 'dd')
* console.log(app.options)
* // => {
* // initial: 'props',
* // foo: 'bar',
* // qux: 123,
* // aa: { bb: { cc: 'dd' } }
* // }
*
* console.log(app.option('aa.bb')) // => { cc: 'dd' }
* console.log(app.option('aa')) // => { bb: { cc: 'dd' }
* console.log(app.option('foo')) // => 'bar'
* ```
*
* @param {String|Object} `key` path to some option property, e.g. `a.b.c`
* @param {any} `value` if `key` is string, any value to set to `key` property
* @return {Object} _clone_ of the modified `app.options` object, or some `key` value
* @api public
*/
app.option = function option (key, value) {
if (!arguments.length) {
app.emit('option', app.options)
// option:getAll app.options
return app.options
}
if (arguments.length === 1 && typeof key === 'string') {
var val = getValue(app.options, key)
app.emit('option', app.options, key, val)
// option:get key
return val
}
app.options = set(app, key, value)
return app.options
}
/**
* > Enables a `key` to have `true` value. It is simply just
* a shortcut for `app.option('foo', true)`.
*
* **Example**
*
* ```js
* app.use(options())
* console.log(app.options) // => {}
*
* app.enable('foo')
* console.log(app.options) // => { foo: true }
*
* app.enable('qux.baz')
* console.log(app.options) // => { foo: true, qux: { baz: true } }
* ```
*
* @param {String} `key` a path to property to enable
* @return {Object} always self for chaining
* @api public
*/
app.enable = function enable (key) {
app.emit('enable', key)
app.option(key, true)
return app
}
/**
* > Disable a `key` to have `false` value. It is simply just
* a shortcut for `app.option('zzz', false)`.
*
* **Example**
*
* ```js
* app.use(options())
* console.log(app.options) // => {}
*
* app.enable('foo')
* console.log(app.options) // => { foo: true }
*
* app.disable('foo')
* console.log(app.options) // => { foo: false }
*
* app.enable('qux.baz')
* console.log(app.options.qux) // => { baz: true }
*
* app.disable('qux.baz')
* console.log(app.options.qux) // => { baz: false }
* ```
*
* @param {String} `key` a path to property to disable
* @return {Object} always self for chaining
* @api public
*/
app.disable = function disable (key) {
app.emit('disable', key)
set(app, key, false)
return app
}
}
}
/**
* > Helper method. Exist just because
* the signal for "duplication" between `app.enable`
* and `app.disable` by `codeclimate`
*
* @param {Object} `app`
* @param {String} `key`
* @param {any} `value`
*/
function set (app, key, value) {
if (isObject(key)) {
app.emit('option', app.options, key)
// option:setAll key
app.options = mixin({}, app.options, key)
} else {
app.emit('option', app.options, key, value)
// option:set key, value
setValue(app.options, key, value)
}
return app.options
}