-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
58 lines (55 loc) · 1.87 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
module.exports = matcher;
function matcher(obj, flags) {
"use strict";
var props = Object.getOwnPropertyNames(obj);
var parserArgs = [];
var re = new RegExp(props.reduce(function(p, c) {
if(!isNaN(c)) {
throw new TypeError("Objects with numeric keys are not supported." +
"Got object with key: " + c + " for object: " + obj);
}
var val = getVal(obj, c, parserArgs);
if(c[0] === "_") return p + val;
else return p + "(" + val + ")";
}, ""), flags || "");
var _nonCapturingRe = props.map(function(p) { return obj[p]; }).join("");
props = props.filter(function(x) { return x[0] !== "_"; });
var parser = function(str) {
var o = {};
var res = re.exec(str);
if(res === null) return null;
for(var i = 0; i < props.length; i++) {
if(parserArgs[i]) { // got a parser
o[props[i]] = parserArgs[i](res[i + 1]); // invoke the parser
} else {
o[props[i]] = res[i + 1];
}
}
return o;
};
parser._nonCapturingRe = _nonCapturingRe; // expose raw RE for nesting
parser.re = re; // expose regexp
return parser;
}
function getVal(obj, prop, extraParsers) {
"use strict";
var val = obj[prop];
var isParser = Boolean(val.re);
var isRegExp = Boolean(val.source);
var isObject = Object(val) === val;
// convert objects that are not matchers or RegExps to nested parsers
if (isObject && !isParser && !isRegExp) {
val = matcher(val);
}
isParser = Boolean(val.re); // update in case we now converted it to a parser
if (isParser) { // accept parsers as arguments
extraParsers.push(val); // note this is a parser
val = val._nonCapturingRe; // don't capture for parser arguments
} else if (isRegExp) {
extraParsers.push(null); // not a parser
val = val.source;
} else {
extraParsers.push(null); // not a parser
}
return val;
}