-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mode.js
120 lines (106 loc) · 2.25 KB
/
mode.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
'use strict';
var utils = require('./utils');
var identity = utils.identity;
var types = ['mode', 'toggle'];
/**
* Mode constructor for making a mode object when
* a mode is created with `logger.mode()`
*
* @param {Object} `options` Options to configure the mode.
* @param {String} `options.name` Required name of the mode
* @param {String|Type} `options.type` Type of mode to create. Defaults to `mode`. Values may be `['mode', 'toggle']`.
* @api public
*/
function Mode(options) {
if (!(this instanceof Mode)) {
return new Mode(options);
}
this.options = utils.extend({type: 'mode'}, options);
if (!this.name) {
throw new Error('expected options.name to be set');
}
this.type = this.options.type;
}
/**
* Type of `mode`. Valid types are ['mode', 'toggle']
*
* ```js
* console.log(verbose.type);
* //=> "mode"
* console.log(not.type);
* //=> "toggle"
* ```
*
* @name type
* @api public
*/
utils.define(Mode.prototype, 'type', {
enumerable: true,
set: function(val) {
val = utils.arrayify(val);
utils.assertType(types, val);
this.options.type = val;
},
get: function() {
return this.options.type;
}
});
/**
* Readable name of `mode`.
*
* ```js
* console.log(verbose.name);
* //=> "verbose"
* console.log(not.name);
* //=> "not"
* ```
*
* @name name
* @api public
*/
utils.define(Mode.prototype, 'name', {
enumerable: true,
set: function(val) {
this.options.name = val;
},
get: function() {
return this.options.name;
}
});
/**
* Optional modifier function that accepts a value and returns a modified value.
* When not present, an identity function is used to return the original value.
*
* ```js
* var msg = "some error message";
*
* // wrap message in ansi codes for "red"
* msg = red.fn(msg);
* console.log(msg);
*
* //=> "\u001b[31msome error message\u001b[39m";
* ```
*
* @name `fn`
* @api public
*/
utils.define(Mode.prototype, 'fn', {
set: function(fn) {
this.options.fn = fn;
},
get: function() {
return this.options.fn || identity;
}
});
/**
* Custom inspect method for displaying the mode on the console.
*
* @return {String} mode.name
*/
Mode.prototype.inspect = function() {
return this.name;
};
/**
* Exposes `Mode`
*/
module.exports = Mode;