-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
getPathMatches.js
57 lines (53 loc) · 1.43 KB
/
getPathMatches.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
'use strict';
var getPathToString = require('./getPathToString.js');
function getPathMatches(_) {
var pathToString = getPathToString(_);
function pathMatches(path, paths) {
var pathString;
var pathArray;
if (_.isString(path)) {
pathString = path;
} else {
pathArray = path;
}
if (!Array.isArray(paths)) {
paths = [paths];
} else {
paths = _.cloneDeep(paths);
}
for (var i = 0; i < paths.length; i++) {
if (_.isString(paths[i])) {
paths[i] = _.toPath(paths[i]);
}
if (Array.isArray(paths[i])) {
if (pathArray === undefined) {
pathArray = _.toPath(pathString);
}
if (
pathArray.length >= paths[i].length &&
_.isEqual(_.takeRight(pathArray, paths[i].length), paths[i])
) {
// console.log('path matched');
return paths[i];
}
} else if (paths[i] instanceof RegExp) {
if (pathString === undefined) {
pathString = pathToString(path);
}
if (paths[i].test(pathString)) {
// console.log('regex matched', paths[i]);
return paths[i];
}
} else {
throw new Error(
'To match path use only string/regex or array of them.'
);
}
}
// console.log('matched nothing');
return false;
}
return pathMatches;
}
getPathMatches.notChainable = true;
module.exports = getPathMatches;