-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathruler.js
265 lines (222 loc) · 5.21 KB
/
ruler.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Ruler is a helper class for building responsibility chains from
* parse rules. It allows:
*
* - easy stack rules chains
* - getting main chain and named chains content (as arrays of functions)
*
* Helper methods, should not be used directly.
* @api private
*/
export default function Ruler() {
// List of added rules. Each element is:
//
// { name: XXX,
// enabled: Boolean,
// fn: Function(),
// alt: [ name2, name3 ] }
//
this.__rules__ = [];
// Cached rule chains.
//
// First level - chain name, '' for default.
// Second level - digital anchor for fast filtering by charcodes.
//
this.__cache__ = null;
}
/**
* Find the index of a rule by `name`.
*
* @param {String} `name`
* @return {Number} Index of the given `name`
* @api private
*/
Ruler.prototype.__find__ = function (name) {
var len = this.__rules__.length;
var i = -1;
while (len--) {
if (this.__rules__[++i].name === name) {
return i;
}
}
return -1;
};
/**
* Build the rules lookup cache
*
* @api private
*/
Ruler.prototype.__compile__ = function () {
var self = this;
var chains = [ '' ];
// collect unique names
self.__rules__.forEach(function (rule) {
if (!rule.enabled) {
return;
}
rule.alt.forEach(function (altName) {
if (chains.indexOf(altName) < 0) {
chains.push(altName);
}
});
});
self.__cache__ = {};
chains.forEach(function (chain) {
self.__cache__[chain] = [];
self.__rules__.forEach(function (rule) {
if (!rule.enabled) {
return;
}
if (chain && rule.alt.indexOf(chain) < 0) {
return;
}
self.__cache__[chain].push(rule.fn);
});
});
};
/**
* Ruler public methods
* ------------------------------------------------
*/
/**
* Replace rule function
*
* @param {String} `name` Rule name
* @param {Function `fn`
* @param {Object} `options`
* @api private
*/
Ruler.prototype.at = function (name, fn, options) {
var idx = this.__find__(name);
var opt = options || {};
if (idx === -1) {
throw new Error('Parser rule not found: ' + name);
}
this.__rules__[idx].fn = fn;
this.__rules__[idx].alt = opt.alt || [];
this.__cache__ = null;
};
/**
* Add a rule to the chain before given the `ruleName`.
*
* @param {String} `beforeName`
* @param {String} `ruleName`
* @param {Function} `fn`
* @param {Object} `options`
* @api private
*/
Ruler.prototype.before = function (beforeName, ruleName, fn, options) {
var idx = this.__find__(beforeName);
var opt = options || {};
if (idx === -1) {
throw new Error('Parser rule not found: ' + beforeName);
}
this.__rules__.splice(idx, 0, {
name: ruleName,
enabled: true,
fn: fn,
alt: opt.alt || []
});
this.__cache__ = null;
};
/**
* Add a rule to the chain after the given `ruleName`.
*
* @param {String} `afterName`
* @param {String} `ruleName`
* @param {Function} `fn`
* @param {Object} `options`
* @api private
*/
Ruler.prototype.after = function (afterName, ruleName, fn, options) {
var idx = this.__find__(afterName);
var opt = options || {};
if (idx === -1) {
throw new Error('Parser rule not found: ' + afterName);
}
this.__rules__.splice(idx + 1, 0, {
name: ruleName,
enabled: true,
fn: fn,
alt: opt.alt || []
});
this.__cache__ = null;
};
/**
* Add a rule to the end of chain.
*
* @param {String} `ruleName`
* @param {Function} `fn`
* @param {Object} `options`
* @return {String}
*/
Ruler.prototype.push = function (ruleName, fn, options) {
var opt = options || {};
this.__rules__.push({
name: ruleName,
enabled: true,
fn: fn,
alt: opt.alt || []
});
this.__cache__ = null;
};
/**
* Enable a rule or list of rules.
*
* @param {String|Array} `list` Name or array of rule names to enable
* @param {Boolean} `strict` If `true`, all non listed rules will be disabled.
* @api private
*/
Ruler.prototype.enable = function (list, strict) {
list = !Array.isArray(list)
? [ list ]
: list;
// In strict mode disable all existing rules first
if (strict) {
this.__rules__.forEach(function (rule) {
rule.enabled = false;
});
}
// Search by name and enable
list.forEach(function (name) {
var idx = this.__find__(name);
if (idx < 0) {
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = true;
}, this);
this.__cache__ = null;
};
/**
* Disable a rule or list of rules.
*
* @param {String|Array} `list` Name or array of rule names to disable
* @api private
*/
Ruler.prototype.disable = function (list) {
list = !Array.isArray(list)
? [ list ]
: list;
// Search by name and disable
list.forEach(function (name) {
var idx = this.__find__(name);
if (idx < 0) {
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = false;
}, this);
this.__cache__ = null;
};
/**
* Get a rules list as an array of functions.
*
* @param {String} `chainName`
* @return {Object}
* @api private
*/
Ruler.prototype.getRules = function (chainName) {
if (this.__cache__ === null) {
this.__compile__();
}
return this.__cache__[chainName] || [];
};