stringweaver 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tests/UnitTests.js +921 -0
package/package.json
CHANGED
|
@@ -0,0 +1,921 @@
|
|
|
1
|
+
/* node:coverage disable */
|
|
2
|
+
/* For StringWeaver tests we are not interested in coverage for this file */
|
|
3
|
+
import assert from 'node:assert';
|
|
4
|
+
import {describe, it} from 'node:test';
|
|
5
|
+
import {default as $S, CustomStringConstructor} from "../index.js";
|
|
6
|
+
import {quotingStyles} from "../src/genericMethods.js";
|
|
7
|
+
|
|
8
|
+
describe(`Basics constructor`, () => {
|
|
9
|
+
describe(`Instantiation`, () => {
|
|
10
|
+
it(`constructor can be used as regular function`, () => {
|
|
11
|
+
const hi = $S(`Hello world`);
|
|
12
|
+
assert.strictEqual(String(hi), `Hello world`);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it(`constructor can be used as tagged template function`, () => {
|
|
16
|
+
const wrld = `world`;
|
|
17
|
+
const hi = $S`Hello ${wrld}`;
|
|
18
|
+
assert.strictEqual(String(hi), `Hello world`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it(`constructor can be used from symbolic String extension`, () => {
|
|
22
|
+
const wrld = `world`;
|
|
23
|
+
const hi = `Hello ${wrld}`[Symbol.toSB];
|
|
24
|
+
assert.strictEqual(String(hi), `Hello world`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it(`$S creates an instance with chainable methods`, () => {
|
|
28
|
+
const hi = $S`Hello world`;
|
|
29
|
+
assert.strictEqual(String(hi), `Hello world`);
|
|
30
|
+
hi.prefix(`=> `);
|
|
31
|
+
assert.strictEqual(String(hi), `=> Hello world`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it(`An instance (without custom getters/methods) has no keys`, () => {
|
|
35
|
+
const hi = $S`Hello world`;
|
|
36
|
+
assert.strictEqual(Object.keys(hi).length, 0);
|
|
37
|
+
assert.strictEqual(JSON.stringify(hi), `{}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe(`Always returns a string`, () => {
|
|
41
|
+
it(`parameter null => empty string`, () => {
|
|
42
|
+
assert.strictEqual($S(null).value, "");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it(`no parameter => empty string`, () => {
|
|
46
|
+
assert.strictEqual($S().value, "");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it(`parameter Object => empty string`, () => {
|
|
50
|
+
assert.strictEqual($S({a:1, b: 2}).value, "");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it(`parameter RegExp => empty string`, () => {
|
|
54
|
+
assert.strictEqual($S(/[a-z]/g).value, "");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it(`parameter Map instance => empty string`, () => {
|
|
58
|
+
assert.strictEqual($S(new Map()).value, "");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it(`parameter Array instance => empty string`, () => {
|
|
62
|
+
assert.strictEqual($S([1,2,3]).value, "");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it(`parameter new String => empty string`, () => {
|
|
66
|
+
assert.strictEqual($S(new String()).value, "");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe(`Instantiation by Symbolic String extensions`, () => {
|
|
71
|
+
it(`creates an instance from a plain string`, () => {
|
|
72
|
+
assert.strictEqual("Hello world"[Symbol.toSB].constructor, CustomStringConstructor);
|
|
73
|
+
assert.strictEqual("Hello world"[Symbol.toSB].value, `Hello world`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it(`creates an instance from a template string`, () => {
|
|
77
|
+
const wrld = `world`;
|
|
78
|
+
assert.strictEqual(`Hello ${wrld}`[Symbol.toSB].constructor, CustomStringConstructor);
|
|
79
|
+
assert.strictEqual(`Hello ${wrld}`[Symbol.toSB].wordsUCFirst.value, `Hello World`);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it(`creates an instance from an empty string`, () => {
|
|
83
|
+
assert.strictEqual(``[Symbol.toSB].constructor, CustomStringConstructor);
|
|
84
|
+
assert.strictEqual(``[Symbol.toSB].append(`!!!`).value, `!!!`);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe(`Constructor static methods`, () => {
|
|
90
|
+
it(`$S.keys delivers all keys`, () => {
|
|
91
|
+
const keysShouldbe = [
|
|
92
|
+
'append',
|
|
93
|
+
'camelCase',
|
|
94
|
+
'clone',
|
|
95
|
+
'constructor',
|
|
96
|
+
'empty',
|
|
97
|
+
'enclose',
|
|
98
|
+
'firstUp',
|
|
99
|
+
'format',
|
|
100
|
+
'history',
|
|
101
|
+
'indexOf',
|
|
102
|
+
'insert',
|
|
103
|
+
'interpolate',
|
|
104
|
+
'kebabCase',
|
|
105
|
+
'lastIndexOf',
|
|
106
|
+
'notEmpty',
|
|
107
|
+
'prefix',
|
|
108
|
+
'quote',
|
|
109
|
+
'replaceWords',
|
|
110
|
+
'snakeCase',
|
|
111
|
+
'toString',
|
|
112
|
+
'trimAll',
|
|
113
|
+
'trimAllKeepLF',
|
|
114
|
+
'truncate',
|
|
115
|
+
'undo',
|
|
116
|
+
'undoAll',
|
|
117
|
+
'undoLast',
|
|
118
|
+
'value',
|
|
119
|
+
'valueOf',
|
|
120
|
+
'wordsUCFirst'
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
assert.deepStrictEqual($S.keys, keysShouldbe);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it(`$S.info delivers all keys with additional information`, () => {
|
|
127
|
+
const keysShouldbe = [
|
|
128
|
+
'append (chainable method)',
|
|
129
|
+
'camelCase (chainable getter)',
|
|
130
|
+
'clone (chainable getter)',
|
|
131
|
+
'constructor (getter (override))',
|
|
132
|
+
'empty (getter)',
|
|
133
|
+
'enclose (chainable method)',
|
|
134
|
+
'firstUp (chainable getter)',
|
|
135
|
+
'format (chainable method)',
|
|
136
|
+
'history (getter)',
|
|
137
|
+
'indexOf (method (override))',
|
|
138
|
+
'insert (chainable method)',
|
|
139
|
+
'interpolate (chainable method)',
|
|
140
|
+
'kebabCase (chainable getter)',
|
|
141
|
+
'lastIndexOf (method (override))',
|
|
142
|
+
'notEmpty (chainable getter|undefined)',
|
|
143
|
+
'prefix (chainable method)',
|
|
144
|
+
'quote (Object. See [constructor].quoteInfo)',
|
|
145
|
+
'replaceWords (chainable method)',
|
|
146
|
+
'snakeCase (chainable getter)',
|
|
147
|
+
'toString (method (override))',
|
|
148
|
+
'trimAll (chainable getter)',
|
|
149
|
+
'trimAllKeepLF (chainable getter)',
|
|
150
|
+
'truncate (chainable method)',
|
|
151
|
+
'undo (chainable getter)',
|
|
152
|
+
'undoAll (chainable getter)',
|
|
153
|
+
'undoLast (chainable method)',
|
|
154
|
+
'value (getter/setter)',
|
|
155
|
+
'valueOf (method (override))',
|
|
156
|
+
'wordsUCFirst (chainable getter)'
|
|
157
|
+
];
|
|
158
|
+
const info = $S.info;
|
|
159
|
+
// the first lines may vary and is not relevant for the test
|
|
160
|
+
assert.deepStrictEqual(info.slice(info.findIndex(v => v.startsWith(`append`))), keysShouldbe);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it(`$S.keys.quoteInfo as expected`, () => {
|
|
164
|
+
const shouldBe = [
|
|
165
|
+
'[instance].quote.backtick (` [instance value] `)',
|
|
166
|
+
'[instance].quote.bracket ({ [instance value] })',
|
|
167
|
+
'[instance].quote.curlyDouble (“ [instance value] ”)',
|
|
168
|
+
'[instance].quote.curlyDoubleEqual (“ [instance value] “)',
|
|
169
|
+
'[instance].quote.curlyDoubleInward (” [instance value] “)',
|
|
170
|
+
'[instance].quote.curlyLHDouble („ [instance value] ”)',
|
|
171
|
+
'[instance].quote.curlyLHDoubleInward („ [instance value] “)',
|
|
172
|
+
'[instance].quote.curlyLHSingle (‚ [instance value] ’)',
|
|
173
|
+
'[instance].quote.curlyLHSingleInward (‚ [instance value] ‘)',
|
|
174
|
+
'[instance].quote.curlySingle (‛ [instance value] ’)',
|
|
175
|
+
'[instance].quote.curlySingleEqual (‛ [instance value] ‛)',
|
|
176
|
+
'[instance].quote.curlySingleInward (’ [instance value] ‛)',
|
|
177
|
+
'[instance].quote.double (" [instance value] ")',
|
|
178
|
+
'[instance].quote.guillemets (« [instance value] »)',
|
|
179
|
+
'[instance].quote.guillemetsInward (» [instance value] «)',
|
|
180
|
+
'[instance].quote.guillemetsSingle (‹ [instance value] ›)',
|
|
181
|
+
'[instance].quote.guillemetsSingleInward (› [instance value] ‹)',
|
|
182
|
+
"[instance].quote.single (' [instance value] ')",
|
|
183
|
+
'[instance].quote.squareBrackets ([ [instance value] ])'
|
|
184
|
+
];
|
|
185
|
+
assert.deepStrictEqual($S.quoteInfo, shouldBe);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it(`$S.create getter delivers an instance with an empty string value`, () => {
|
|
189
|
+
const hi = $S.create;
|
|
190
|
+
assert.strictEqual(hi.value, ``);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it(`$S.create getter delivers an instance`, () => {
|
|
194
|
+
const hi = $S.create.append(`Hello!`);
|
|
195
|
+
assert.strictEqual(hi.value, `Hello!`);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it(`$S.addCustom getter enumerable works as expected`, () => {
|
|
199
|
+
$S.addCustom({name: `upperQuoted`, method: me => {return me.toUpperCase().quote.curlyDouble; }, enumerable: true, isGetter: true});
|
|
200
|
+
assert.strictEqual(Object.getOwnPropertyDescriptor($S`hi`, 'upperQuoted').enumerable, true);
|
|
201
|
+
assert.strictEqual($S`Hello world`.upperQuoted.value, `“HELLO WORLD”`);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it(`$S.addCustom getter non enumerable works as expected`, () => {
|
|
205
|
+
$S.addCustom({name: `lowerQuotSingle`, method: me => {return me.toLowerCase().quote.curlySingle; }, enumerable: false, isGetter: true});
|
|
206
|
+
assert.strictEqual(Object.getOwnPropertyDescriptor($S`hi`, 'lowerQuotSingle').enumerable, false);
|
|
207
|
+
assert.strictEqual($S`HELLO WORLD`.lowerQuotSingle.value, `‛hello world’`);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it(`$S.addCustom method works as expected`, () => {
|
|
211
|
+
const ok = $S.addCustom({name: `surroundWithInvertedArrows`, method: (me, add) => { return me.prefix(`->`).append(add); }, enumerable: false});
|
|
212
|
+
assert.strictEqual($S`HELLO WORLD`.surroundWithInvertedArrows(`<-`).value, `->HELLO WORLD<-`);
|
|
213
|
+
assert.strictEqual(ok, `addCustom: the method named "surroundWithInvertedArrows" is added`);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it(`$S.addCustom already existing custom as expected`, () => {
|
|
217
|
+
assert.strictEqual(
|
|
218
|
+
$S.addCustom({name: `upperQuoted`, method: me => {return me.toUpperCase().quote.curlyDouble; }, enumerable: true, isGetter: true}),
|
|
219
|
+
`addCustom: the property "upperQuoted" exists and can not be redefined`);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it(`non enumerable custom getter NOT in instance keys`, () => {
|
|
223
|
+
assert.strictEqual(Object.keys($S``).find(v => v === `lowerQuotSingle`), undefined );
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it(`custom enumerable getter in instance keys`, () => {
|
|
227
|
+
assert.strictEqual(Object.keys($S``).find(v => v === `upperQuoted`), `upperQuoted` );
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
it(`$S.format works as expected`, () => {
|
|
231
|
+
const mymy = $S.format(`hello {wrld}`, {wrld: `there - `}, {wrld: `world`});
|
|
232
|
+
assert.strictEqual(mymy.value, `hello there - hello world`);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it(`$S.regExp (tagged template) as expected`, () => {
|
|
236
|
+
const re = $S.regExp`
|
|
237
|
+
[a-z] // only lowercase a-z
|
|
238
|
+
| [0-3] // or numbers 0-3
|
|
239
|
+
${["g", "i"]} // globally`;
|
|
240
|
+
assert.strictEqual(re.constructor, RegExp);
|
|
241
|
+
assert.deepStrictEqual(re, /[a-z]|[0-3]/gi);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it(`$S.regExp (function call) as expected`, () => {
|
|
245
|
+
const re = $S.regExp(`
|
|
246
|
+
[a-z] // only lowercase a-z
|
|
247
|
+
| [0-3] // or numbers 0-3`,
|
|
248
|
+
`g`,
|
|
249
|
+
`im`);
|
|
250
|
+
assert.strictEqual(re.constructor, RegExp);
|
|
251
|
+
assert.deepStrictEqual(re, /[a-z]|[0-3]/gim);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it(`$S.regExp invalid returns string with error message`, () => {
|
|
255
|
+
const re = $S.regExp(`
|
|
256
|
+
([a-z] // only lowercase a-z (a non terminated group)`,
|
|
257
|
+
`g`,
|
|
258
|
+
`im`);
|
|
259
|
+
assert.strictEqual(/Error creating/.test(re), true);
|
|
260
|
+
assert.strictEqual(/error message/.test(re), true);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it(`$S.uuid4 as expected`, () => {
|
|
264
|
+
const uuid = $S.uuid4;
|
|
265
|
+
const splitted = uuid.split(/-/);
|
|
266
|
+
assert.strictEqual(splitted.length, 5);
|
|
267
|
+
assert.strictEqual(splitted[2].startsWith(`4`), true);
|
|
268
|
+
assert.strictEqual(splitted[0].length, 8);
|
|
269
|
+
assert.strictEqual(splitted[1].length, 4);
|
|
270
|
+
assert.strictEqual(splitted[2].length, 4);
|
|
271
|
+
assert.strictEqual(splitted[3].length, 4);
|
|
272
|
+
assert.strictEqual(splitted[4].length, 12);
|
|
273
|
+
const contentOk = splitted.map(v => /[A-E0-9]/gi.test(v)).filter(v => v);
|
|
274
|
+
assert.strictEqual(contentOk.length, 5);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it(`$S.randomString as expected`, () => {
|
|
278
|
+
let rs = $S.randomString();
|
|
279
|
+
assert.strictEqual(rs.length, 12, rs + `default len 12` );
|
|
280
|
+
assert.strictEqual(/[a-z]/gi.test(rs), true, rs + `default a-zA-Z`);
|
|
281
|
+
assert.strictEqual(/[0-9]/g.test(rs), false, rs + `default no numbers`);
|
|
282
|
+
assert.strictEqual(/[!?@#$%^&*=+_;]/g.test(rs), false, rs + `default no symbols`);
|
|
283
|
+
rs = $S.randomString({includeNumbers: true});
|
|
284
|
+
assert.strictEqual(/[a-z0-9]/gi.test(rs), true, rs + `numbers`);
|
|
285
|
+
rs = $S.randomString({startAlphabetic: true, includeNumbers: true});
|
|
286
|
+
assert.strictEqual(/^[a-z]/i.test(rs), true, rs + `numbers + startalphabetic`);
|
|
287
|
+
rs = $S.randomString({includeUppercase: false});
|
|
288
|
+
assert.strictEqual(/[A-Z]/g.test(rs), false, rs + `no uppercase`);
|
|
289
|
+
rs = $S.randomString({len: 24});
|
|
290
|
+
assert.strictEqual(rs.length, 24, rs + `length 24`);
|
|
291
|
+
rs = $S.randomString({includeNumbers: true, includeSymbols: true});
|
|
292
|
+
assert.strictEqual(/[!?@#$%^&*=+_;-a-z0-9]/gi.test(rs), true, rs + `number+symbols`);
|
|
293
|
+
rs = $S.randomString({includeSymbols: true});
|
|
294
|
+
assert.strictEqual(!/[0-9]/gi.test(rs), true, rs + `not number but symbols`);
|
|
295
|
+
assert.strictEqual(/[!?@#$%^&*=+_;-a-z]/g.test(rs), true, rs + `symbols yes`);
|
|
296
|
+
rs = $S.randomString({includeSymbols: true, startAlphabetic: true});
|
|
297
|
+
assert.strictEqual(/^[a-z]/i.test(rs), true, rs + `symbols, not numbers, startalphabetic`);
|
|
298
|
+
rs = $S.randomString({includeUppercase: false, includeSymbols: true, startAlphabetic: true});
|
|
299
|
+
assert.strictEqual(/^[a-z]/.test(rs), true, rs + `lowercase only, symbols, startalphabetic`);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe(`Instance methods, setters & getters (alphabetically ordered)`, () => {
|
|
305
|
+
it(`append (single string)`, () => {
|
|
306
|
+
const hi = $S`Hello world`.append(` and hello!`);
|
|
307
|
+
assert.strictEqual(hi.value, `Hello world and hello!`);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it(`append (multiple strings)`, () => {
|
|
311
|
+
const hi = $S`Hello world`.append(` and hello`, ` to you too!`);
|
|
312
|
+
assert.strictEqual(hi.value, `Hello world and hello to you too!`);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it(`append (no input) does nothing`, () => {
|
|
316
|
+
const hi = $S`Hello world`.append();
|
|
317
|
+
assert.strictEqual(hi.value, `Hello world`);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// todo
|
|
321
|
+
describe(`case`, () => {
|
|
322
|
+
it(`camelCase`, () => {
|
|
323
|
+
const hello = $S`Hello World`.camelCase;
|
|
324
|
+
assert.strictEqual(hello.value, `helloWorld`);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it(`camelCase from dashed`, () => {
|
|
328
|
+
const hello = $S`hello-world`.camelCase;
|
|
329
|
+
assert.strictEqual(hello.value, `helloWorld`);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it(`camelCase /w multiple spaces`, () => {
|
|
333
|
+
const hello = $S` hello world `.camelCase;
|
|
334
|
+
assert.strictEqual(hello.value, `helloWorld`);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it(`camelCase /w multiple dashes`, () => {
|
|
338
|
+
const hello = $S`-hello---world `.camelCase;
|
|
339
|
+
assert.strictEqual(hello.value, `helloWorld`);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it(`kebabCase regular`, () => {
|
|
343
|
+
const hello = $S`helloWorld`.kebabCase;
|
|
344
|
+
assert.strictEqual(hello.value, `hello-world`);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it(`kebabCase /w numbers`, () => {
|
|
348
|
+
const hello = $S`hello42World`.kebabCase;
|
|
349
|
+
assert.strictEqual(hello.value, `hello-world`);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it(`kebabCase /w spaces`, () => {
|
|
353
|
+
const hello = $S`hello World`.kebabCase;
|
|
354
|
+
assert.strictEqual(hello.value, `hello-world`);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it(`kebabCase /w diacriticals`, () => {
|
|
358
|
+
const hello = $S`hello Woërld`.kebabCase;
|
|
359
|
+
assert.strictEqual(hello.value, `hello-world`);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it(`kebabCase /w non alphabetic`, () => {
|
|
363
|
+
const hello = $S`hello $#! World!`.kebabCase;
|
|
364
|
+
assert.strictEqual(hello.value, `hello-world`);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it(`snakeCase regular`, () => {
|
|
368
|
+
const hello = $S`helloWorld`.snakeCase;
|
|
369
|
+
assert.strictEqual(hello.value, `hello_world`);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it(`snakeCase /w numbers`, () => {
|
|
373
|
+
const hello = $S`hello42World`.snakeCase;
|
|
374
|
+
assert.strictEqual(hello.value, `hello_world`);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it(`snakeCase /w spaces`, () => {
|
|
378
|
+
const hello = $S`hello World`.snakeCase;
|
|
379
|
+
assert.strictEqual(hello.value, `hello_world`);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it(`snakeCase /w diacriticals`, () => {
|
|
383
|
+
const hello = $S`hello Woërld`.snakeCase;
|
|
384
|
+
assert.strictEqual(hello.value, `hello_world`);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it(`snakeCase /w non alphabetic`, () => {
|
|
388
|
+
const hello = $S`hello $#! World!`.snakeCase;
|
|
389
|
+
assert.strictEqual(hello.value, `hello_world`);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it(`firstUp`, () => {
|
|
393
|
+
const hello = $S`hello world`.firstUp;
|
|
394
|
+
assert.strictEqual(hello.value, `Hello world`);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it(`wordsUCFirst`, () => {
|
|
398
|
+
const hello = $S`hello world What a day 't is`.wordsUCFirst;
|
|
399
|
+
assert.strictEqual(hello.value, `Hello World What A Day 't Is`);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it(`clone as expected`, () => {
|
|
404
|
+
const x = $S`Hi`;
|
|
405
|
+
const y = x.clone;
|
|
406
|
+
assert.strictEqual(x.value, y.value);
|
|
407
|
+
x.prefix(`>>`);
|
|
408
|
+
assert.notStrictEqual(x.value, y.value);
|
|
409
|
+
assert.strictEqual(x.value, `>>Hi`);
|
|
410
|
+
assert.strictEqual(y.value, `Hi`);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it(`[instance].constructor is CustomStringConstructor`, () => {
|
|
414
|
+
assert.deepStrictEqual($S``.constructor, CustomStringConstructor);
|
|
415
|
+
assert.strictEqual($S``.constructor.name, `CustomStringConstructor`);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
describe(`enclose`, () => {
|
|
419
|
+
it(`enclose without values does nothing`, () => {
|
|
420
|
+
assert.strictEqual($S`Hello world`.enclose().value, `Hello world`);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it(`enclose with start value only as expected`, () => {
|
|
424
|
+
assert.strictEqual($S`Hello world`.enclose(`**`).value, `**Hello world**`);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it(`enclose with start and end value only as expected`, () => {
|
|
428
|
+
assert.strictEqual($S`Hello world`.enclose(`**`, "<").value, `**Hello world<`);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
describe(`.empty as expected`, () => {
|
|
432
|
+
it(`parameter null`, () => {
|
|
433
|
+
assert.strictEqual($S(null).empty, true);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it(`no parameter`, () => {
|
|
437
|
+
assert.strictEqual($S().empty, true);
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it(`parameter string 1`, () => {
|
|
441
|
+
assert.strictEqual($S`{a:1, b: 2}`.empty, false);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it(`parameter string 2`, () => {
|
|
445
|
+
assert.strictEqual($S("hello world").empty, false);
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it(`enclose with start value === instance as expected`, () => {
|
|
450
|
+
assert.strictEqual($S`Hello world`.enclose($S`**`).value, `**Hello world**`);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it(`enclose with start/end value === instance as expected`, () => {
|
|
454
|
+
assert.strictEqual($S`Hello world`.enclose($S`**`, $S`<`).value, `**Hello world<`);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it(`enclose with start value === instance, end value === string as expected`, () => {
|
|
458
|
+
assert.strictEqual($S`Hello world`.enclose($S`**`, `<`).value, `**Hello world<`);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
it(`enclose with start value === string, end value === instance as expected`, () => {
|
|
462
|
+
assert.strictEqual($S`Hello world`.enclose(`**`, $S`<`).value, `**Hello world<`);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it(`enclose with start value !== string, end value === instance does nothing`, () => {
|
|
466
|
+
assert.strictEqual($S`Hello world`.enclose([1, 2, 4], $S`<`).value, `Hello world`);
|
|
467
|
+
});
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
it(`format as expected`, () => {
|
|
471
|
+
// alias: interpolate
|
|
472
|
+
const hi = $S`- hello dear {wrld}`.format({wrld: `you `}, {wrld: `world`});
|
|
473
|
+
assert.strictEqual(hi.value, `- hello dear you - hello dear world`);
|
|
474
|
+
assert.strictEqual($S``.format({hi: `there`}).value, ``, `empty input string`);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it(`history as expected`, () => {
|
|
478
|
+
// see also: undo[All/Last]
|
|
479
|
+
const hi = $S`- hello dear {wrld}`.format({wrld: `you `}, {wrld: `world`});
|
|
480
|
+
assert.deepStrictEqual(hi.history, ['- hello dear {wrld}', '- hello dear you - hello dear world']);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
describe(`insert`, () => {
|
|
484
|
+
it(`insert (values array) as expected`, () => {
|
|
485
|
+
const hi = $S`oh dear world`.insert({values: [`Hello `]});
|
|
486
|
+
assert.strictEqual(hi.value, `Hello oh dear world`);
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it(`insert (single value) as expected`, () => {
|
|
490
|
+
const hi = $S`oh dear world`.insert({value: `Hello `});
|
|
491
|
+
assert.strictEqual(hi.value, `Hello oh dear world`);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it(`insert with empty input string as expected`, () => {
|
|
495
|
+
assert.strictEqual($S``.insert({values: [`hello`, ` `, `world`]}).value, `hello world`);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it(`insert with single string as values as expected`, () => {
|
|
499
|
+
assert.strictEqual($S`world`.insert({values: `hello `}).value, `hello world`);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it(`insert at only as expected`, () => {
|
|
503
|
+
const hi = $S`oh dear world`.insert({at: 5});
|
|
504
|
+
assert.strictEqual(hi.value, `oh dear world`);
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
it(`insert impossible at-value as expected`, () => {
|
|
508
|
+
const hi = $S`oh dear world`.insert({value: `Hello`, at: 25});
|
|
509
|
+
assert.strictEqual(hi.value, `oh dear worldHello`);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it(`insert negative at-value as expected`, () => {
|
|
513
|
+
const hi = $S`oh dear world`.insert({values: [` `, `Hello`], at: -6});
|
|
514
|
+
assert.strictEqual(hi.value, `oh dear Hello world`);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it(`insert no parameters as expected`, () => {
|
|
518
|
+
const hi = $S`oh dear world`.insert();
|
|
519
|
+
assert.strictEqual(hi.value, `oh dear world`);
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
describe(`interpolate`, () => {
|
|
524
|
+
it(`interpolate empty inputstring as expected`, () => {
|
|
525
|
+
const hi = $S``.interpolate({dr: `dear`});
|
|
526
|
+
assert.strictEqual(hi.value, ``);
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
it(`interpolate single template as expected`, () => {
|
|
530
|
+
const hi = $S`oh {dr} world`.interpolate({dr: `dear`});
|
|
531
|
+
assert.strictEqual(hi.value, `oh dear world`);
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it(`interpolate multiple templates as expected`, () => {
|
|
535
|
+
const hi = $S`- hello dear {wrld}`.interpolate({wrld: `you `}, {wrld: `world`});
|
|
536
|
+
assert.strictEqual(hi.value, `- hello dear you - hello dear world`);
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it(`interpolate no arguments as expected`, () => {
|
|
540
|
+
const hi = $S`- hello dear {wrld}`.interpolate();
|
|
541
|
+
assert.strictEqual(hi.value, `- hello dear {wrld}`);
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
it(`indexof (override) as expected`, () => {
|
|
546
|
+
const hi = $S`hello world`;
|
|
547
|
+
assert.strictEqual(hi.indexOf(`o`), 4);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it(`indexof (override, nothing found => undefined) as expected`, () => {
|
|
551
|
+
const hi = $S`hello world`;
|
|
552
|
+
assert.strictEqual(hi.indexOf(`z`), undefined);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it(`lastIndexof (override) as expected`, () => {
|
|
556
|
+
const hi = $S`hello world`;
|
|
557
|
+
assert.strictEqual(hi.lastIndexOf(`o`), 7);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it(`lastIndexof (override, nothing found => undefined) as expected`, () => {
|
|
561
|
+
const hi = $S`hello world`;
|
|
562
|
+
assert.strictEqual(hi.lastIndexOf(`z`), undefined);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
describe(`.notEmpty as expected`, () => {
|
|
566
|
+
it(`instance empty string value as expected`, () => {
|
|
567
|
+
assert.strictEqual(($S()
|
|
568
|
+
.notEmpty?.append(`hello!`) ?? $S()).value, "");
|
|
569
|
+
});
|
|
570
|
+
it(`instance string value as expected`, () => {
|
|
571
|
+
assert.strictEqual(($S("hithere and ")
|
|
572
|
+
.notEmpty?.append(`hello!`) ?? $S()).value, "hithere and hello!");
|
|
573
|
+
});
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
describe(`prefix/prepend`, () => {
|
|
577
|
+
it(`prefix no arguments as expected`, () => {
|
|
578
|
+
const hi = $S`Hi`.prefix();
|
|
579
|
+
assert.strictEqual(hi.value, `Hi`);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it(`prefix single as expected`, () => {
|
|
583
|
+
const hi = $S`Hi`.prefix(`>>`);
|
|
584
|
+
assert.strictEqual(hi.value, `>>Hi`);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it(`prefix multiple as expected`, () => {
|
|
588
|
+
const hi = $S`Hi`.prefix(`01`, ` >> `);
|
|
589
|
+
assert.strictEqual(hi.value, `01 >> Hi`);
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it(`prefix empty input string as expected`, () => {
|
|
593
|
+
const hi = $S``.prefix(`HI`, ` `, `THERE`);
|
|
594
|
+
assert.strictEqual(hi.value, `HI THERE`);
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
it(`prefix (one of) input not a string as expected`, () => {
|
|
598
|
+
const hi = $S`HI`.prefix(`there`, [42]);
|
|
599
|
+
assert.strictEqual(hi.value, `HI`);
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
describe(`replaceWords`, () => {
|
|
604
|
+
it(`no parameters as expected`, () => {
|
|
605
|
+
const noParams = $S`hello world!`.replaceWords();
|
|
606
|
+
assert.strictEqual(noParams.value, `hello world!`);
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it(`invalid replacement (null) as expected`, () => {
|
|
610
|
+
const noParams = $S`hello world!`.replaceWords({replacements: null});
|
|
611
|
+
assert.strictEqual(noParams.value, `hello world!`);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it(`invalid replacement (undefined) as expected`, () => {
|
|
615
|
+
const noParams = $S`hello world!`.replaceWords({replacements: undefined});
|
|
616
|
+
assert.strictEqual(noParams.value, `hello world!`);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
it(`invalid replacement (new Set()) as expected`, () => {
|
|
620
|
+
const noParams = $S`hello world!`.replaceWords({replacements: new Set()});
|
|
621
|
+
assert.strictEqual(noParams.value, `hello world!`);
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
it(`invalid replacement (RegExp) as expected`, () => {
|
|
625
|
+
const noParams = $S`hello world!`.replaceWords({replacements: /[a-z]/i});
|
|
626
|
+
assert.strictEqual(noParams.value, `hello world!`);
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it (`case sensitive multiple replacements`, () => {
|
|
630
|
+
const ccs = $S`hello world!`.replaceWords({replacements: {hello: `hi`, World: `universe`}, caseSensitive: true});
|
|
631
|
+
assert.strictEqual(ccs.value, `hi world!`);
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
it(`case insensitive multiple replacements`, () => {
|
|
635
|
+
const cci = $S`hello world!`.replaceWords({replacements: {hello: `hi`, World: `universe`}});
|
|
636
|
+
assert.strictEqual(cci.value, `hi universe!`);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it(`single replacement as expected`, () => {
|
|
640
|
+
const cci = $S`hello world!`.replaceWords({ replacements: {hello: `hi`} });
|
|
641
|
+
assert.strictEqual(cci.value, `hi world!`);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it(`single replacement case sensitive as expected`, () => {
|
|
645
|
+
const ccs = $S`hello world!`.replaceWords({replacements: {HELLO: `hi`}, caseSensitive: true});
|
|
646
|
+
assert.strictEqual(ccs.value, `hello world!`);
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it(`invalid nr of replacement parameters as expected`, () => {
|
|
650
|
+
const ccs = $S`hello world!`.replaceWords({replacements: {HELLO: `hi`}});
|
|
651
|
+
assert.strictEqual(ccs.value, `hi world!`);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it(`keys with regexp reserved characters work as expected`,() => {
|
|
655
|
+
const ccs = $S("replace /.+me\\").replaceWords({
|
|
656
|
+
replacements: {replace: "Hello", "/.+me\\": $S`world`}});
|
|
657
|
+
assert.strictEqual(ccs.value, `Hello world`);
|
|
658
|
+
})
|
|
659
|
+
|
|
660
|
+
it(`empty input string as expected`, () => {
|
|
661
|
+
const ccs = $S``.replaceWords({replacements: {}});
|
|
662
|
+
assert.strictEqual(ccs.value, ``);
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
describe(`quoting`, () => {
|
|
667
|
+
it(`[instance].quote[quotingStyle] for all possibilities as expected`, () => {
|
|
668
|
+
const testme = Object.keys($S.create.quote).map((key) => {
|
|
669
|
+
if (/^remove$|^custom$/.test(key)) { return false; }
|
|
670
|
+
return $S`${key}: ` + $S(`quoting`).quote[key];
|
|
671
|
+
}).filter(k => k);
|
|
672
|
+
|
|
673
|
+
assert.deepStrictEqual(testme, [
|
|
674
|
+
"backtick: `quoting`",
|
|
675
|
+
"bracket: {quoting}",
|
|
676
|
+
"curlyDouble: “quoting”",
|
|
677
|
+
"curlyDoubleInward: ”quoting“",
|
|
678
|
+
"curlyDoubleEqual: “quoting“",
|
|
679
|
+
"curlyLHDoubleInward: „quoting“",
|
|
680
|
+
"curlyLHSingle: ‚quoting’",
|
|
681
|
+
"curlyLHSingleInward: ‚quoting‘",
|
|
682
|
+
"curlySingle: ‛quoting’",
|
|
683
|
+
"curlySingleEqual: ‛quoting‛",
|
|
684
|
+
"curlySingleInward: ’quoting‛",
|
|
685
|
+
"double: \"quoting\"",
|
|
686
|
+
"guillemets: «quoting»",
|
|
687
|
+
"guillemetsInward: »quoting«",
|
|
688
|
+
"guillemetsSingle: ‹quoting›",
|
|
689
|
+
"guillemetsSingleInward: ›quoting‹",
|
|
690
|
+
"single: 'quoting'",
|
|
691
|
+
"squareBrackets: [quoting]"
|
|
692
|
+
]);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
it(`quotingStyles.re is what we expect`, () => {
|
|
696
|
+
assert.deepStrictEqual(
|
|
697
|
+
quotingStyles.re,
|
|
698
|
+
/^[\`\{\}\”\“\„\‚\’\‘\‛\"\«\»\‹\›\'\[\]]|[\`\{\}\”\“\„\‚\’\‘\‛\"\«\»\‹\›\'\[\]]$/g);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it(`[instance.quote.remove for all possibilities as expected]`, () => {
|
|
702
|
+
const testme = Object.keys($S.create.quote).map((key) => {
|
|
703
|
+
if (/^remove|^custom$/.test(key)) { return {[key]: false}; }
|
|
704
|
+
return {[key]: $S(`quoting`).quote[key]};
|
|
705
|
+
})
|
|
706
|
+
.filter(v => Object.values(v)[0])
|
|
707
|
+
.map(v => {
|
|
708
|
+
const [key, value] = Object.entries(v)[0];
|
|
709
|
+
return $S`${key}: ` + value.quote.remove
|
|
710
|
+
});
|
|
711
|
+
assert.deepStrictEqual(testme, [
|
|
712
|
+
"backtick: quoting",
|
|
713
|
+
"bracket: quoting",
|
|
714
|
+
"curlyDouble: quoting",
|
|
715
|
+
"curlyDoubleInward: quoting",
|
|
716
|
+
"curlyDoubleEqual: quoting",
|
|
717
|
+
"curlyLHDoubleInward: quoting",
|
|
718
|
+
"curlyLHSingle: quoting",
|
|
719
|
+
"curlyLHSingleInward: quoting",
|
|
720
|
+
"curlySingle: quoting",
|
|
721
|
+
"curlySingleEqual: quoting",
|
|
722
|
+
"curlySingleInward: quoting",
|
|
723
|
+
"double: quoting",
|
|
724
|
+
"guillemets: quoting",
|
|
725
|
+
"guillemetsInward: quoting",
|
|
726
|
+
"guillemetsSingle: quoting",
|
|
727
|
+
"guillemetsSingleInward: quoting",
|
|
728
|
+
"single: quoting",
|
|
729
|
+
"squareBrackets: quoting"
|
|
730
|
+
]);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
it(`[instance.quote.custom as expected]`, () => {
|
|
734
|
+
const hi = $S`quoting`.quote.custom(`!!`, `!!`);
|
|
735
|
+
assert.strictEqual(hi.value, `!!quoting!!`);
|
|
736
|
+
});
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it(`toString as expected`, () => {
|
|
740
|
+
const hi = $S`stringified`;
|
|
741
|
+
assert.strictEqual(hi.constructor, CustomStringConstructor);
|
|
742
|
+
assert.notStrictEqual(hi.toString().constructor, CustomStringConstructor);
|
|
743
|
+
assert.strictEqual(hi.toString(), `stringified`);
|
|
744
|
+
assert.strictEqual(String(hi), `stringified`);
|
|
745
|
+
assert(`${hi}`, `stringified`);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
describe(`trimAll`, () => {
|
|
749
|
+
it(`trimAll removes all excessive whitespace`, () => {
|
|
750
|
+
// excessive whitespace: all consecutive whitespaces (so 2 or more tabs, spaces, line feeds etc)
|
|
751
|
+
const tooMuch = $S`stringified with too\nmuch\n\nwhitespace characters`;
|
|
752
|
+
assert.strictEqual(tooMuch.trimAll.value, "stringified with too\nmuch\nwhitespace characters", String(tooMuch));
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it(`trimAllKeepLF removes all excessive whitespace but keeps single line feeds if applicable`, () => {
|
|
756
|
+
const tooMuch = $S`stringified with too\nmuch\n\nwhitespace characters`;
|
|
757
|
+
assert.strictEqual(tooMuch.trimAllKeepLF.value, "stringified with too\nmuch\n\nwhitespace characters");
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
it(`trimAll empty input string as expected`, () => {
|
|
761
|
+
assert.strictEqual($S``.trimAll.value, ``);
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
it(`trimAllKeppLF empty input string as expected`, () => {
|
|
765
|
+
assert.strictEqual($S``.trimAllKeepLF.value, ``);
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
describe(`truncate`, () => {
|
|
770
|
+
it(`truncate (not html entity, no wordBoundary) as expected`, () => {
|
|
771
|
+
const hi = $S`Hello universe`;
|
|
772
|
+
assert.strictEqual(hi.truncate({at: 8}).value, `Hello u...`);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it(`truncate (not html entity, wordBoundary true) as expected`, () => {
|
|
776
|
+
const hi = $S`Hello, (universe) say no more`;
|
|
777
|
+
assert.strictEqual(hi.truncate({at: 8, wordBoundary: true}).value, `Hello,...`);
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it(`truncate (html entity) as expected`, () => {
|
|
781
|
+
const hi = $S`Hello universe say no more`;
|
|
782
|
+
assert.strictEqual(hi.truncate({at: 8, html: true}).value, `Hello u…`);
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
it(`truncate (html entity, wordBoundary true) as expected`, () => {
|
|
786
|
+
const hi = $S`Hello universe: say no more`;
|
|
787
|
+
assert.strictEqual(hi.truncate({at: 18, html: true, wordBoundary: true}).value, `Hello universe:…`);
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it(`truncate at >= input length as expected`, () => {
|
|
791
|
+
const hi = $S`Hello universe say no more`;
|
|
792
|
+
assert.strictEqual(hi.truncate({at: 26}).value, `Hello universe say no more`);
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
it(`truncate empty input string as expected`, () => {
|
|
796
|
+
const hi = $S``;
|
|
797
|
+
assert.strictEqual(hi.truncate({at: 16, html: true, wordBoundary: true}).value, ``);
|
|
798
|
+
});
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
describe(`undo`, () => {
|
|
802
|
+
it(`undo as expected`, () => {
|
|
803
|
+
const hi = $S`Hello`;
|
|
804
|
+
hi.append(` World`, ` how are you?`);
|
|
805
|
+
assert.strictEqual(hi.value, `Hello World how are you?`);
|
|
806
|
+
assert.strictEqual(hi.undo.value, `Hello`);
|
|
807
|
+
hi.prefix(`>>`).prefix(`<<`);
|
|
808
|
+
assert.strictEqual(hi.undo.value, `>>Hello`);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
it(`undoAll as expected`, () => {
|
|
812
|
+
const hi = $S`Hello`;
|
|
813
|
+
hi.append(` World`, ` how are you?`).prefix(`>>`).prefix(`<<`);
|
|
814
|
+
assert.strictEqual(hi.value, `<<>>Hello World how are you?`);
|
|
815
|
+
assert.strictEqual(hi.undoAll.value, `Hello`);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
it(`undoLast as expected`, () => {
|
|
819
|
+
const hi = $S`Hello`;
|
|
820
|
+
hi.append(` World`, ` how are you?`).prefix(`>>`).prefix(`<<`);
|
|
821
|
+
assert.strictEqual(hi.value, `<<>>Hello World how are you?`);
|
|
822
|
+
assert.strictEqual(hi.undoLast(2).value, `Hello World how are you?`);
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it(`undo with [instance].history.length == 1 as expected`, () => {
|
|
826
|
+
const hi = $S`Hello`;
|
|
827
|
+
assert.strictEqual(hi.value, `Hello`);
|
|
828
|
+
assert.strictEqual(hi.history.length, 1);
|
|
829
|
+
assert.strictEqual(hi.clone.undo.value, `Hello`);
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
it(`undoLast with non numeric parameter as expected`, () => {
|
|
833
|
+
const hi = $S`Hello`;
|
|
834
|
+
hi.append(` World`, ` how are you?`).prefix(`>>`).prefix(`<<`);
|
|
835
|
+
assert.strictEqual(hi.value, `<<>>Hello World how are you?`);
|
|
836
|
+
assert.strictEqual(hi.undoLast('NO').value, `<<>>Hello World how are you?`);
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
it(`undoLast with n > history.length as expected (equals undoAll)`, () => {
|
|
840
|
+
const hi = $S`Hello`;
|
|
841
|
+
hi.append(` World`, ` how are you?`).prefix(`>>`).prefix(`<<`);
|
|
842
|
+
assert.strictEqual(hi.value, `<<>>Hello World how are you?`);
|
|
843
|
+
assert.strictEqual(hi.clone.undoLast(15).value, `Hello`);
|
|
844
|
+
assert.strictEqual(hi.clone.undoLast(15).value, hi.clone.undoAll.value);
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
describe(`value setter`, () => {
|
|
849
|
+
it(`value set as expected`, () => {
|
|
850
|
+
const hi = $S`Hello`;
|
|
851
|
+
hi.value = `hello world`;
|
|
852
|
+
assert.strictEqual(hi.value, `hello world`);
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
it(`value += as expected`, () => {
|
|
856
|
+
const hi = $S`Hello`;
|
|
857
|
+
hi.value += ` world`;
|
|
858
|
+
assert.strictEqual(hi.value, `Hello world`);
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
it(`value set with no string type as expected`, () => {
|
|
862
|
+
const hi = $S`hello`;
|
|
863
|
+
hi.value = {hello: 1};
|
|
864
|
+
assert.strictEqual(hi.value, `hello`);
|
|
865
|
+
// note: += will *always* stringify the value to add
|
|
866
|
+
hi.value += 42;
|
|
867
|
+
assert.strictEqual(hi.value, `hello42`);
|
|
868
|
+
hi.value += {hello: 1};
|
|
869
|
+
assert.strictEqual(hi.value, `hello42[object Object]`);
|
|
870
|
+
});
|
|
871
|
+
|
|
872
|
+
it(`value set with empty string type as expected`, () => {
|
|
873
|
+
const hi = $S`hello`;
|
|
874
|
+
hi.value = ``;
|
|
875
|
+
assert.strictEqual(hi.value, `hello`);
|
|
876
|
+
});
|
|
877
|
+
});
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
describe(`Native String methods are wrapped and usable`, () => {
|
|
881
|
+
it(`includes as expected`, () => {
|
|
882
|
+
const hi = $S`hello world`;
|
|
883
|
+
assert.strictEqual(hi.includes(`world`), true);
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it(`codePointAt as expected`, () => {
|
|
887
|
+
const hi = $S`😎hello world`;
|
|
888
|
+
assert.strictEqual(hi.codePointAt(0), 128526);
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
it(`length as expected`, () => {
|
|
892
|
+
const hi = $S`hello world`;
|
|
893
|
+
assert.strictEqual(hi.length, 11);
|
|
894
|
+
});
|
|
895
|
+
|
|
896
|
+
it(`slice as expected`, () => {
|
|
897
|
+
const hi = $S`hello world`;
|
|
898
|
+
assert.strictEqual(hi.slice(0, 5).value, `hello`);
|
|
899
|
+
assert.strictEqual(hi.undo.slice(-5).value, `world`);
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it(`slice chainable`, () => {
|
|
903
|
+
const hi = $S`hello world`;
|
|
904
|
+
assert.strictEqual(hi.slice(0, 5).append(`WORLD`).value, `helloWORLD`);
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
it(`slice w/indexOf as expected`, () => {
|
|
908
|
+
const hi = $S`hello world`;
|
|
909
|
+
assert.strictEqual(hi.slice(hi.indexOf(`w`)).value, `world`);
|
|
910
|
+
});
|
|
911
|
+
|
|
912
|
+
it(`startsWith as expected`, () => {
|
|
913
|
+
const hi = $S`hello world`;
|
|
914
|
+
assert.strictEqual(hi.startsWith(`h`), true);
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
it(`toUpperCase is chainable`, () => {
|
|
918
|
+
const hi = $S`hello world`;
|
|
919
|
+
assert.strictEqual(hi.toUpperCase().quote.double.value, `"HELLO WORLD"`);
|
|
920
|
+
});
|
|
921
|
+
});
|