-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (41 loc) · 1.38 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
'use strict';
var stringFormat = require('string-format'),
escapeStringRegexp = require('escape-string-regexp'),
traverse = require('traverse'),
flowright = require('lodash.flowright');
var regexFormat = function (spec) {
spec = RegExp(spec);
var params = [].slice.call(arguments, 1);
var source = spec.source,
flags = spec.toString().match('/([^/]*)$')[1];
// Escape substitutions.
var escapedParams = traverse(params).map(function (node) {
if (this.isLeaf) {
if (typeof node == 'function') {
this.update(flowright(escapeStringRegexp, String, node));
}
else {
this.update(escapeStringRegexp(String(node)));
}
}
});
// Whether a format group was matched ("{}" or "{#...}"),
// contrary to the ordinary RegExp syntax ("{1}", "{1,2}").
var group;
source = source.replace(/(\\*)(\{|\})([#}]?)/g, function (match, backslashes, brace, hash) {
if (backslashes.length % 2) {
// Escaped brace.
return backslashes + brace + brace + hash;
}
else if (brace == '{') {
group = !!hash;
return backslashes + brace + (group ? '' : brace) + (hash == '}' ? hash : '');
}
else {
return backslashes + brace + (group ? '' : brace) + hash;
}
});
source = stringFormat.apply(null, [source].concat(escapedParams));
return RegExp(source, flags);
};
module.exports = regexFormat;