-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
98 lines (78 loc) · 2.13 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
'use strict';
var startsWith = require('path-starts-with');
var normalizePath = require('normalize-path');
function containsPath(filepath, substr, options) {
if (typeof filepath !== 'string') {
throw new TypeError('expected filepath to be a string');
}
if (typeof substr !== 'string') {
throw new TypeError('expected substring to be a string');
}
if (substr === '') {
return false;
}
// return true if the given strings are an exact match
if (filepath === substr) {
return true;
}
if (substr.charAt(0) === '!') {
return !containsPath(filepath, substr.slice(1), options);
}
options = options || {};
if (options.nocase === true) {
filepath = filepath.toLowerCase();
substr = substr.toLowerCase();
}
var fp = normalize(filepath, false);
var str = normalize(substr, false);
// return false if the normalized substring is only a slash
if (str === '/') {
return false;
}
// if normalized strings are equal, return true
if (fp === str) {
return true;
}
if (startsWith(filepath, substr, options)) {
return true;
}
var idx = fp.indexOf(str);
var prefix = substr.slice(0, 2);
// if the original substring started with "./", we'll
// assume it should match from the beginning of the string
if (prefix === './' || prefix === '.\\') {
return idx === 0;
}
if (idx !== -1) {
if (options.partialMatch === true) {
return true;
}
// if the first character in the substring is a
// dot or slash, we can consider this a match
var ch = str.charAt(0);
if (ch === '/') {
return true;
}
// since partial matches were not enabled, we only consider
// this a match if the next character is a dot or a slash
var before = fp.charAt(idx - 1);
var after = fp.charAt(idx + str.length);
return (before === '' || before === '/')
&& (after === '' || after === '/');
}
return false;
}
/**
* Normalize paths
*/
function normalize(str) {
str = normalizePath(str, false);
if (str.slice(0, 2) === './') {
str = str.slice(2);
}
return str;
}
/**
* Expose `containsPath`
*/
module.exports = containsPath;