-
Notifications
You must be signed in to change notification settings - Fork 3
/
switcher.js
51 lines (51 loc) · 1.74 KB
/
switcher.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
/*!
* switcher 1.0 (c) 2012 Nico Wiedemann - MIT license
* https://github.com/EarMaster/switcher
*/
!function (definition) {
// AMD
if (typeof define==='function' && define.amd)
define(definition);
// node.js
else if (typeof module!=='undefined' && typeof module.exports!=='undefined')
module.exports = definition();
// browser
else
self.switcher = definition();
}(function () {
'use strict';
/**
* function to provide a similar syntax to a switch statement but with regex as cases
* @param {String} scope string on which the regexes should be tested against
* @param {Object} tests object containing regexes as keys and callback functions as values which will be called with the scope as first parameter if the regex match.
* @param {Object} options object containing options like breakMode (see https://github.com/EarMaster/switcher for full list of options)
* @return {Boolean} returns true if one or more tests matched
*/
var switcher = function (scope, tests, options) {
var defaultOptions = {
breakMode: true // if set to true switcher will stop after the first match
};
if (!options)
options = defaultOptions;
else
for (var option in defaultOptions)
if (!options.hasOwnProperty(option))
options[option] = defaultOptions[option];
var matched = false;
for (var test in tests) {
test = {
fullExpression: test,
expression: test.substr(0,1)=='/'?test.substr(1, test.lastIndexOf('/')-1):test,
flags: test.substr(0,1)=='/'?test.substr(test.lastIndexOf('/')+1):''
};
if (new RegExp(test.expression, test.flags).test(scope)) {
matched = true;
tests[test.fullExpression](scope, test.fullExpression);
if (options.breakMode)
break;
}
}
return matched;
};
return switcher;
});