-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (48 loc) · 1.29 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
/*!
* path-ends-with <https://github.com/jonschlinkert/path-ends-with>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var path = require('path');
module.exports = function(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 (filepath === '' || substr === '') {
return false;
}
if (filepath === substr) {
return true;
}
if (options && options.nocase === true) {
filepath = filepath.toLowerCase();
substr = substr.toLowerCase();
}
if (substr[0] === '/' || substr[0] === '\\') {
return startsWith(filepath, substr);
}
var a = filepath.split(/[\\/]+/);
var b = substr.split(/[\\/]+/);
if (options && options.partialMatch === true) {
return startsWith(a.join('/'), b.join('/'));
}
if (b.length === 1) {
var last = a[a.length - 1];
var ext = path.extname(last);
return last === substr || ext === substr || ext.slice(1) === substr;
}
while (b.length && a.length) {
if (b.pop() !== a.pop()) {
return false;
}
}
return true;
};
function startsWith(a, b) {
return a.slice(-b.length) === b;
}