-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
181 lines (131 loc) · 12.1 KB
/
test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import test from "node:test";
import { strict as assert } from "node:assert";
import exported, { prollyfill, prototypeLocaleIndexOf, indexOf } from "./index.js";
const localeIndexOf = exported(Intl);
test('localeIndexOf', async (t) => {
await t.test('basic behaviour', () => {
assert.equal(localeIndexOf('abcä', 'abcä'), 0, 'same strings');
assert.equal(localeIndexOf('äbc', 'äb'), 0, 'substring at the start of string');
assert.equal(localeIndexOf('abäcd', 'bäc'), 1, 'substring in the middle of string');
assert.equal(localeIndexOf('abcä', 'bcä'), 1, 'substring at the end of string');
assert.equal(localeIndexOf('abcä', 'ä'), 3, 'character at the end of string');
assert.equal(localeIndexOf('äbc', 'bd'), -1, 'no match');
});
await t.test('decomposed strings', () => {
assert.equal(localeIndexOf('caf\u00e9', 'caf\u0065\u0301'), 0, 'decomposed substring: same strings');
assert.equal(localeIndexOf('caf\u00e9 x', 'caf\u0065\u0301'), 0, 'decomposed substring: substring at the start of string');
assert.equal(localeIndexOf('xcaf\u00e9x', 'caf\u0065\u0301'), 1, 'decomposed substring: substring in the middle of string');
assert.equal(localeIndexOf('xcaf\u00e9', 'caf\u0065\u0301'), 1, 'decomposed substring: substring at the end of string');
assert.equal(localeIndexOf('cf\u00e9', 'caf\u0065\u0301'), -1, 'decomposed substring: no match');
assert.equal(localeIndexOf('caf\u0065\u0301', 'caf\u0065\u0301'), 0, 'decomposed both: same strings');
assert.equal(localeIndexOf('caf\u0065\u0301 x', 'caf\u0065\u0301'), 0, 'decomposed both: substring at the start of string');
assert.equal(localeIndexOf('xcaf\u0065\u0301x', 'caf\u0065\u0301'), 1, 'decomposed both: substring in the middle of string');
assert.equal(localeIndexOf('xcaf\u0065\u0301', 'caf\u0065\u0301'), 1, 'decomposed both: substring at the end of string');
assert.equal(localeIndexOf('cf\u0065\u0301', 'caf\u0065\u0301'), -1, 'decomposed both: no match');
assert.equal(localeIndexOf('caf\u0065\u0301', 'caf\u00e9'), 0, 'decomposed string: same strings');
assert.equal(localeIndexOf('caf\u0065\u0301 x', 'caf\u00e9'), 0, 'decomposed string: substring at the start of string');
assert.equal(localeIndexOf('xcaf\u0065\u0301x', 'caf\u00e9'), 1, 'decomposed string: substring in the middle of string');
assert.equal(localeIndexOf('xcaf\u0065\u0301', 'caf\u00e9'), 1, 'decomposed string: substring at the end of string');
assert.equal(localeIndexOf('cf\u0065\u0301', 'caf\u00e9'), -1, 'decomposed string: no match');
assert.equal(localeIndexOf('\u0065\u0301\u0065\u0301caf\u0065\u0301', 'caf\u00e9'), 4, 'return string index, not grapheme index');
});
await t.test('sensitivity: base', () => {
const options = { sensitivity: 'base' };
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', { sensitivity: 'variant' }), -1, 'en: no match with sensitivity: variant');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', { sensitivity: 'variant' }), -1, 'de: no match with sensitivity: variant');
assert.equal(localeIndexOf('here is b for you', 'a', 'en', options), -1, 'en: no match with another letter');
assert.equal(localeIndexOf('here is b for you', 'a', 'de', options), -1, 'de: no match with another letter');
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', options), 8, 'en: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', options), -1, 'de: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'en', options), 8, 'en: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'de', options), -1, 'de: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'en', options), 8, 'en: A in the string, a in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'de', options), 8, 'de: A in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'en', options), 8, 'en: a in the string, A in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'de', options), 8, 'de: a in the string, A in the substring');
});
t.test('sensitivity: accent', () => {
const options = { sensitivity: 'accent' };
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', { sensitivity: 'variant' }), -1, 'no match with sensitivity: variant');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', { sensitivity: 'variant' }), -1, 'de: no match with sensitivity: variant');
assert.equal(localeIndexOf('here is b for you', 'a', 'en', options), -1, 'en: no match with another letter');
assert.equal(localeIndexOf('here is b for you', 'a', 'de', options), -1, 'de: no match with another letter');
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', options), -1, 'en: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', options), -1, 'de: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'en', options), -1, 'en: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'de', options), -1, 'de: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'en', options), 8, 'en: A in the string, a in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'de', options), 8, 'de: A in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'en', options), 8, 'en: a in the string, A in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'de', options), 8, 'de: a in the string, A in the substring');
});
await t.test('sensitivity: case', () => {
const options = { sensitivity: 'case' };
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', { sensitivity: 'variant' }), -1, 'no match with sensitivity: variant');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', { sensitivity: 'variant' }), -1, 'no match with sensitivity: variant');
assert.equal(localeIndexOf('here is b for you', 'a', 'en', options), -1, 'en: no match with another letter');
assert.equal(localeIndexOf('here is b for you', 'a', 'de', options), -1, 'de: no match with another letter');
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', options), 8, 'en: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', options), -1, 'de: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'en', options), 8, 'en: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'de', options), -1, 'de: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'en', options), -1, 'en: A in the string, a in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'de', options), -1, 'de: A in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'en', options), -1, 'en: a in the string, A in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'de', options), -1, 'de: a in the string, A in the substring');
});
await t.test('sensitivity: variant', () => {
const options = { sensitivity: 'variant' };
assert.equal(localeIndexOf('here is ä for you', 'ä', 'en', options), 8, 'en: match with same letter');
assert.equal(localeIndexOf('here is ä for you', 'ä', 'de', options), 8, 'de: match with same letter');
assert.equal(localeIndexOf('here is b for you', 'a', 'en', options), -1, 'en: no match with another letter');
assert.equal(localeIndexOf('here is b for you', 'a', 'de', options), -1, 'de: no match with another letter');
assert.equal(localeIndexOf('here is ä for you', 'a', 'en', options), -1, 'en: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is ä for you', 'a', 'de', options), -1, 'de: ä in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'en', options), -1, 'en: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is a for you', 'ä', 'de', options), -1, 'de: a in the string, ä in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'en', options), -1, 'en: A in the string, a in the substring');
assert.equal(localeIndexOf('here is A for you', 'a', 'de', options), -1, 'de: A in the string, a in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'en', options), -1, 'en: a in the string, A in the substring');
assert.equal(localeIndexOf('here is a for you', 'A', 'de', options), -1, 'de: a in the string, A in the substring');
});
await t.test('existing collator', () => {
const collatorEN = new Intl.Collator('en', { sensitivity: 'base', usage: 'search' });
assert.equal(localeIndexOf('here is ä for you', 'a', collatorEN), 8, 'en: ä in the string, a in the substring');
const collatorDE = new Intl.Collator('de', { sensitivity: 'base', usage: 'search' });
assert.equal(localeIndexOf('here is ä for you', 'a', collatorDE), -1, 'de: ä in the string, a in the substring');
});
await t.test('prollyfill mode', () => {
String.prototype.localeIndexOf = prototypeLocaleIndexOf(Intl);
assert.equal('here is ä for you'.localeIndexOf('a', 'en', { sensitivity: 'base' }), 8, 'en: ä in the string, a in the substring');
assert.equal('here is ä for you'.localeIndexOf('a', 'de', { sensitivity: 'base' }), -1, 'de: ä in the string, a in the substring');
const collatorEN = new Intl.Collator('en', { sensitivity: 'base', usage: 'search' });
assert.equal('here is ä for you'.localeIndexOf('a', collatorEN), 8, 'en: ä in the string, a in the substring');
const collatorDE = new Intl.Collator('de', { sensitivity: 'base', usage: 'search' });
assert.equal('here is ä for you'.localeIndexOf('a', collatorDE), -1, 'de: ä in the string, a in the substring');
delete String.prototype.localeIndexOf;
});
await t.test('prollyfill installation', () => {
prollyfill();
assert.equal('here is ä for you'.localeIndexOf('a', 'en', { sensitivity: 'base' }), 8, 'ä in the string, a in the substring');
delete String.prototype.localeIndexOf;
});
await t.test('ignorePunctuation', () => {
const options = { ignorePunctuation: true };
// the caveat is that whitespace is also considered punctuation
assert.equal(localeIndexOf('tes', 'e', 'en', options), 1, 'en: string contains punctuation');
assert.equal(localeIndexOf('tes', 'e', 'de', options), 1, 'de: string contains punctuation');
assert.equal(localeIndexOf('a mätch, (possibly) true', 'mätchpossibly', 'en', options), 1, 'en: string contains punctuation');
assert.equal(localeIndexOf('a mätch, (possibly) true', 'mätchpossibly', 'de', options), 1, 'de: string contains punctuation');
assert.equal(localeIndexOf('a mätchpossibly true', 'mätch possibly!!', 'en', options), 1, 'en: substring contains punctuation');
assert.equal(localeIndexOf('a mätchpossibly true', 'mätch possibly!!', 'de', options), 1, 'de: substring contains punctuation');
assert.equal(localeIndexOf('a mätch, (possibly!) true', 'mätch possibly!!', 'en', options), 1, 'en: string and substring contain punctuation');
assert.equal(indexOf.lastLength, 17, 'en: lastLength');
assert.equal(localeIndexOf('a mätch, (possibly!) true', 'mätch possibly!!', 'de', options), 1, 'de: string and substring contain punctuation');
assert.equal(indexOf.lastLength, 17, 'de: lastLength');
assert.equal(localeIndexOf('\u0065\u0301 mätch, (possibl\u0065\u0301!) true', 'mätch possibl\u00e9!!', 'de', options), 2, 'de: decomposed string and substring contain punctuation');
assert.equal(indexOf.lastLength, 18, 'de: lastLength');
assert.equal(localeIndexOf('\u0065\u0301 mätch, (possibl\u00e9!) true', 'mätch possibl\u0065\u0301!!', 'de', options), 2, 'de: string and decomposed substring contain punctuation');
assert.equal(indexOf.lastLength, 17, 'de: lastLength');
});
});