/
index.js
498 lines (419 loc) · 13.4 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/** @license Licenced under MIT - qbus - ©2015 Pehr Boman <github.com/unkelpehr> */
(function () {
'use strict';
var root = this;
/**
* Normalizes given path by converting double frontslashes to singles
* and removing the slashes from the beginning and end of the string.
*
* @param {String} path The path to normalize
* @return {String} Normalized path
*/
function normalizePath (path) {
// Remove double slashes
if (path.indexOf('//') !== -1) {
path = path.replace(/\/+/g, '/');
}
// Shift slash
if (path[0] === '/') {
path = path.substr(1);
}
// Pop slash
if (path[path.length - 1] === '/') {
path = path.substr(0, path.length - 1);
}
return path;
}
/**
* Helper function for fast execution of functions with dynamic parameters.
*
* @method exec
* @param {Function} func Function to execute
* @param {Object} context Object used as `this`-value
* @param {Array} args Array of arguments to pass
*/
function exec (func, context, args) {
var res;
switch (args.length) {
case 0: res = func.call(context); break;
case 1: res = func.call(context, args[0]); break;
case 2: res = func.call(context, args[0], args[1]); break;
case 3: res = func.call(context, args[0], args[1], args[2]); break;
case 4: res = func.call(context, args[0], args[1], args[2], args[3]); break;
default: res = func.apply(context, args); break;
}
return res;
}
/**
* Passes given query to 'this.exec' and shifts the first, mandatory, full match.
* This function is added as a property to all RegExp-objects that passes Qbus.parse.
*
* @method execQuery
* @param {String} query String execute
* @return {Null|Array>} Null if it does not match, otherwise Array.
*/
function execQuery (query) {
var match, i, arr;
if ((match = this.exec(query))) {
arr = new Array(match.length - 1);
for (i = 1; i < match.length; ++i) {
arr[i - 1] = match[i];
}
return arr;
}
return match;
};
/**
* Converts given expression to RegExp.
* If a RegExp is given it will copy it and add 'execQuery' to its properties.
*
* @method emit
* @param {String|RegExp} expr The string to convert or RegExp to add 'execQuery'.
* @return {RegExp}
*/
function parse (expr) {
var finalRegexp,
strSlashPref;
if (typeof expr !== 'string') {
// Handle RegExp `expr`
if (expr instanceof RegExp) {
finalRegexp = new RegExp(expr);
finalRegexp.query = execQuery;
return finalRegexp;
}
throw new TypeError(
'Usage: qbus.parse(<`query` = String|RegExp>)\n' +
'Got: qbus.parse(<`' + typeof expr + '` = ' + expr + '>)'
);
}
strSlashPref = expr[expr.length - 1] === '/';
// Trim slashes and remove doubles
expr = normalizePath(expr);
// Pass everything from and including possible beginning frontslash
// until and not including the next frontslash, to `parseLevel`.
expr = expr.replace(/\/?[^\/]+?(?=\/|$)/g, function (match, index) {
// Return level if it doesn't contain any modifiers.
// : must be preceeded by / or start of string to count
// ? must be used with valid : to count (so we don't need to check for that)
// * always counts
if (match.indexOf('*') === -1 && (match.length <= 2 || !/(^|\/)+\:+?/.test(match))) {
return match.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
var slashSuff = match[0] === '/' ? '/' : '',
slashPref = !!expr[match.length + index] || strSlashPref,
level = slashSuff ? match.substr(1) : match,
firstChar = level[0],
lastChar = level.substr(-1);
// Handle captures
if (firstChar === ':') {
// Handle optional :capture?
if (lastChar === '?') {
// Wrap any preceding slash in the optional capture group
return (slashSuff ? '(?:/([^/]+?))?' : '([^/]+?)?');
}
// Handle mandatory :capture
return slashSuff + '([^/]+?)';
}
// All matches that end with a slash or does not end with * are easy.
// We'll just choose to capture anything except a frontslash.
//
// /some/stuff*/ => /some/stuff([^/]+)?/
// /some/st*ff/ => /some/st([^/]+)?ff/
if (slashPref || lastChar !== '*') {
return slashSuff + level.replace(/\*/g, '([^/]+)?');
}
// Handle all matches that ends with * and are not followd by frontslash; 'stuff*', '*'.
// We'll replace all asterisks except the last with catch-alls. The last one is omitted and replaced with
// a pattern that matches everything up until, but not included, the last frontslash in the string or end-of-string.
//
// /some/stuff* => some/stuff(.*?(?:(?=\/$)|(?=$)))?
// /some/st*ff* => some/st(.*)?ff(.*?(?:(?=\/$)|(?=$)))?
// /* => (.*?(?:(?=\\/$)|(?=$)))?
return slashSuff + level.replace(/\*(?!$)/g, '(.*)?').slice(0, -1) + '(.*?(?:(?=/$)|(?=$)))?';
});
// Create RegExp object from the parsed query
finalRegexp = new RegExp('^/?' + expr + '/?$', 'i');
// Add `execQuery` to it's properties
finalRegexp.query = execQuery;
return finalRegexp;
};
/**
* Qbus constructor
*
* @class
* @return {Qbus} Qbus instance
*/
function Qbus (parent, proto) {
var self = this;
// Optional `new` keyword
if (!self) {
return new Qbus(parent, proto);
}
// Typechecking
if ((parent && !parent.emit) || (proto && typeof proto !== 'object')) {
throw new TypeError(
'Usage: new Qbus([<`parent` = Object>, <`proto` = Object>])\n'+
'Got: new Qbus(<`' + typeof parent + '` = ' + parent + '>, <`' + typeof proto + '` = ' + proto + '>)'
);
}
// Create storage namespace
self.qbus = {
paths: {},
parse: parse,
parent: parent || null
};
// Hide it if possible
if (Object.defineProperty) {
Object.defineProperty(self, 'qbus', {
enumerable: false
});
}
if (proto) {
proto.on = Qbus.prototype.on;
proto.once = Qbus.prototype.once;
proto.off = Qbus.prototype.off;
proto.emit = Qbus.prototype.emit;
}
return self;
}
/**
* Extracts the static portion of a query; i.e. everything up until a modifier.
* /* = /
* /get/some/:stuff?/ = /get/some
* /get/some/* = /get/some
* /get/some/stu* = /get/some
* /get/some/stuff/ = /get/some/stuff
*
* @param {String} query The query to extract the static portion of
* @return {String} The static portion of the query given
*/
function getFixed (query) {
var fixed, iOP, iOW;
// Search for the the first occurence of a wildcard or capture portion
iOP = query.search(/(^|\/)+?:+?[^\/]+?/);
iOW = query.search(/(^|\/)+?[^\/]*?\*{1}/);
// Both negative - static query
if (iOP === -1 && iOW === -1) {
return query;
}
// Extract static portion
fixed = query.substr(0, Math.min(
iOP !== -1 ? iOP : query.length,
iOW !== -1 ? iOW : query.length
));
// Pop slash
if (fixed[fixed.length - 1] === '/') {
fixed = fixed.substr(0, fixed.length - 1);
}
return fixed;
}
/**
* Attaches a new query handler for the given function.
*
* @method on
* @param {String|RegExp} expr Expression to match against. String or a RegExp object
* @param {Function} handler A function to execute when the expression is matched.
* @return {Object} `this`
*/
Qbus.prototype.on = function (expr, handler) {
var paths = this.qbus.paths,
normal,
fixed,
isRegExp = expr instanceof RegExp;
if ((!isRegExp && typeof expr !== 'string') || typeof handler !== 'function') {
throw new TypeError(
'Usage: qbus.on(<`expr` = String|RegExp>, <`handler` = Function>)\n'+
'Got: qbus.on(<`' + typeof expr + '` = ' + expr + '>, <`' + typeof handler + '` = ' + handler + '>)'
);
}
// Handle RegExp queries
if (isRegExp) {
(paths['/'] || (paths['/'] = [])).push({
input: expr.source,
handler: handler,
expr: parse(expr)
});
return this;
}
// Trim slashes and remove doubles
normal = normalizePath(expr);
// Get the static portion of the expression or fallback on '/'.
fixed = getFixed(normal) || '/';
// Create namespace
if (!paths[fixed]) {
paths[fixed] = [];
}
// All done
paths[fixed].push({
input: normal,
handler: handler,
// If the fixed portion of the expr equals the normal
// then this is a simple, non-regexp expr that can use string comparison.
expr: normal === fixed ? normal : parse(expr)
});
return this;
};
/**
* Attaches a new query handler for the given function.
* The query handler will only be called once.
*
* @method on
* @param {String|RegExp} expr Expression to match against. String or a RegExp object
* @param {Function} handler A function to execute when the expression is matched.
* @return {Object} `this`
*/
Qbus.prototype.once = function (expr, handler) {
var self = this;
if (typeof handler !== 'function' || (typeof expr !== 'string' && !(expr instanceof RegExp))) {
throw new TypeError(
'Usage: qbus.once(<`expr` = String|RegExp>, <`handler` = Function>)\n'+
'Got: qbus.once(<`' + typeof expr + '` = ' + expr + '>, <`' + typeof handler + '` = ' + handler + '>)'
);
}
return this.on(expr, function temp () {
var i = 0,
args = new Array(arguments.length);
for (; i < args.length; ++i) {
args[i] = arguments[i];
}
self.off(expr, temp);
exec(handler, self, args);
});
};
/**
* Removes all subscriptions matching `expr` and the optional `handler` function.
*
* @method off
* @param {String|RegExp} expr Expression to match
* @param {Function=} handler Function to match
* @return {Object} `this`
*/
Qbus.prototype.off = function (expr, handler) {
var paths = this.qbus.paths,
isRegExp,
parent,
i;
if ((typeof expr !== 'string' && !(isRegExp = expr instanceof RegExp)) || (typeof handler !== 'undefined' && typeof handler !== 'function')) {
throw new TypeError(
'Usage: qbus.off(<`expr` = String|RegExp>[, <`handler` = Function>])\n'+
'Got: qbus.off(<`' + typeof expr + '` = ' + expr + '>, <`' + typeof handler + '` = ' + handler + '>)'
);
}
// Convert RegExp' queries to strings
if (isRegExp) {
expr = expr.source;
parent = paths['/'];
} else {
expr = normalizePath(expr);
parent = paths[getFixed(expr) || '/'];
}
if (parent) {
for (i = 0; i < parent.length; ++i) {
if (parent[i].input === expr && (!handler || parent[i].handler === handler)) {
parent.splice(--i, 1);
}
}
}
return this;
};
/**
* Executes all stored handlers that has an expression that matches `query`.
*
* @method emit
* @param {String} query The string to match against
* @return {Object} `this`
*/
Qbus.prototype.emit = function (query) {
var paths = this.qbus.paths,
i, x,
sub,
match,
args = [],
parent,
needle,
returned,
slashEnd,
normal,
argsLen = arguments.length;
// Get all arguments after `query` as a regular array
if (argsLen > 1) {
for (i = 1; i < argsLen; ++i) {
args.push(arguments[i]);
}
}
// Typecheck after converting the arguments to a regular array so we can include `args` in the message.
// Dropping the `arguments` bomb causes V8 bailout: Bad value context for arguments value.
if (typeof query !== 'string') {
throw new TypeError(
'Usage: qbus.emit(<`query` = String>[, <`arg1` = *>], <`arg2` = *>, ...)\n'+
'Got: qbus.emit(<`' + typeof query + '` = ' + query + '>, ' + args + ')'
);
}
slashEnd = query[query.length - 1] == '/' ? '/' : '';
// `needle` will be modified while we look for listeners so
// `normal` will be the value that we'll compare against.
needle = normal = normalizePath(query);
// Skip a do...while by setting `needle` to '/' if it's empty.
// It will be empty if the query equaled '/' before normalizePath trimmed the slashes.
needle = needle || '/';
while (needle) {
parent = paths[needle];
if (parent) {
for (i = 0; i < parent.length; ++i) {
sub = parent[i];
// RegExp matching
if (sub.expr.query) {
if ((match = sub.expr.query(normal + slashEnd))) {
// Extend `args` into matches
for (x = 0; x < args.length; ++x) {
match.push(args[x]);
}
returned = exec(sub.handler, this, match);
}
}
// String comparison
else if (normal == sub.expr) {
returned = exec(sub.handler, this, args);
}
// Discontinue if a handler returned false
if (returned === false) {
return this;
}
}
}
// Break after processing '/'
if (needle.length === 1) {
break;
}
// For each run we pop a part of the needle
// 'foo/bar/baz'.substr(0, 7) => foo/bar
// 'foo/bar'
// 'foo'
// ''
//
// By looping it backwards we let the most explicit listeners
// have a running chance to break the loop by returning false.
//
// They are also guaranteed to be executed before a less explicit
// listener breaks the loop.
needle = needle.substr(0, needle.lastIndexOf('/')) || '/';
}
// Bubble
if (this.qbus.parent) {
this.qbus.parent.emit.apply(this.qbus.parent, arguments);
}
return this;
};
// Add parse as a property to the constructor
Qbus.parse = parse;
// Expose
if (typeof module != 'undefined' && typeof module.exports === 'object') {
module.exports = Qbus;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return Qbus;
});
} else {
root.Qbus = Qbus;
}
}).call(this);