1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const slug = require('../slug');
|
9 |
|
10 | describe('slug', function() {
|
11 | it('should convert input to string', function() {
|
12 | [slug(1)].should.eql(['1']);
|
13 | return [slug(567890)].should.eql(['567890']);
|
14 | });
|
15 |
|
16 | it('should replace whitespaces with replacement', function() {
|
17 | [slug('foo bar baz')].should.eql(['foo-bar-baz']);
|
18 | [slug('foo bar baz', '_')].should.eql(['foo_bar_baz']);
|
19 | return [slug('foo bar baz', '')].should.eql(['foobarbaz']);
|
20 | });
|
21 |
|
22 | it('should remove trailing space if any', () => [slug(' foo bar baz ')].should.eql(['foo-bar-baz']));
|
23 |
|
24 | it('should remove not allowed chars', function() {
|
25 | [slug('foo, bar baz')].should.eql(['foo-bar-baz']);
|
26 | [slug('foo- bar baz')].should.eql(['foo-bar-baz']);
|
27 | return [slug('foo] bar baz')].should.eql(['foo-bar-baz']);
|
28 | });
|
29 |
|
30 | it('should leave allowed chars in rfc3986 mode', function() {
|
31 | const allowed = ['.', '_', '~'];
|
32 | return Array.from(allowed).map((a) =>
|
33 | [slug(`foo ${a} bar baz`,
|
34 | {mode: "rfc3986"})].should.eql([`foo-${a}-bar-baz`]));
|
35 | });
|
36 |
|
37 | it('should leave allowed chars in pretty mode', function() {
|
38 | const allowed = ['_', '~'];
|
39 | return Array.from(allowed).map((a) =>
|
40 | [slug(`foo ${a} bar baz`)].should.eql([`foo-${a}-bar-baz`]));
|
41 | });
|
42 |
|
43 | it('should replace latin chars', function() {
|
44 | const char_map = {
|
45 | 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE',
|
46 | 'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I',
|
47 | 'Î': 'I', 'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O',
|
48 | 'Õ': 'O', 'Ö': 'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U',
|
49 | 'Ü': 'U', 'Ű': 'U', 'Ý': 'Y', 'Þ': 'TH', 'ß': 'ss', 'à':'a', 'á':'a',
|
50 | 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c', 'è': 'e',
|
51 | 'é': 'e', 'ê': 'e', 'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i',
|
52 | 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o',
|
53 | 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u', 'ű': 'u',
|
54 | 'ý': 'y', 'þ': 'th', 'ÿ': 'y', 'ẞ': 'SS'
|
55 | };
|
56 | return (() => {
|
57 | const result = [];
|
58 | for (let char in char_map) {
|
59 | const replacement = char_map[char];
|
60 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
61 | }
|
62 | return result;
|
63 | })();
|
64 | });
|
65 |
|
66 | it('should replace greek chars', function() {
|
67 | const char_map = {
|
68 | 'α':'a', 'β':'b', 'γ':'g', 'δ':'d', 'ε':'e', 'ζ':'z', 'η':'h', 'θ':'8',
|
69 | 'ι':'i', 'κ':'k', 'λ':'l', 'μ':'m', 'ν':'n', 'ξ':'3', 'ο':'o', 'π':'p',
|
70 | 'ρ':'r', 'σ':'s', 'τ':'t', 'υ':'y', 'φ':'f', 'χ':'x', 'ψ':'ps', 'ω':'w',
|
71 | 'ά':'a', 'έ':'e', 'ί':'i', 'ό':'o', 'ύ':'y', 'ή':'h', 'ώ':'w', 'ς':'s',
|
72 | 'ϊ':'i', 'ΰ':'y', 'ϋ':'y', 'ΐ':'i',
|
73 | 'Α':'A', 'Β':'B', 'Γ':'G', 'Δ':'D', 'Ε':'E', 'Ζ':'Z', 'Η':'H', 'Θ':'8',
|
74 | 'Ι':'I', 'Κ':'K', 'Λ':'L', 'Μ':'M', 'Ν':'N', 'Ξ':'3', 'Ο':'O', 'Π':'P',
|
75 | 'Ρ':'R', 'Σ':'S', 'Τ':'T', 'Υ':'Y', 'Φ':'F', 'Χ':'X', 'Ψ':'PS', 'Ω':'W',
|
76 | 'Ά':'A', 'Έ':'E', 'Ί':'I', 'Ό':'O', 'Ύ':'Y', 'Ή':'H', 'Ώ':'W', 'Ϊ':'I',
|
77 | 'Ϋ':'Y'
|
78 | };
|
79 | return (() => {
|
80 | const result = [];
|
81 | for (let char in char_map) {
|
82 | const replacement = char_map[char];
|
83 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
84 | }
|
85 | return result;
|
86 | })();
|
87 | });
|
88 |
|
89 | it('should replace turkish chars', function() {
|
90 | const char_map = {
|
91 | 'ş':'s', 'Ş':'S', 'ı':'i', 'İ':'I', 'ç':'c', 'Ç':'C', 'ü':'u', 'Ü':'U',
|
92 | 'ö':'o', 'Ö':'O', 'ğ':'g', 'Ğ':'G'
|
93 | };
|
94 | return (() => {
|
95 | const result = [];
|
96 | for (let char in char_map) {
|
97 | const replacement = char_map[char];
|
98 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
99 | }
|
100 | return result;
|
101 | })();
|
102 | });
|
103 |
|
104 | it('should replace cyrillic chars', function() {
|
105 | const char_map = {
|
106 | 'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e', 'ё':'yo', 'ж':'zh',
|
107 | 'з':'z', 'и':'i', 'й':'j', 'к':'k', 'л':'l', 'м':'m', 'н':'n', 'о':'o',
|
108 | 'п':'p', 'р':'r', 'с':'s', 'т':'t', 'у':'u', 'ф':'f', 'х':'h', 'ц':'c',
|
109 | 'ч':'ch', 'ш':'sh', 'щ':'sh', 'ъ':'u', 'ы':'y', 'ь':'', 'э':'e', 'ю':'yu',
|
110 | 'я':'ya',
|
111 | 'А':'A', 'Б':'B', 'В':'V', 'Г':'G', 'Д':'D', 'Е':'E', 'Ё':'Yo', 'Ж':'Zh',
|
112 | 'З':'Z', 'И':'I', 'Й':'J', 'К':'K', 'Л':'L', 'М':'M', 'Н':'N', 'О':'O',
|
113 | 'П':'P', 'Р':'R', 'С':'S', 'Т':'T', 'У':'U', 'Ф':'F', 'Х':'H', 'Ц':'C',
|
114 | 'Ч':'Ch', 'Ш':'Sh', 'Щ':'Sh', 'Ъ':'U', 'Ы':'Y', 'Ь':'', 'Э':'E', 'Ю':'Yu',
|
115 | 'Я':'Ya', 'Є':'Ye', 'І':'I', 'Ї':'Yi', 'Ґ':'G', 'є':'ye', 'і':'i', 'ї':'yi', 'ґ':'g'
|
116 | };
|
117 | return (() => {
|
118 | const result = [];
|
119 | for (let char in char_map) {
|
120 | const replacement = char_map[char];
|
121 | let expected = `foo-${replacement}-bar-baz`;
|
122 | if (!replacement) { expected = "foo-bar-baz"; }
|
123 | result.push([slug(`foo ${char} bar baz`)].should.eql([expected]));
|
124 | }
|
125 | return result;
|
126 | })();
|
127 | });
|
128 |
|
129 | it('should replace czech chars', function() {
|
130 | const char_map = {
|
131 | 'č':'c', 'ď':'d', 'ě':'e', 'ň': 'n', 'ř':'r', 'š':'s', 'ť':'t', 'ů':'u',
|
132 | 'ž':'z', 'Č':'C', 'Ď':'D', 'Ě':'E', 'Ň': 'N', 'Ř':'R', 'Š':'S', 'Ť':'T',
|
133 | 'Ů':'U', 'Ž':'Z'
|
134 | };
|
135 | return (() => {
|
136 | const result = [];
|
137 | for (let char in char_map) {
|
138 | const replacement = char_map[char];
|
139 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
140 | }
|
141 | return result;
|
142 | })();
|
143 | });
|
144 |
|
145 | it('should replace polish chars', function() {
|
146 | const char_map = {
|
147 | 'ą':'a', 'ć':'c', 'ę':'e', 'ł':'l', 'ń':'n', 'ó':'o', 'ś':'s', 'ź':'z',
|
148 | 'ż':'z', 'Ą':'A', 'Ć':'C', 'Ę':'E', 'Ł':'L', 'Ń':'N', 'Ś':'S',
|
149 | 'Ź':'Z', 'Ż':'Z'
|
150 | };
|
151 | return (() => {
|
152 | const result = [];
|
153 | for (let char in char_map) {
|
154 | const replacement = char_map[char];
|
155 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
156 | }
|
157 | return result;
|
158 | })();
|
159 | });
|
160 |
|
161 | it('should replace latvian chars', function() {
|
162 | const char_map = {
|
163 | 'ā':'a', 'č':'c', 'ē':'e', 'ģ':'g', 'ī':'i', 'ķ':'k', 'ļ':'l', 'ņ':'n',
|
164 | 'š':'s', 'ū':'u', 'ž':'z', 'Ā':'A', 'Č':'C', 'Ē':'E', 'Ģ':'G', 'Ī':'I',
|
165 | 'Ķ':'K', 'Ļ':'L', 'Ņ':'N', 'Š':'S', 'Ū':'U', 'Ž':'Z'
|
166 | };
|
167 | return (() => {
|
168 | const result = [];
|
169 | for (let char in char_map) {
|
170 | const replacement = char_map[char];
|
171 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
172 | }
|
173 | return result;
|
174 | })();
|
175 | });
|
176 |
|
177 | it('should replace vietnamese chars', function() {
|
178 | const char_map = {
|
179 | 'Ạ': 'A', 'Ả': 'A', 'Ầ': 'A', 'Ấ': 'A', 'Ậ': 'A', 'Ẩ': 'A', 'Ẫ': 'A',
|
180 | 'Ằ': 'A', 'Ắ': 'A', 'Ặ': 'A', 'Ẳ': 'A', 'Ẵ': 'A', 'Ẹ': 'E', 'Ẻ': 'E',
|
181 | 'Ẽ': 'E', 'Ề': 'E', 'Ế': 'E', 'Ệ': 'E', 'Ể': 'E', 'Ễ': 'E', 'Ị': 'I',
|
182 | 'Ỉ': 'I', 'Ĩ': 'I', 'Ọ': 'O', 'Ỏ': 'O', 'Ồ': 'O', 'Ố': 'O', 'Ộ': 'O',
|
183 | 'Ổ': 'O', 'Ỗ': 'O', 'Ơ': 'O', 'Ờ': 'O', 'Ớ': 'O', 'Ợ': 'O', 'Ở': 'O',
|
184 | 'Ỡ': 'O', 'Ụ': 'U', 'Ủ': 'U', 'Ũ': 'U', 'Ư': 'U', 'Ừ': 'U', 'Ứ': 'U',
|
185 | 'Ự': 'U', 'Ử': 'U', 'Ữ': 'U', 'Ỳ': 'Y', 'Ỵ': 'Y', 'Ỷ': 'Y', 'Ỹ': 'Y',
|
186 | 'Đ': 'D', 'ạ': 'a', 'ả': 'a', 'ầ': 'a', 'ấ': 'a', 'ậ': 'a', 'ẩ': 'a',
|
187 | 'ẫ': 'a', 'ằ': 'a', 'ắ': 'a', 'ặ': 'a', 'ẳ': 'a', 'ẵ': 'a', 'ẹ': 'e',
|
188 | 'ẻ': 'e', 'ẽ': 'e', 'ề': 'e', 'ế': 'e', 'ệ': 'e', 'ể': 'e', 'ễ': 'e',
|
189 | 'ị': 'i', 'ỉ': 'i', 'ĩ': 'i', 'ọ': 'o', 'ỏ': 'o', 'ồ': 'o', 'ố': 'o',
|
190 | 'ộ': 'o', 'ổ': 'o', 'ỗ': 'o', 'ơ': 'o', 'ờ': 'o', 'ớ': 'o', 'ợ': 'o',
|
191 | 'ở': 'o', 'ỡ': 'o', 'ụ': 'u', 'ủ': 'u', 'ũ': 'u', 'ư': 'u', 'ừ': 'u',
|
192 | 'ứ': 'u', 'ự': 'u', 'ử': 'u', 'ữ': 'u', 'ỳ': 'y', 'ỵ': 'y', 'ỷ': 'y',
|
193 | 'ỹ': 'y', 'đ': 'd'
|
194 | };
|
195 | return (() => {
|
196 | const result = [];
|
197 | for (let char in char_map) {
|
198 | const replacement = char_map[char];
|
199 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
200 | }
|
201 | return result;
|
202 | })();
|
203 | });
|
204 |
|
205 | it('should replace currencies', function() {
|
206 | const char_map = {
|
207 | '€': 'euro', '₢': 'cruzeiro', '₣': 'french franc', '£': 'pound',
|
208 | '₤': 'lira', '₥': 'mill', '₦': 'naira', '₧': 'peseta', '₨': 'rupee', '₹': 'indian rupee',
|
209 | '₩': 'won', '₪': 'new shequel', '₫': 'dong', '₭': 'kip', '₮': 'tugrik',
|
210 | '₯': 'drachma', '₰': 'penny', '₱': 'peso', '₲': 'guarani', '₳': 'austral',
|
211 | '₴': 'hryvnia', '₵': 'cedi', '¢': 'cent', '¥': 'yen', '元': 'yuan',
|
212 | '円': 'yen', '﷼': 'rial', '₠': 'ecu', '¤': 'currency', '฿': 'baht',
|
213 | "$": 'dollar'
|
214 | };
|
215 | return (() => {
|
216 | const result = [];
|
217 | for (let char in char_map) {
|
218 | let replacement = char_map[char];
|
219 | replacement = replacement.replace(' ', '-');
|
220 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
221 | }
|
222 | return result;
|
223 | })();
|
224 | });
|
225 |
|
226 | it('should replace symbols in rfc3986 mode', function() {
|
227 | const char_map = {
|
228 | '©':'c', 'œ': 'oe', 'Œ': 'OE', '∑': 'sum', '®': 'r',
|
229 | '∂': 'd', 'ƒ': 'f', '™': 'tm',
|
230 | '℠': 'sm', '…': '...', '˚': 'o', 'º': 'o', 'ª': 'a',
|
231 | '∆': 'delta', '∞': 'infinity', '♥': 'love', '&': 'and', '|': 'or',
|
232 | '<': 'less', '>': 'greater'
|
233 | };
|
234 | return (() => {
|
235 | const result = [];
|
236 | for (let char in char_map) {
|
237 | const replacement = char_map[char];
|
238 | result.push([slug(`foo ${char} bar baz`,
|
239 | {mode: "rfc3986"})].should.eql([
|
240 | `foo-${replacement}-bar-baz`.toLowerCase()]));
|
241 | }
|
242 | return result;
|
243 | })();
|
244 | });
|
245 |
|
246 | it('should replace symbols in pretty mode', function() {
|
247 | const char_map = {
|
248 | '©':'c', 'œ': 'oe', 'Œ': 'OE', '∑': 'sum', '®': 'r',
|
249 | '∂': 'd', 'ƒ': 'f', '™': 'tm',
|
250 | '℠': 'sm', '˚': 'o', 'º': 'o', 'ª': 'a',
|
251 | '∆': 'delta', '∞': 'infinity', '♥': 'love', '&': 'and', '|': 'or',
|
252 | '<': 'less', '>': 'greater'
|
253 | };
|
254 | return (() => {
|
255 | const result = [];
|
256 | for (let char in char_map) {
|
257 | const replacement = char_map[char];
|
258 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
259 | }
|
260 | return result;
|
261 | })();
|
262 | });
|
263 |
|
264 | it('should remove ellipsis in pretty mode', function() {
|
265 | const char_map = {
|
266 | '…': '...'
|
267 | };
|
268 | return (() => {
|
269 | const result = [];
|
270 | for (let char in char_map) {
|
271 | result.push([slug(`foo ${char} bar baz`)].should.eql(["foo-bar-baz"]));
|
272 | }
|
273 | return result;
|
274 | })();
|
275 | });
|
276 |
|
277 | it('should strip … symbols in pretty mode', () => [slug("foo … bar baz")].should.eql(["foo-bar-baz"]));
|
278 |
|
279 | it('should strip symbols', function() {
|
280 | const char_map = [
|
281 | '†', '“', '”', '‘', '’', '•'
|
282 | ];
|
283 | return Array.from(char_map).map((char) =>
|
284 | [slug(`foo ${char} bar baz`)].should.eql(["foo-bar-baz"]));
|
285 | });
|
286 |
|
287 | it('should replace unicode', function() {
|
288 | const char_map = {
|
289 | '☢':"radioactive",'☠':"skull-and-bones",'☤':"caduceus",
|
290 | '☣':"biohazard",'☭':"hammer-and-sickle", '☯':"yin-yang",'☮':"peace",
|
291 | '☏':"telephone",'☔':"umbrella-with-rain-drops",'☎':"telephone",
|
292 | '☀':"sun-with-rays",'★':"star",'☂':"umbrella",'☃':"snowman",
|
293 | '✈':"airplane",'✉':"envelope",'✊':"raised-fist"
|
294 | };
|
295 | return (() => {
|
296 | const result = [];
|
297 | for (let char in char_map) {
|
298 | const replacement = char_map[char];
|
299 | result.push([slug(`foo ${char} bar baz`)].should.eql([`foo-${replacement}-bar-baz`]));
|
300 | }
|
301 | return result;
|
302 | })();
|
303 | });
|
304 |
|
305 | it('should replace no unicode when disabled', function() {
|
306 | const char_map = '😹☢☠☤☣☭☯☮☏☔☎☀★☂☃✈✉✊'.split('');
|
307 | return Array.from(char_map).map((char) =>
|
308 | [slug(`foo ${char} bar baz`, {symbols:false})].should.eql(["foo-bar-baz"]));
|
309 | });
|
310 |
|
311 | it('should allow altering the charmap', function() {
|
312 | const charmap = {
|
313 | 'f': 'ph', 'o':'0', 'b':'8', 'a':'4', 'r':'2', 'z':'5'
|
314 | };
|
315 | return [slug("foo bar baz", {charmap}).toUpperCase()].should.eql(['PH00-842-845']);
|
316 | });
|
317 |
|
318 | it('should replace lithuanian characters', () => slug('ąčęėįšųūžĄČĘĖĮŠŲŪŽ').should.eql('aceeisuuzACEEISUUZ'));
|
319 |
|
320 | it('should replace multichars', () => [slug("w/ <3 && sugar || ☠")].should.eql(['with-love-and-sugar-or-skull-and-bones']));
|
321 |
|
322 | it('should be flavourable', function() {
|
323 | const text = "It's your journey ... we guide you through.";
|
324 | const expected = "Its-your-journey-we-guide-you-through";
|
325 | return [slug(text, {mode:'pretty'})].should.eql([expected]);
|
326 | });
|
327 |
|
328 | it('should default to lowercase in rfc3986 mode', function() {
|
329 | const text = "It's Your Journey We Guide You Through.";
|
330 | const expected = "its-your-journey-we-guide-you-through.";
|
331 | return [slug(text, {mode:'rfc3986'})].should.eql([expected]);
|
332 | });
|
333 |
|
334 | it('should allow disabling of lowercase', function() {
|
335 | const text = "It's Your Journey We Guide You Through.";
|
336 | const expected = "Its-Your-Journey-We-Guide-You-Through.";
|
337 | return [slug(text, {mode:'rfc3986', lower:false})].should.eql([expected]);
|
338 | });
|
339 |
|
340 | it('should replace arabic characters', () => slug('مرحبا بك').should.eql('mrhba-bk'));
|
341 |
|
342 | it('should replace zh characters', () => slug('鳄梨').should.eql('6bOE5qKo'));
|
343 | });
|