-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathspecial-chars.js
136 lines (121 loc) · 5.33 KB
/
special-chars.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
var path = require('path');
var assert = require('assert');
var isWindows = require('is-windows');
var nm = require('./support/match');
describe('special characters', function() {
describe('regex', function() {
it('should match common regex characters', function() {
var fixtures = ['a c', 'a1c', 'a123c', 'a.c', 'a.xy.zc', 'a.zc', 'abbbbc', 'abbbc', 'abbc', 'abc', 'abq', 'axy zc', 'axy', 'axy.zc', 'axyzc', '^abc$'];
nm(fixtures, 'ab?bc', ['abbbc']);
nm(fixtures, 'ab*c', ['abbbbc', 'abbbc', 'abbc', 'abc']);
nm(fixtures, 'ab+bc', ['abbbbc', 'abbbc', 'abbc']);
nm(fixtures, '^abc$', ['^abc$']);
nm(fixtures, 'a.c', ['a.c']);
nm(fixtures, 'a.*c', ['a.c', 'a.xy.zc', 'a.zc']);
nm(fixtures, 'a*c', ['a c', 'a.c', 'a1c', 'a123c', 'abbbbc', 'abbbc', 'abbc', 'abc', 'axyzc', 'axy zc', 'axy.zc', 'a.xy.zc', 'a.zc']);
nm(fixtures, 'a\\w+c', ['a1c', 'a123c', 'abbbbc', 'abbbc', 'abbc', 'abc', 'axyzc'], 'Should match word characters');
nm(fixtures, 'a\\W+c', ['a.c', 'a c'], 'Should match non-word characters');
nm(fixtures, 'a\\d+c', ['a1c', 'a123c'], 'Should match numbers');
nm(['foo@#$%123ASD #$$%^&', 'foo!@#$asdfl;', '123'], '\\d+', ['123']);
nm(['a123c', 'abbbc'], 'a\\D+c', ['abbbc'], 'Should match non-numbers');
nm(['foo', ' foo '], '(f|o)+\\b', ['foo'], 'Should match word boundaries');
});
});
describe('spaces:', function() {
it('should match spaces', function() {
assert(nm.isMatch(' ', ' '));
assert(nm.isMatch(' ', ' '));
assert(!nm.isMatch(' ', ' '));
assert(!nm.isMatch(' ', ' '));
assert(nm.isMatch(' /', ' /'));
assert(nm.isMatch(' / ', ' / '));
assert(nm.isMatch(' /foo', ' /*'));
assert(nm.isMatch(' /foo', ' /*'));
assert(nm.isMatch('foo ', '* '));
assert(nm.isMatch(' foo/foo', ' foo/*'));
assert(nm.isMatch('foo /foo', 'foo /*'));
assert(nm.isMatch('foo /foo ', 'foo /*'));
});
});
describe('$ dollar signs:', function() {
it('should treat dollar signs as literal', function() {
assert(nm.isMatch('$', '$'));
assert(nm.isMatch('$/foo', '$/*'));
assert(nm.isMatch('$/foo', '$/*'));
assert(nm.isMatch('foo$', '*$'));
assert(nm.isMatch('$foo/foo', '$foo/*'));
assert(nm.isMatch('foo$/foo', 'foo$/*'));
assert(nm.isMatch('foo$/foo$', 'foo$/*'));
});
});
describe('^ caret', function() {
it('should treat caret as literal:', function() {
assert(nm.isMatch('^', '^'));
assert(nm.isMatch('^/foo', '^/*'));
assert(nm.isMatch('^/foo', '^/*'));
assert(nm.isMatch('foo^', '*^'));
assert(nm.isMatch('^foo/foo', '^foo/*'));
assert(nm.isMatch('foo^/foo', 'foo^/*'));
});
});
describe('slashes', function() {
it('should match forward slashes', function() {
assert(nm.isMatch('/', '/'));
});
it('should match backslashes', function() {
assert(nm.isMatch('\\', '[\\\\/]'));
assert(nm.isMatch('\\', '[\\\\/]+'));
assert(nm.isMatch('\\\\', '[\\\\/]+'));
assert(nm.isMatch('\\\\\\', '[\\\\/]+'));
if (isWindows()) {
nm(['\\'], '[\\\\/]', ['/']);
nm(['\\', '\\\\', '\\\\\\'], '[\\\\/]+', ['/']);
} else {
nm(['\\'], '[\\\\/]', ['\\']);
nm(['\\', '\\\\', '\\\\\\'], '[\\\\/]+', ['\\', '\\\\', '\\\\\\']);
}
var sep = path.sep;
path.sep = '\\';
assert(nm.isMatch('\\', '[\\\\/]'));
assert(nm.isMatch('\\', '[\\\\/]+'));
assert(nm.isMatch('\\\\', '[\\\\/]+'));
assert(nm.isMatch('\\\\\\', '[\\\\/]+'));
nm(['\\'], '[\\\\/]', ['/']);
nm(['\\', '\\\\', '\\\\\\'], '[\\\\/]+', ['/']);
path.sep = sep;
});
});
describe('colons and drive letters', function() {
it('should treat common URL characters as literals', function() {
assert(nm.isMatch(':', ':'));
assert(nm.isMatch(':/foo', ':/*'));
assert(nm.isMatch('D://foo', 'D://*'));
assert(nm.isMatch('D://foo', 'D:\\/\\/*'));
});
});
describe('[ab] - brackets:', function() {
it('should support regex character classes:', function() {
nm(['a/b.md', 'a/c.md', 'a/d.md', 'a/E.md'], 'a/[A-Z].md', ['a/E.md']);
nm(['a/b.md', 'a/c.md', 'a/d.md'], 'a/[bd].md', ['a/b.md', 'a/d.md']);
nm(['a-1.md', 'a-2.md', 'a-3.md', 'a-4.md', 'a-5.md'], 'a-[2-4].md', ['a-2.md', 'a-3.md', 'a-4.md']);
nm(['a/b.md', 'b/b.md', 'c/b.md', 'b/c.md', 'a/d.md'], '[bc]/[bd].md', ['b/b.md', 'c/b.md']);
});
it('should handle brackets', function() {
nm(['ab', 'ac', 'ad', 'a*', '*'], '[a*]*', ['*', 'a*'], {bash: false});
nm(['ab', 'ac', 'ad', 'a*', '*'], '[a*]*', ['*', 'a*', 'ab', 'ac', 'ad']);
});
it('should handle unclosed brackets', function() {
nm(['[!ab', '[ab'], '[!a*', ['[!ab']);
});
});
describe('(a|b) - logical OR:', function() {
it('should support regex logical OR:', function() {
nm(['a/a', 'a/b', 'a/c', 'b/a', 'b/b'], '(a|b)/b', ['a/b', 'b/b']);
nm(['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'c/b'], '((a|b)|c)/b', ['a/b', 'b/b', 'c/b']);
nm(['a/b.md', 'a/c.md', 'a/d.md'], 'a/(b|d).md', ['a/b.md', 'a/d.md']);
nm(['a-1.md', 'a-2.md', 'a-3.md', 'a-4.md', 'a-5.md'], 'a-(2|3|4).md', ['a-2.md', 'a-3.md', 'a-4.md']);
nm(['a/b.md', 'b/b.md', 'c/b.md', 'b/c.md', 'a/d.md'], '(b|c)/(b|d).md', ['b/b.md', 'c/b.md']);
});
});
});