1 |
|
2 | const assert = require('assert');
|
3 | const GladString = require('../namespace/string');
|
4 | let string = new GladString();
|
5 |
|
6 | let LATIN_CHAR_MAP = {
|
7 | 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'Ae',
|
8 | 'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I',
|
9 | 'Î': 'I', 'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O',
|
10 | 'Õ': 'O', 'Ö': 'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U',
|
11 | 'Ü': 'U', 'Ű': 'U', 'Ý': 'Y', 'Þ': 'Th', 'ß': 'ss', 'à':'a', 'á':'a',
|
12 | 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c', 'è': 'e',
|
13 | 'é': 'e', 'ê': 'e', 'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i',
|
14 | 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o',
|
15 | 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u', 'ű': 'u',
|
16 | 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
|
17 | };
|
18 |
|
19 | let GREEK_CHAR_MAP = {
|
20 | 'α':'a', 'β':'b', 'γ':'g', 'δ':'d', 'ε':'e', 'ζ':'z', 'η':'h', 'θ':'8',
|
21 | 'ι':'i', 'κ':'k', 'λ':'l', 'μ':'m', 'ν':'n', 'ξ':'3', 'ο':'o', 'π':'p',
|
22 | 'ρ':'r', 'σ':'s', 'τ':'t', 'υ':'y', 'φ':'f', 'χ':'x', 'ψ':'ps', 'ω':'w',
|
23 | 'ά':'a', 'έ':'e', 'ί':'i', 'ό':'o', 'ύ':'y', 'ή':'h', 'ώ':'w', 'ς':'s',
|
24 | 'ϊ':'i', 'ΰ':'y', 'ϋ':'y', 'ΐ':'i',
|
25 | 'Α':'A', 'Β':'B', 'Γ':'G', 'Δ':'D', 'Ε':'E', 'Ζ':'Z', 'Η':'H', 'Θ':'8',
|
26 | 'Ι':'I', 'Κ':'K', 'Λ':'L', 'Μ':'M', 'Ν':'N', 'Ξ':'3', 'Ο':'O', 'Π':'P',
|
27 | 'Ρ':'R', 'Σ':'S', 'Τ':'T', 'Υ':'Y', 'Φ':'F', 'Χ':'X', 'Ψ':'PS', 'Ω':'W',
|
28 | 'Ά':'A', 'Έ':'E', 'Ί':'I', 'Ό':'O', 'Ύ':'Y', 'Ή':'H', 'Ώ':'W', 'Ϊ':'I',
|
29 | 'Ϋ':'Y'
|
30 | };
|
31 |
|
32 | let TURKISH_CHAR_MAP = { 'ş':'s', 'Ş':'S', 'ı':'i', 'İ':'I', 'ğ':'g', 'Ğ':'G' };
|
33 |
|
34 | let RUSSIAN_CHAR_MAP = {
|
35 | 'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e', 'ё':'yo', 'ж':'zh',
|
36 | 'з':'z', 'и':'i', 'й':'j', 'к':'k', 'л':'l', 'м':'m', 'н':'n', 'о':'o',
|
37 | 'п':'p', 'р':'r', 'с':'s', 'т':'t', 'у':'u', 'ф':'f', 'х':'h', 'ц':'c',
|
38 | 'ч':'ch', 'ш':'sh', 'щ':'sh', 'ъ':'u', 'ы':'y', 'э':'e', 'ю':'yu',
|
39 | 'я':'ya',
|
40 | 'А':'A', 'Б':'B', 'В':'V', 'Г':'G', 'Д':'D', 'Е':'E', 'Ё':'Yo', 'Ж':'Zh',
|
41 | 'З':'Z', 'И':'I', 'Й':'J', 'К':'K', 'Л':'L', 'М':'M', 'Н':'N', 'О':'O',
|
42 | 'П':'P', 'Р':'R', 'С':'S', 'Т':'T', 'У':'U', 'Ф':'F', 'Х':'H', 'Ц':'C',
|
43 | 'Ч':'Ch', 'Ш':'Sh', 'Щ':'Sh', 'Ъ':'U', 'Ы':'Y', 'Ь':'Ь', 'Э':'E', 'Ю':'Yu',
|
44 | 'Я':'Ya'
|
45 | };
|
46 |
|
47 | describe("String Tests", function () {
|
48 |
|
49 | it("deburr:: should convert unicode", function () {
|
50 | assert.equal(string.deburr('♥'), 'love');
|
51 | });
|
52 |
|
53 | it("deburr:: should convert multi char symbols", function () {
|
54 | assert.equal(string.deburr('<3'), 'love');
|
55 | assert.equal(string.deburr('<3 w/ stuff'), 'love with stuff');
|
56 | assert.equal(string.deburr('cr@zy&&loud'), 'cr@zyandloud');
|
57 | });
|
58 |
|
59 | it("deburr:: should convert latin chars", function () {
|
60 | assert.equal(
|
61 | string.deburr(Object.keys(LATIN_CHAR_MAP).join('')),
|
62 | Object.keys(LATIN_CHAR_MAP).map(char => LATIN_CHAR_MAP[char]).join('')
|
63 | );
|
64 | });
|
65 |
|
66 | it("deburr:: should convert greek chars", function () {
|
67 | assert.equal(
|
68 | string.deburr(Object.keys(GREEK_CHAR_MAP).join('')),
|
69 | Object.keys(GREEK_CHAR_MAP).map(char => GREEK_CHAR_MAP[char]).join('')
|
70 | );
|
71 | });
|
72 |
|
73 | it("deburr:: should convert turkish chars", function () {
|
74 | assert.equal(
|
75 | string.deburr(Object.keys(TURKISH_CHAR_MAP).join('')),
|
76 | Object.keys(TURKISH_CHAR_MAP).map(char => TURKISH_CHAR_MAP[char]).join('')
|
77 | );
|
78 | });
|
79 |
|
80 | it("deburr:: should convert russian chars", function () {
|
81 | assert.equal(
|
82 | string.deburr(Object.keys(RUSSIAN_CHAR_MAP).join('')),
|
83 | Object.keys(RUSSIAN_CHAR_MAP).map(char => RUSSIAN_CHAR_MAP[char]).join('')
|
84 | );
|
85 | });
|
86 |
|
87 | it("slug:: should slugify a normal string", function () {
|
88 | assert.equal(
|
89 | string.slugify("Charlie on Glad JS"),
|
90 | "charlie-on-glad-js"
|
91 | );
|
92 | });
|
93 |
|
94 | it("slug:: should slugify a weird string", function () {
|
95 | assert.equal(
|
96 | string.slugify("Charlie on Glad JS w/ βeta"),
|
97 | "charlie-on-glad-js-with-beta"
|
98 | );
|
99 | });
|
100 |
|
101 | it("slug:: should slugify string with multichar", function () {
|
102 | assert.equal(
|
103 | string.slugify("Charlie on Glad JS :)"),
|
104 | "charlie-on-glad-js-smile"
|
105 | );
|
106 | });
|
107 |
|
108 | it("slug:: should slugify multiple arguments", function () {
|
109 | assert.equal(
|
110 | string.slugify("San Francisco", "CA"),
|
111 | "san-francisco-ca"
|
112 | );
|
113 | });
|
114 |
|
115 | it("slug:: should slugify multiple arguments (2)", function () {
|
116 | assert.equal(
|
117 | string.slugify("San Francisco", "CA", ", foo"),
|
118 | "san-francisco-ca-foo"
|
119 | );
|
120 | });
|
121 |
|
122 | it("camelize:: should Camelize string", function () {
|
123 | assert.equal(
|
124 | string.camelize("fooze-barz"),
|
125 | "foozeBarz"
|
126 | );
|
127 | });
|
128 |
|
129 | it("titleize:: should titleize string", function () {
|
130 | assert.equal(
|
131 | string.titelize("fooze barz"),
|
132 | "Fooze Barz"
|
133 | );
|
134 | });
|
135 |
|
136 | it("slasherize:: should slasherize string", function () {
|
137 | assert.equal(
|
138 | string.slasherize("fooze barz"),
|
139 | "fooze/barz"
|
140 | );
|
141 | });
|
142 |
|
143 | it("reverseSlasherize:: should reverseSlasherize string", function () {
|
144 | assert.equal(
|
145 | string.reverseSlasherize("fooze barz"),
|
146 | "barz/fooze"
|
147 | );
|
148 | });
|
149 |
|
150 | it("underscore:: should underscore string", function () {
|
151 | assert.equal(
|
152 | string.underscore("fooze barz"),
|
153 | "fooze_barz"
|
154 | );
|
155 | });
|
156 |
|
157 | it("cleanSpaces:: should cleanSpaces string", function () {
|
158 | assert.equal(
|
159 | string.cleanSpaces("fooze barz"),
|
160 | "fooze barz"
|
161 | );
|
162 | });
|
163 |
|
164 | it("endsWith:: should return true if a string ends with fooze", function () {
|
165 | assert.equal(
|
166 | string.endsWith("fooze barz fooze", "fooze"),
|
167 | true
|
168 | );
|
169 | });
|
170 |
|
171 | it("endsWith:: should return false if a string does not end with fooze", function () {
|
172 | assert.equal(
|
173 | string.endsWith("fooze barz", "fooze"),
|
174 | false
|
175 | );
|
176 | });
|
177 |
|
178 | it("escape:: should escape a string", function () {
|
179 | assert.equal(
|
180 | string.escape("fred, barney, & pebbles"),
|
181 | "fred, barney, & pebbles"
|
182 | );
|
183 | });
|
184 |
|
185 | it("unescape:: should unescape a string", function () {
|
186 | assert.equal(
|
187 | string.unescape("fred, barney, & pebbles"),
|
188 | "fred, barney, & pebbles"
|
189 | );
|
190 | });
|
191 |
|
192 | it('escapeRegExp:: should escape a string for use in regexp', function () {
|
193 | assert.equal(
|
194 | string.escapeRegExp('[lodash](https://lodash.com/)'),
|
195 | "\\[lodash\\]\\(https://lodash\\.com/\\)"
|
196 | );
|
197 | });
|
198 |
|
199 | it("repeat:: should repeat a string 2 times", function () {
|
200 | assert.equal(
|
201 | string.repeat("glad", 2),
|
202 | "gladglad"
|
203 | );
|
204 | });
|
205 |
|
206 | it("repeat:: should repeat a string 2 times with spacing", function () {
|
207 | assert.equal(
|
208 | string.repeat("glad ", 2),
|
209 | "glad glad "
|
210 | );
|
211 | });
|
212 |
|
213 | it("startsWith:: should return true if a string starts with fooze", function () {
|
214 | assert.equal(
|
215 | string.startsWith("fooze barz", "fooze"),
|
216 | true
|
217 | );
|
218 | });
|
219 |
|
220 | it("startsWith:: should return false if a string does not start with fooze", function () {
|
221 | assert.equal(
|
222 | string.startsWith("x fooze barz", "fooze"),
|
223 | false
|
224 | );
|
225 | });
|
226 |
|
227 | it("words:: should split a string based on words", function () {
|
228 | assert.deepEqual(
|
229 | string.words('fred, barney, & pebbles'),
|
230 | ['fred', 'barney', 'pebbles']
|
231 | );
|
232 | });
|
233 |
|
234 | it("words:: should split a string based on words and keep elements by regexp", function () {
|
235 | assert.deepEqual(
|
236 | string.words('fred, barney, & pebbles', /[^, ]+/g),
|
237 | ['fred', 'barney', '&', 'pebbles']
|
238 | );
|
239 | });
|
240 |
|
241 | it ("sentenceCase:: should capitalize Beginnings of sentences", function () {
|
242 | assert.equal(
|
243 | string.sentenceCase("the quick. brown fox. jumped over stuff."),
|
244 | "The quick. Brown fox. Jumped over stuff."
|
245 | );
|
246 | });
|
247 |
|
248 | });
|