-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.js
147 lines (130 loc) · 3.27 KB
/
stats.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
'use strict';
var utils = require('./utils');
/**
* Stats contructor that contains information
* about a chained event being built up.
*
* ```js
* {
* // "not" => toggle, "verbose" => mode
* modes: ['not', 'verbose'],
*
* // "red" => modifier
* styles: ['red'],
*
* // specified when emitter is created
* level: 1,
*
* // name of emitter that will trigger an event
* // in this case "red" will not trigger an event
* name: 'subhead',
*
* // arguments passed into emitter function "subhead"
* args: ['foo', 'bar', 'baz']
* }
* ```
*
* @param {Object} `parent` Optional stats instance to inherit `modes` and `styles` from.
* @api public
*/
function Stats(parent) {
if (!(this instanceof Stats)) {
return new Stats(parent);
}
this.initStats(parent);
}
/**
* Initialize the stats object with modes and styles from the parent.
*
* @param {Object} `parent` Optional parent object to inherit stats from.
* @return {Object} `this` for chaining.
*/
Stats.prototype.initStats = function(parent) {
this.modes = utils.arrayify(parent ? parent.modes : this.modes);
this.styles = utils.arrayify(parent ? parent.styles : this.styles);
return this;
};
/**
* Helper method to union a value onto an array property.
* See `addMode` and `addStyle`.
*
* @param {String} `prop` Name of property to union to.
* @param {*} `val` Value to union on the property.
* @return {Object} `this` for chaining.
*/
Stats.prototype.union = function(prop, val) {
utils.union(this, prop, val);
return this;
};
/**
* Add a mode to the `modes` array for this stats object.
*
* ```js
* var verbose = new Mode({name: 'verbose'});
* stats.addMode(verbose);
* ```
*
* @param {Object} `mode` Instance of a Mode to add to the stats object.
* @return {Object} `this` for chaining.
* @api public
*/
Stats.prototype.addMode = function(mode) {
return this.union('modes', mode);
};
/**
* Get the array of modes from the stats object.
* Optionally, pass a property in and return an array with
* only the property.
*
* ```js
* var modes = stats.getModes();
* //=> [{name: 'verbose'}]
* var modeNames = stats.getModes('name');
* //=> ['verbose']
* ```
*
* @param {String} `prop` Optional property to pick from the mode objects to return.
* @return {Array} Array of modes or mode properties.
* @api public
*/
Stats.prototype.getModes = function(prop) {
if (!prop) return this.modes;
return this.modes.map(function(mode) {
return mode[prop];
});
};
/**
* Add a style to the `styles` array for this stats object.
*
* ```js
* stats.addStyle('red');
* ```
*
* @param {String} `style` Name of style to add.
* @return {Object} `this` for chaining.
* @api public
*/
Stats.prototype.addStyle = function(style) {
return this.union('styles', style);
};
/**
* Sets the emitter for this stats object to indicate this is a complete stats object ready to be emitted.
*
* ```js
* var info = new Emitter({name: 'info'});
* stats.addEmitter(info);
* ```
*
* @param {Object} `emitter` Instance of a Emitter to add to the stats object.
* @return {Object} `this` for chaining.
* @api public
*/
Stats.prototype.addEmitter = function(emitter) {
this.name = emitter.name;
this.emitter = emitter;
return this;
};
/**
* Exposes `Stats`
*/
module.exports = Stats;