-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.test.mjs
100 lines (66 loc) · 2.26 KB
/
index.test.mjs
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
import { expect } from 'chai';
import lib from './index.js';
describe('diacritic-regex', function () {
describe('toRegex', function () {
it('changes string to regex group', function () {
expect(lib.toRegex()('abe').toString()).to.equal('/[' + lib.mappings.a + ']b[' + lib.mappings.e + ']/i')
expect(lib.toRegex()('àbé').toString()).to.equal('/[' + lib.mappings.a + ']b[' + lib.mappings.e + ']/i')
})
it('returns unchanged regex when no diacritic chars', function () {
expect(lib.toRegex()('pqrst').toString()).to.equal('/pqrst/i')
})
it('changes flag', function () {
expect(lib.toRegex({ flags: 'm' })('pqrst').toString()).to.equal('/pqrst/m')
})
it('allows new mappings', function () {
var tr = lib.toRegex({
mappings: {
'~': '/~`',
't': 'tT'
}
})
expect(tr('~lt').toString()).to.equal('/[/~`]l[tT]/i')
expect(tr('~lt').test('omg `lT')).to.equal(true)
})
})
describe('toString', function () {
it('returns unchanged string when no diacritics', function () {
expect(lib.toString()('pqrst')).to.equal('pqrst')
})
it('returns changed string when no diacritics', function () {
expect(lib.toString()('pqrst')).to.equal('pqrst')
})
it('allows new mappings', function () {
expect(lib.toString({
mappings: {
'~': '/~`',
't': 'tT'
}
})('~lt')).to.equal('[/~`]l[tT]')
expect(lib.toString({
mappings: {
'e': 'e'
}
})('e')).to.equal('e')
var ts = lib.toString({
mappings: {
'*': ['\\S+'],
'e': ['e']
}
});
expect(ts('e*e')).to.equal('e\\S+e')
expect(new RegExp(ts('jogue * bola')).test('jogue poké bola')).to.equal(true)
ts = lib.toString({
mappings: {
'*': ['\\w+', '\\d+']
}
});
expect(ts('e*e')).to.equal('[' + lib.mappings.e + ']\\w+\\d+[' + lib.mappings.e + ']')
expect(new RegExp(ts('jogue * bola')).test('jogue zzzz0 bola')).to.equal(true)
lib.mappings['&'] = 'a'
ts = lib.toString();
expect(new RegExp(ts('&')).test('a')).to.equal(true)
expect(new RegExp(ts('&')).test('b')).to.equal(false)
})
})
})