-
Notifications
You must be signed in to change notification settings - Fork 63
/
braces.compile.js
75 lines (62 loc) · 2.52 KB
/
braces.compile.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
'use strict';
require('mocha');
const assert = require('assert').strict;
const compile = require('../lib/compile');
const parse = require('../lib/parse');
describe('braces.compile()', () => {
describe('errors', () => {
it('should throw an error when invalid args are passed', () => {
assert.throws(() => compile());
});
});
describe('invalid characters', () => {
it('should escape invalid bracket characters', () => {
assert.equal(compile(parse(']{a,b,c}')), '\\](a|b|c)');
});
});
describe('sets', () => {
it('should support empty sets', () => {
assert.equal(compile(parse('{a,}')), '(a|)');
assert.equal(compile(parse('{a,,}')), '(a|)');
assert.equal(compile(parse('{a,,,}')), '(a|)');
assert.equal(compile(parse('{a,,,,}')), '(a|)');
assert.equal(compile(parse('{a,,,,,}')), '(a|)');
});
});
describe('ranges', () => {
it('should escape braces with invalid ranges', () => {
assert.equal(compile(parse('{a...b}')), '{a...b}');
assert.equal(compile(parse('{a...b}'), { escapeInvalid: true }), '\\{a...b\\}');
});
it('should expand brace patterns with both sets and ranges', () => {
assert.equal(compile(parse('{a..e,z}')), '(a..e|z)');
assert.equal(compile(parse('{a..e,a..z}')), '(a..e|a..z)');
});
it('should escape braces with too many range expressions', () => {
assert.equal(compile(parse('{a..e..x..z}')), '{a..e..x..z}');
assert.equal(compile(parse('{a..e..x..z}'), { escapeInvalid: true }), '\\{a..e..x..z\\}');
});
it('should compile very simple numeric ranges', () => {
assert.equal(compile(parse('{1..5}')), '([1-5])');
});
it('should compile numeric ranges with increments', () => {
assert.equal(compile(parse('{1..5..2}')), '(1|3|5)');
});
it('should compile zero-padded numeric ranges', () => {
assert.equal(compile(parse('{01..05}')), '(0?[1-5])');
});
it('should compile zero-padded numeric ranges with increments', () => {
assert.equal(compile(parse('{01..05..2}')), '(1|3|5)');
});
});
describe('invalid', () => {
it('should escape incomplete brace patterns', () => {
assert.equal(compile(parse(']{a/b')), '\\]{a/b');
assert.equal(compile(parse(']{a/b'), { escapeInvalid: true }), '\\]\\{a/b');
});
it('should escape non-brace patterns (no sets or ranges)', () => {
assert.equal(compile(parse(']{a/b}')), '\\]{a/b}');
assert.equal(compile(parse(']{a/b}'), { escapeInvalid: true }), '\\]\\{a/b\\}');
});
});
});