]> Lady’s Gitweb - Pisces/blob - string.test.js
Test constructability, name, length in string.js
[Pisces] / string.test.js
1 // ♓🌟 Piscēs ∷ string.test.js
2 // ====================================================================
3 //
4 // Copyright © 2022–2023 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assert,
12 assertEquals,
13 assertSpyCalls,
14 assertStrictEquals,
15 assertThrows,
16 describe,
17 it,
18 spy,
19 } from "./dev-deps.js";
20 import {
21 asciiLowercase,
22 asciiUppercase,
23 characters,
24 codepoints,
25 codeUnits,
26 getCharacter,
27 getCodepoint,
28 getCodeUnit,
29 getFirstSubstringIndex,
30 getLastSubstringIndex,
31 join,
32 Matcher,
33 rawString,
34 scalarValues,
35 scalarValueString,
36 splitOnASCIIWhitespace,
37 splitOnCommas,
38 stringCatenate,
39 stringEndsWith,
40 stringFromCodepoints,
41 stringFromCodeUnits,
42 stringIncludes,
43 stringMatch,
44 stringMatchAll,
45 stringNormalize,
46 stringPadEnd,
47 stringPadStart,
48 stringRepeat,
49 stringReplace,
50 stringReplaceAll,
51 stringSearch,
52 stringSlice,
53 stringSplit,
54 stringStartsWith,
55 stringValue,
56 stripAndCollapseASCIIWhitespace,
57 stripLeadingAndTrailingASCIIWhitespace,
58 substring,
59 toString,
60 } from "./string.js";
61
62 describe("Matcher", () => {
63 it("[[Call]] throws an error", () => {
64 assertThrows(() => Matcher(""));
65 });
66
67 it("[[Construct]] accepts a string first argument", () => {
68 assert(new Matcher(""));
69 });
70
71 it("[[Construct]] accepts a unicode regular expression first argument", () => {
72 assert(new Matcher(/(?:)/u));
73 });
74
75 it("[[Construct]] throws with a non·unicode regular expression first argument", () => {
76 assertThrows(() => new Matcher(/(?:)/));
77 });
78
79 it("[[Construct]] creates a callable object", () => {
80 assertStrictEquals(typeof new Matcher(""), "function");
81 });
82
83 it("[[Construct]] creates a new Matcher", () => {
84 assertStrictEquals(
85 Object.getPrototypeOf(new Matcher("")),
86 Matcher.prototype,
87 );
88 });
89
90 it("[[Construct]] creates an object which inherits from RegExp", () => {
91 assert(new Matcher("") instanceof RegExp);
92 });
93
94 it("[[Construct]] throws when provided with a noncallable, non·null third argument", () => {
95 assertThrows(() => new Matcher("", undefined, "failure"));
96 });
97
98 describe(".length", () => {
99 it("[[Get]] returns the correct length", () => {
100 assertStrictEquals(Matcher.length, 1);
101 });
102 });
103
104 describe(".name", () => {
105 it("[[Get]] returns the correct name", () => {
106 assertStrictEquals(Matcher.name, "Matcher");
107 });
108 });
109
110 describe("::dotAll", () => {
111 it("[[Get]] returns true when the dotAll flag is present", () => {
112 assertStrictEquals(new Matcher(/(?:)/su).dotAll, true);
113 });
114
115 it("[[Get]] returns false when the dotAll flag is not present", () => {
116 assertStrictEquals(new Matcher(/(?:)/u).dotAll, false);
117 });
118
119 describe(".length", () => {
120 it("[[Get]] returns the correct length", () => {
121 assertStrictEquals(
122 Object.getOwnPropertyDescriptor(
123 Matcher.prototype,
124 "dotAll",
125 ).get.length,
126 0,
127 );
128 });
129 });
130
131 describe(".name", () => {
132 it("[[Get]] returns the correct name", () => {
133 assertStrictEquals(
134 Object.getOwnPropertyDescriptor(
135 Matcher.prototype,
136 "dotAll",
137 ).get.name,
138 "get dotAll",
139 );
140 });
141 });
142 });
143
144 describe("::exec", () => {
145 it("[[Call]] returns the match object given a complete match", () => {
146 assertEquals(
147 [...new Matcher(/.(?<wow>(?:.(?=.))*)(.)?/u).exec("success")],
148 ["success", "ucces", "s"],
149 );
150 assertEquals(
151 [...new Matcher(
152 /.(?<wow>(?:.(?=.))*)(.)?/u,
153 undefined,
154 ($) => $ === "success",
155 ).exec("success")],
156 ["success", "ucces", "s"],
157 );
158 });
159
160 it("[[Call]] calls the constraint if the match succeeds", () => {
161 const constraint = spy((_) => true);
162 const matcher = new Matcher("(.).*", undefined, constraint);
163 const result = matcher.exec({
164 toString() {
165 return "etaoin";
166 },
167 });
168 assertEquals([...result], ["etaoin", "e"]);
169 assertSpyCalls(constraint, 1);
170 assertStrictEquals(constraint.calls[0].args[0], "etaoin");
171 assertEquals([...constraint.calls[0].args[1]], ["etaoin", "e"]);
172 assertStrictEquals(constraint.calls[0].args[2], matcher);
173 assertStrictEquals(constraint.calls[0].self, undefined);
174 });
175
176 it("[[Call]] does not call the constraint if the match fails", () => {
177 const constraint = spy((_) => true);
178 const matcher = new Matcher("", undefined, constraint);
179 matcher.exec("failure");
180 assertSpyCalls(constraint, 0);
181 });
182
183 it("[[Call]] returns null given a partial match", () => {
184 assertStrictEquals(new Matcher("").exec("failure"), null);
185 });
186
187 it("[[Call]] returns null if the constraint fails", () => {
188 assertStrictEquals(
189 new Matcher(".*", undefined, () => false).exec(""),
190 null,
191 );
192 });
193
194 describe(".length", () => {
195 it("[[Get]] returns the correct length", () => {
196 assertStrictEquals(Matcher.prototype.exec.length, 1);
197 });
198 });
199
200 describe(".name", () => {
201 it("[[Get]] returns the correct name", () => {
202 assertStrictEquals(Matcher.prototype.exec.name, "exec");
203 });
204 });
205 });
206
207 describe("::global", () => {
208 it("[[Get]] returns true when the global flag is present", () => {
209 assertStrictEquals(new Matcher(/(?:)/gu).global, true);
210 });
211
212 it("[[Get]] returns false when the global flag is not present", () => {
213 assertStrictEquals(new Matcher(/(?:)/u).global, false);
214 });
215
216 describe(".length", () => {
217 it("[[Get]] returns the correct length", () => {
218 assertStrictEquals(
219 Object.getOwnPropertyDescriptor(
220 Matcher.prototype,
221 "global",
222 ).get.length,
223 0,
224 );
225 });
226 });
227
228 describe(".name", () => {
229 it("[[Get]] returns the correct name", () => {
230 assertStrictEquals(
231 Object.getOwnPropertyDescriptor(
232 Matcher.prototype,
233 "global",
234 ).get.name,
235 "get global",
236 );
237 });
238 });
239 });
240
241 describe("::hasIndices", () => {
242 it("[[Get]] returns true when the hasIndices flag is present", () => {
243 assertStrictEquals(new Matcher(/(?:)/du).hasIndices, true);
244 });
245
246 it("[[Get]] returns false when the hasIndices flag is not present", () => {
247 assertStrictEquals(new Matcher(/(?:)/u).hasIndices, false);
248 });
249
250 describe(".length", () => {
251 it("[[Get]] returns the correct length", () => {
252 assertStrictEquals(
253 Object.getOwnPropertyDescriptor(
254 Matcher.prototype,
255 "hasIndices",
256 ).get.length,
257 0,
258 );
259 });
260 });
261
262 describe(".name", () => {
263 it("[[Get]] returns the correct name", () => {
264 assertStrictEquals(
265 Object.getOwnPropertyDescriptor(
266 Matcher.prototype,
267 "hasIndices",
268 ).get.name,
269 "get hasIndices",
270 );
271 });
272 });
273 });
274
275 describe("::ignoreCase", () => {
276 it("[[Get]] returns true when the ignoreCase flag is present", () => {
277 assertStrictEquals(new Matcher(/(?:)/iu).ignoreCase, true);
278 });
279
280 it("[[Get]] returns false when the ignoreCase flag is not present", () => {
281 assertStrictEquals(new Matcher(/(?:)/u).ignoreCase, false);
282 });
283
284 describe(".length", () => {
285 it("[[Get]] returns the correct length", () => {
286 assertStrictEquals(
287 Object.getOwnPropertyDescriptor(
288 Matcher.prototype,
289 "ignoreCase",
290 ).get.length,
291 0,
292 );
293 });
294 });
295
296 describe(".name", () => {
297 it("[[Get]] returns the correct name", () => {
298 assertStrictEquals(
299 Object.getOwnPropertyDescriptor(
300 Matcher.prototype,
301 "ignoreCase",
302 ).get.name,
303 "get ignoreCase",
304 );
305 });
306 });
307 });
308
309 describe("::multiline", () => {
310 it("[[Get]] returns true when the multiline flag is present", () => {
311 assertStrictEquals(new Matcher(/(?:)/mu).multiline, true);
312 });
313
314 it("[[Get]] returns false when the multiline flag is not present", () => {
315 assertStrictEquals(new Matcher(/(?:)/u).multiline, false);
316 });
317
318 describe(".length", () => {
319 it("[[Get]] returns the correct length", () => {
320 assertStrictEquals(
321 Object.getOwnPropertyDescriptor(
322 Matcher.prototype,
323 "multiline",
324 ).get.length,
325 0,
326 );
327 });
328 });
329
330 describe(".name", () => {
331 it("[[Get]] returns the correct name", () => {
332 assertStrictEquals(
333 Object.getOwnPropertyDescriptor(
334 Matcher.prototype,
335 "multiline",
336 ).get.name,
337 "get multiline",
338 );
339 });
340 });
341 });
342
343 describe("::source", () => {
344 it("[[Get]] returns the RegExp source", () => {
345 assertStrictEquals(new Matcher("").source, "(?:)");
346 assertStrictEquals(new Matcher(/.*/su).source, ".*");
347 });
348
349 describe(".length", () => {
350 it("[[Get]] returns the correct length", () => {
351 assertStrictEquals(
352 Object.getOwnPropertyDescriptor(
353 Matcher.prototype,
354 "source",
355 ).get.length,
356 0,
357 );
358 });
359 });
360
361 describe(".name", () => {
362 it("[[Get]] returns the correct name", () => {
363 assertStrictEquals(
364 Object.getOwnPropertyDescriptor(
365 Matcher.prototype,
366 "source",
367 ).get.name,
368 "get source",
369 );
370 });
371 });
372 });
373
374 describe("::sticky", () => {
375 it("[[Get]] returns true when the sticky flag is present", () => {
376 assertStrictEquals(new Matcher(/(?:)/uy).sticky, true);
377 });
378
379 it("[[Get]] returns false when the sticky flag is not present", () => {
380 assertStrictEquals(new Matcher(/(?:)/u).sticky, false);
381 });
382
383 describe(".length", () => {
384 it("[[Get]] returns the correct length", () => {
385 assertStrictEquals(
386 Object.getOwnPropertyDescriptor(
387 Matcher.prototype,
388 "sticky",
389 ).get.length,
390 0,
391 );
392 });
393 });
394
395 describe(".name", () => {
396 it("[[Get]] returns the correct name", () => {
397 assertStrictEquals(
398 Object.getOwnPropertyDescriptor(
399 Matcher.prototype,
400 "sticky",
401 ).get.name,
402 "get sticky",
403 );
404 });
405 });
406 });
407
408 describe("::toString", () => {
409 it("[[Call]] returns the string source", () => {
410 assertStrictEquals(new Matcher(/(?:)/u).toString(), "/(?:)/u");
411 });
412 });
413
414 describe("::unicode", () => {
415 it("[[Get]] returns true when the unicode flag is present", () => {
416 assertStrictEquals(new Matcher(/(?:)/u).unicode, true);
417 });
418
419 describe(".length", () => {
420 it("[[Get]] returns the correct length", () => {
421 assertStrictEquals(
422 Object.getOwnPropertyDescriptor(
423 Matcher.prototype,
424 "unicode",
425 ).get.length,
426 0,
427 );
428 });
429 });
430
431 describe(".name", () => {
432 it("[[Get]] returns the correct name", () => {
433 assertStrictEquals(
434 Object.getOwnPropertyDescriptor(
435 Matcher.prototype,
436 "unicode",
437 ).get.name,
438 "get unicode",
439 );
440 });
441 });
442 });
443
444 describe("~", () => {
445 it("[[Call]] returns true for a complete match", () => {
446 assertStrictEquals(new Matcher("")(""), true);
447 assertStrictEquals(new Matcher(/.*/su)("success\nyay"), true);
448 assertStrictEquals(
449 new Matcher(/.*/su, undefined, ($) => $ === "success")(
450 "success",
451 ),
452 true,
453 );
454 });
455
456 it("[[Call]] calls the constraint if the match succeeds", () => {
457 const constraint = spy((_) => true);
458 const matcher = new Matcher("(.).*", undefined, constraint);
459 matcher("etaoin");
460 assertSpyCalls(constraint, 1);
461 assertStrictEquals(constraint.calls[0].args[0], "etaoin");
462 assertEquals([...constraint.calls[0].args[1]], ["etaoin", "e"]);
463 assertStrictEquals(constraint.calls[0].args[2], matcher);
464 assertStrictEquals(constraint.calls[0].self, undefined);
465 });
466
467 it("[[Call]] does not call the constraint if the match fails", () => {
468 const constraint = spy((_) => true);
469 const matcher = new Matcher("", undefined, constraint);
470 matcher("failure");
471 assertSpyCalls(constraint, 0);
472 });
473
474 it("[[Call]] returns false for a partial match", () => {
475 assertStrictEquals(new Matcher("")("failure"), false);
476 assertStrictEquals(new Matcher(/.*/u)("failure\nno"), false);
477 });
478
479 it("[[Call]] returns false if the constraint fails", () => {
480 assertStrictEquals(
481 new Matcher(".*", undefined, () => false)(""),
482 false,
483 );
484 });
485 });
486
487 describe("~lastIndex", () => {
488 it("[[Get]] returns zero", () => {
489 assertStrictEquals(new Matcher("").lastIndex, 0);
490 });
491
492 it("[[Set]] fails", () => {
493 assertThrows(() => (new Matcher("").lastIndex = 1));
494 });
495 });
496
497 describe("~length", () => {
498 it("[[Get]] returns one", () => {
499 assertStrictEquals(new Matcher("").length, 1);
500 });
501 });
502
503 describe("~name", () => {
504 it("[[Get]] wraps the stringified regular expression if no name was provided", () => {
505 assertStrictEquals(new Matcher("").name, "Matcher(/(?:)/u)");
506 assertStrictEquals(
507 new Matcher(/.*/gsu).name,
508 "Matcher(/.*/gsu)",
509 );
510 });
511
512 it("[[Get]] uses the provided name if one was provided", () => {
513 assertStrictEquals(new Matcher("", "success").name, "success");
514 });
515 });
516 });
517
518 describe("asciiLowercase", () => {
519 it("[[Call]] lowercases (just) A·S·C·I·I letters", () => {
520 assertStrictEquals(asciiLowercase("aBſÆss FtɁɂß"), "abſÆss ftɁɂß");
521 });
522
523 it("[[Construct]] throws an error", () => {
524 assertThrows(() => new asciiLowercase(""));
525 });
526
527 describe(".length", () => {
528 it("[[Get]] returns the correct length", () => {
529 assertStrictEquals(asciiLowercase.length, 1);
530 });
531 });
532
533 describe(".name", () => {
534 it("[[Get]] returns the correct name", () => {
535 assertStrictEquals(asciiLowercase.name, "asciiLowercase");
536 });
537 });
538 });
539
540 describe("asciiUppercase", () => {
541 it("[[Call]] uppercases (just) A·S·C·I·I letters", () => {
542 assertStrictEquals(asciiUppercase("aBſÆss FtɁɂß"), "ABſÆSS FTɁɂß");
543 });
544
545 it("[[Construct]] throws an error", () => {
546 assertThrows(() => new asciiUppercase(""));
547 });
548
549 describe(".length", () => {
550 it("[[Get]] returns the correct length", () => {
551 assertStrictEquals(asciiUppercase.length, 1);
552 });
553 });
554
555 describe(".name", () => {
556 it("[[Get]] returns the correct name", () => {
557 assertStrictEquals(asciiUppercase.name, "asciiUppercase");
558 });
559 });
560 });
561
562 describe("characters", () => {
563 it("[[Call]] returns an iterable", () => {
564 assertStrictEquals(
565 typeof characters("")[Symbol.iterator],
566 "function",
567 );
568 });
569
570 it("[[Call]] returns an iterator", () => {
571 assertStrictEquals(typeof characters("").next, "function");
572 });
573
574 it("[[Call]] returns a string character iterator", () => {
575 assertStrictEquals(
576 characters("")[Symbol.toStringTag],
577 "String Character Iterator",
578 );
579 });
580
581 it("[[Call]] iterates over the characters", () => {
582 assertEquals([
583 ...characters("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
584 ], [
585 "I",
586 "i",
587 "🎙",
588 "\uDFFF",
589 "\uDD96",
590 "\uD83C",
591 "\uD800",
592 "🆗",
593 "☺",
594 ]);
595 });
596
597 it("[[Construct]] throws an error", () => {
598 assertThrows(() => new characters(""));
599 });
600
601 describe(".length", () => {
602 it("[[Get]] returns the correct length", () => {
603 assertStrictEquals(characters.length, 1);
604 });
605 });
606
607 describe(".name", () => {
608 it("[[Get]] returns the correct name", () => {
609 assertStrictEquals(characters.name, "characters");
610 });
611 });
612 });
613
614 describe("codeUnits", () => {
615 it("[[Call]] returns an iterable", () => {
616 assertStrictEquals(
617 typeof codeUnits("")[Symbol.iterator],
618 "function",
619 );
620 });
621
622 it("[[Call]] returns an iterator", () => {
623 assertStrictEquals(typeof codeUnits("").next, "function");
624 });
625
626 it("[[Call]] returns a string code unit iterator", () => {
627 assertStrictEquals(
628 codeUnits("")[Symbol.toStringTag],
629 "String Code Unit Iterator",
630 );
631 });
632
633 it("[[Call]] iterates over the code units", () => {
634 assertEquals([
635 ...codeUnits("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
636 ], [
637 0x49,
638 0x69,
639 0xD83C,
640 0xDF99,
641 0xDFFF,
642 0xDD96,
643 0xD83C,
644 0xD800,
645 0xD83C,
646 0xDD97,
647 0x263A,
648 ]);
649 });
650
651 it("[[Construct]] throws an error", () => {
652 assertThrows(() => new codeUnits(""));
653 });
654
655 describe(".length", () => {
656 it("[[Get]] returns the correct length", () => {
657 assertStrictEquals(codeUnits.length, 1);
658 });
659 });
660
661 describe(".name", () => {
662 it("[[Get]] returns the correct name", () => {
663 assertStrictEquals(codeUnits.name, "codeUnits");
664 });
665 });
666 });
667
668 describe("codepoints", () => {
669 it("[[Call]] returns an iterable", () => {
670 assertStrictEquals(
671 typeof codepoints("")[Symbol.iterator],
672 "function",
673 );
674 });
675
676 it("[[Call]] returns an iterator", () => {
677 assertStrictEquals(typeof codepoints("").next, "function");
678 });
679
680 it("[[Call]] returns a string codepoint iterator", () => {
681 assertStrictEquals(
682 codepoints("")[Symbol.toStringTag],
683 "String Codepoint Iterator",
684 );
685 });
686
687 it("[[Call]] iterates over the codepoints", () => {
688 assertEquals([
689 ...codepoints("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
690 ], [
691 0x49,
692 0x69,
693 0x1F399,
694 0xDFFF,
695 0xDD96,
696 0xD83C,
697 0xD800,
698 0x1F197,
699 0x263A,
700 ]);
701 });
702
703 it("[[Construct]] throws an error", () => {
704 assertThrows(() => new codepoints(""));
705 });
706
707 describe(".length", () => {
708 it("[[Get]] returns the correct length", () => {
709 assertStrictEquals(codepoints.length, 1);
710 });
711 });
712
713 describe(".name", () => {
714 it("[[Get]] returns the correct name", () => {
715 assertStrictEquals(codepoints.name, "codepoints");
716 });
717 });
718 });
719
720 describe("getCharacter", () => {
721 it("[[Call]] returns the character at the provided position", () => {
722 assertStrictEquals(getCharacter("Ii🎙🆗☺", 4), "🆗");
723 });
724
725 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
726 assertStrictEquals(getCharacter("Ii🎙🆗☺", 5), "\uDD97");
727 });
728
729 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
730 assertStrictEquals(getCharacter("Ii🎙🆗☺", -1), undefined);
731 assertStrictEquals(getCharacter("Ii🎙🆗☺", 7), undefined);
732 });
733
734 it("[[Construct]] throws an error", () => {
735 assertThrows(() => new getCharacter("a", 0));
736 });
737
738 describe(".length", () => {
739 it("[[Get]] returns the correct length", () => {
740 assertStrictEquals(getCharacter.length, 2);
741 });
742 });
743
744 describe(".name", () => {
745 it("[[Get]] returns the correct name", () => {
746 assertStrictEquals(getCharacter.name, "getCharacter");
747 });
748 });
749 });
750
751 describe("getCodeUnit", () => {
752 it("[[Call]] returns the code unit at the provided position", () => {
753 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 4), 0xD83C);
754 });
755
756 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
757 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 5), 0xDD97);
758 });
759
760 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
761 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", -1), undefined);
762 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 7), undefined);
763 });
764
765 it("[[Construct]] throws an error", () => {
766 assertThrows(() => new getCodeUnit("a", 0));
767 });
768
769 describe(".length", () => {
770 it("[[Get]] returns the correct length", () => {
771 assertStrictEquals(getCodeUnit.length, 2);
772 });
773 });
774
775 describe(".name", () => {
776 it("[[Get]] returns the correct name", () => {
777 assertStrictEquals(getCodeUnit.name, "getCodeUnit");
778 });
779 });
780 });
781
782 describe("getCodepoint", () => {
783 it("[[Call]] returns the character at the provided position", () => {
784 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 4), 0x1F197);
785 });
786
787 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
788 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 5), 0xDD97);
789 });
790
791 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
792 assertStrictEquals(getCodepoint("Ii🎙🆗☺", -1), undefined);
793 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 7), undefined);
794 });
795
796 it("[[Construct]] throws an error", () => {
797 assertThrows(() => new getCodepoint("a", 0));
798 });
799
800 describe(".length", () => {
801 it("[[Get]] returns the correct length", () => {
802 assertStrictEquals(getCodepoint.length, 2);
803 });
804 });
805
806 describe(".name", () => {
807 it("[[Get]] returns the correct name", () => {
808 assertStrictEquals(getCodepoint.name, "getCodepoint");
809 });
810 });
811 });
812
813 describe("getFirstSubstringIndex", () => {
814 it("[[Call]] returns the index of the first match", () => {
815 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", "🆗"), 4);
816 });
817
818 it("[[Call]] returns −1 if no match is found", () => {
819 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", "🆗🆖"), -1);
820 });
821
822 it("[[Call]] returns 0 when provided with an empty string", () => {
823 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", ""), 0);
824 });
825
826 it("[[Construct]] throws an error", () => {
827 assertThrows(() => new getFirstSubstringIndex("", ""));
828 });
829
830 describe(".length", () => {
831 it("[[Get]] returns the correct length", () => {
832 assertStrictEquals(getFirstSubstringIndex.length, 2);
833 });
834 });
835
836 describe(".name", () => {
837 it("[[Get]] returns the correct name", () => {
838 assertStrictEquals(
839 getFirstSubstringIndex.name,
840 "getFirstSubstringIndex",
841 );
842 });
843 });
844 });
845
846 describe("getLastSubstringIndex", () => {
847 it("[[Call]] returns the index of the first match", () => {
848 assertStrictEquals(getLastSubstringIndex("Ii🎙🆗☺🆗", "🆗"), 7);
849 });
850
851 it("[[Call]] returns −1 if no match is found", () => {
852 assertStrictEquals(getLastSubstringIndex("Ii🎙🆗☺🆗", "🆖🆗"), -1);
853 });
854
855 it("[[Call]] returns the length when provided with an empty string", () => {
856 assertStrictEquals(
857 getLastSubstringIndex("Ii🎙🆗☺🆗", ""),
858 "Ii🎙🆗☺🆗".length,
859 );
860 });
861
862 it("[[Construct]] throws an error", () => {
863 assertThrows(() => new getLastSubstringIndex("", ""));
864 });
865
866 describe(".length", () => {
867 it("[[Get]] returns the correct length", () => {
868 assertStrictEquals(getLastSubstringIndex.length, 2);
869 });
870 });
871
872 describe(".name", () => {
873 it("[[Get]] returns the correct name", () => {
874 assertStrictEquals(
875 getLastSubstringIndex.name,
876 "getLastSubstringIndex",
877 );
878 });
879 });
880 });
881
882 describe("join", () => {
883 it("[[Call]] joins the provided iterator with the provided separartor", () => {
884 assertStrictEquals(join([1, 2, 3, 4].values(), "☂"), "1☂2☂3☂4");
885 });
886
887 it('[[Call]] uses "," if no separator is provided', () => {
888 assertStrictEquals(join([1, 2, 3, 4].values()), "1,2,3,4");
889 });
890
891 it("[[Call]] uses the empty sting for nullish values", () => {
892 assertStrictEquals(
893 join([null, , null, undefined].values(), "☂"),
894 "☂☂☂",
895 );
896 });
897
898 it("[[Construct]] throws an error", () => {
899 assertThrows(() => new join([]));
900 });
901
902 describe(".length", () => {
903 it("[[Get]] returns the correct length", () => {
904 assertStrictEquals(join.length, 2);
905 });
906 });
907
908 describe(".name", () => {
909 it("[[Get]] returns the correct name", () => {
910 assertStrictEquals(join.name, "join");
911 });
912 });
913 });
914
915 describe("rawString", () => {
916 it("[[Call]] acts like String.raw", () => {
917 assertStrictEquals(rawString`\nraw${" string"}`, "\\nraw string");
918 });
919
920 it("[[Construct]] throws an error", () => {
921 assertThrows(() => new rawString(["string"]));
922 });
923
924 describe(".length", () => {
925 it("[[Get]] returns the correct length", () => {
926 assertStrictEquals(rawString.length, 1);
927 });
928 });
929
930 describe(".name", () => {
931 it("[[Get]] returns the correct name", () => {
932 assertStrictEquals(rawString.name, "rawString");
933 });
934 });
935 });
936
937 describe("scalarValueString", () => {
938 it("[[Call]] replaces invalid values", () => {
939 assertStrictEquals(
940 scalarValueString("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
941 "Ii🎙\uFFFD\uFFFD\uFFFD\uFFFD🆗☺",
942 );
943 });
944
945 it("[[Construct]] throws an error", () => {
946 assertThrows(() => new scalarValueString(""));
947 });
948
949 describe(".length", () => {
950 it("[[Get]] returns the correct length", () => {
951 assertStrictEquals(scalarValueString.length, 1);
952 });
953 });
954
955 describe(".name", () => {
956 it("[[Get]] returns the correct name", () => {
957 assertStrictEquals(scalarValueString.name, "scalarValueString");
958 });
959 });
960 });
961
962 describe("scalarValues", () => {
963 it("[[Call]] returns an iterable", () => {
964 assertStrictEquals(
965 typeof scalarValues("")[Symbol.iterator],
966 "function",
967 );
968 });
969
970 it("[[Call]] returns an iterator", () => {
971 assertStrictEquals(typeof scalarValues("").next, "function");
972 });
973
974 it("[[Call]] returns a string scalar value iterator", () => {
975 assertStrictEquals(
976 scalarValues("")[Symbol.toStringTag],
977 "String Scalar Value Iterator",
978 );
979 });
980
981 it("[[Call]] iterates over the scalar values", () => {
982 assertEquals([
983 ...scalarValues("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
984 ], [
985 0x49,
986 0x69,
987 0x1F399,
988 0xFFFD,
989 0xFFFD,
990 0xFFFD,
991 0xFFFD,
992 0x1F197,
993 0x263A,
994 ]);
995 });
996
997 it("[[Construct]] throws an error", () => {
998 assertThrows(() => new scalarValues(""));
999 });
1000
1001 describe(".length", () => {
1002 it("[[Get]] returns the correct length", () => {
1003 assertStrictEquals(scalarValues.length, 1);
1004 });
1005 });
1006
1007 describe(".name", () => {
1008 it("[[Get]] returns the correct name", () => {
1009 assertStrictEquals(scalarValues.name, "scalarValues");
1010 });
1011 });
1012 });
1013
1014 describe("splitOnASCIIWhitespace", () => {
1015 it("[[Call]] splits on sequences of spaces", () => {
1016 assertEquals(
1017 splitOnASCIIWhitespace("🅰️ 🅱️ 🆎 🅾️"),
1018 ["🅰️", "🅱️", "🆎", "🅾️"],
1019 );
1020 });
1021
1022 it("[[Call]] splits on sequences of tabs", () => {
1023 assertEquals(
1024 splitOnASCIIWhitespace("🅰️\t\t\t🅱️\t🆎\t\t🅾️"),
1025 ["🅰️", "🅱️", "🆎", "🅾️"],
1026 );
1027 });
1028
1029 it("[[Call]] splits on sequences of carriage returns", () => {
1030 assertEquals(
1031 splitOnASCIIWhitespace("🅰️\r\r\r🅱️\r🆎\r\r🅾️"),
1032 ["🅰️", "🅱️", "🆎", "🅾️"],
1033 );
1034 });
1035
1036 it("[[Call]] splits on sequences of newlines", () => {
1037 assertEquals(
1038 splitOnASCIIWhitespace("🅰️\r\r\r🅱️\r🆎\r\r🅾️"),
1039 ["🅰️", "🅱️", "🆎", "🅾️"],
1040 );
1041 });
1042
1043 it("[[Call]] splits on sequences of form feeds", () => {
1044 assertEquals(
1045 splitOnASCIIWhitespace("🅰️\f\f\f🅱️\f🆎\f\f🅾️"),
1046 ["🅰️", "🅱️", "🆎", "🅾️"],
1047 );
1048 });
1049
1050 it("[[Call]] splits on mixed whitespace", () => {
1051 assertEquals(
1052 splitOnASCIIWhitespace("🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️"),
1053 ["🅰️", "🅱️", "🆎", "🅾️"],
1054 );
1055 });
1056
1057 it("[[Call]] returns an array of just the empty string for the empty string", () => {
1058 assertEquals(splitOnASCIIWhitespace(""), [""]);
1059 });
1060
1061 it("[[Call]] returns a single token if there are no spaces", () => {
1062 assertEquals(splitOnASCIIWhitespace("abcd"), ["abcd"]);
1063 });
1064
1065 it("[[Call]] does not split on other kinds of whitespace", () => {
1066 assertEquals(
1067 splitOnASCIIWhitespace("a\u202F\u205F\xa0\v\0\bb"),
1068 ["a\u202F\u205F\xa0\v\0\bb"],
1069 );
1070 });
1071
1072 it("[[Call]] trims leading and trailing whitespace", () => {
1073 assertEquals(
1074 splitOnASCIIWhitespace(
1075 "\f\r\n\r\n \n\t🅰️\f \t\n🅱️\r🆎\n\f🅾️\n\f",
1076 ),
1077 ["🅰️", "🅱️", "🆎", "🅾️"],
1078 );
1079 });
1080
1081 it("[[Construct]] throws an error", () => {
1082 assertThrows(() => new splitOnASCIIWhitespace(""));
1083 });
1084
1085 describe(".length", () => {
1086 it("[[Get]] returns the correct length", () => {
1087 assertStrictEquals(splitOnASCIIWhitespace.length, 1);
1088 });
1089 });
1090
1091 describe(".name", () => {
1092 it("[[Get]] returns the correct name", () => {
1093 assertStrictEquals(
1094 splitOnASCIIWhitespace.name,
1095 "splitOnASCIIWhitespace",
1096 );
1097 });
1098 });
1099 });
1100
1101 describe("splitOnCommas", () => {
1102 it("[[Call]] splits on commas", () => {
1103 assertEquals(
1104 splitOnCommas("🅰️,🅱️,🆎,🅾️"),
1105 ["🅰️", "🅱️", "🆎", "🅾️"],
1106 );
1107 });
1108
1109 it("[[Call]] returns an array of just the empty string for the empty string", () => {
1110 assertEquals(splitOnCommas(""), [""]);
1111 });
1112
1113 it("[[Call]] returns a single token if there are no commas", () => {
1114 assertEquals(splitOnCommas("abcd"), ["abcd"]);
1115 });
1116
1117 it("[[Call]] splits into empty strings if there are only commas", () => {
1118 assertEquals(splitOnCommas(",,,"), ["", "", "", ""]);
1119 });
1120
1121 it("[[Call]] trims leading and trailing whitespace", () => {
1122 assertEquals(
1123 splitOnCommas("\f\r\n\r\n \n\t🅰️,🅱️,🆎,🅾️\n\f"),
1124 ["🅰️", "🅱️", "🆎", "🅾️"],
1125 );
1126 assertEquals(
1127 splitOnCommas("\f\r\n\r\n \n\t,,,\n\f"),
1128 ["", "", "", ""],
1129 );
1130 });
1131
1132 it("[[Call]] removes whitespace from the split tokens", () => {
1133 assertEquals(
1134 splitOnCommas(
1135 "\f\r\n\r\n \n\t🅰️\f , \t\n🅱️,\r\n\r🆎\n\f,🅾️\n\f",
1136 ),
1137 ["🅰️", "🅱️", "🆎", "🅾️"],
1138 );
1139 assertEquals(
1140 splitOnCommas("\f\r\n\r\n \n\t\f , \t\n,\r\n\r\n\f,\n\f"),
1141 ["", "", "", ""],
1142 );
1143 });
1144
1145 it("[[Construct]] throws an error", () => {
1146 assertThrows(() => new splitOnCommas(""));
1147 });
1148
1149 describe(".length", () => {
1150 it("[[Get]] returns the correct length", () => {
1151 assertStrictEquals(splitOnCommas.length, 1);
1152 });
1153 });
1154
1155 describe(".name", () => {
1156 it("[[Get]] returns the correct name", () => {
1157 assertStrictEquals(splitOnCommas.name, "splitOnCommas");
1158 });
1159 });
1160 });
1161
1162 describe("stringCatenate", () => {
1163 it("[[Call]] catenates the values", () => {
1164 assertStrictEquals(stringCatenate("the", " values"), "the values");
1165 });
1166
1167 it("[[Call]] returns an empty string when called with no values", () => {
1168 assertStrictEquals(stringCatenate(), "");
1169 });
1170
1171 it('[[Call]] uses "undefined" when explicitly provided undefined', () => {
1172 assertStrictEquals(
1173 stringCatenate(undefined, undefined),
1174 "undefinedundefined",
1175 );
1176 });
1177
1178 it('[[Call]] uses "null" when provided null', () => {
1179 assertStrictEquals(stringCatenate(null, null), "nullnull");
1180 });
1181
1182 it("[[Construct]] throws an error", () => {
1183 assertThrows(() => new stringCatenate());
1184 });
1185
1186 describe(".length", () => {
1187 it("[[Get]] returns the correct length", () => {
1188 assertStrictEquals(stringCatenate.length, 2);
1189 });
1190 });
1191
1192 describe(".name", () => {
1193 it("[[Get]] returns the correct name", () => {
1194 assertStrictEquals(stringCatenate.name, "stringCatenate");
1195 });
1196 });
1197 });
1198
1199 describe("stringEndsWith", () => {
1200 it("[[Call]] returns whether the string ends with the thing", () => {
1201 assertStrictEquals(
1202 stringEndsWith("very success", " success"),
1203 true,
1204 );
1205 assertStrictEquals(stringEndsWith("very fail", " success"), false);
1206 });
1207
1208 it("[[Call]] accepts an offset", () => {
1209 assertStrictEquals(
1210 stringEndsWith("very successful", " success", 12),
1211 true,
1212 );
1213 });
1214
1215 it("[[Call]] returns true for an empty string test", () => {
1216 assertStrictEquals(stringEndsWith("", ""), true);
1217 });
1218
1219 it("[[Construct]] throws an error", () => {
1220 assertThrows(() => new stringEndsWith("", ""));
1221 });
1222
1223 describe(".length", () => {
1224 it("[[Get]] returns the correct length", () => {
1225 assertStrictEquals(stringEndsWith.length, 2);
1226 });
1227 });
1228
1229 describe(".name", () => {
1230 it("[[Get]] returns the correct name", () => {
1231 assertStrictEquals(stringEndsWith.name, "stringEndsWith");
1232 });
1233 });
1234 });
1235
1236 describe("stringFromCodeUnits", () => {
1237 it("[[Call]] makes the string", () => {
1238 assertStrictEquals(
1239 stringFromCodeUnits(0xD83C, 0xDD97),
1240 "🆗",
1241 );
1242 });
1243
1244 it("[[Call]] throws with non‐integral arguments", () => {
1245 assertThrows(() => stringFromCodeUnits(NaN));
1246 assertThrows(() => stringFromCodeUnits(Infinity));
1247 assertThrows(() => stringFromCodeUnits(0.1));
1248 });
1249
1250 it("[[Call]] throws with arguments out of range", () => {
1251 assertThrows(() => stringFromCodeUnits(-1));
1252 assertThrows(() => stringFromCodeUnits(0x10000));
1253 });
1254
1255 it("[[Construct]] throws an error", () => {
1256 assertThrows(() => new stringFromCodeUnits([]));
1257 });
1258
1259 describe(".length", () => {
1260 it("[[Get]] returns the correct length", () => {
1261 assertStrictEquals(stringFromCodeUnits.length, 1);
1262 });
1263 });
1264
1265 describe(".name", () => {
1266 it("[[Get]] returns the correct name", () => {
1267 assertStrictEquals(
1268 stringFromCodeUnits.name,
1269 "stringFromCodeUnits",
1270 );
1271 });
1272 });
1273 });
1274
1275 describe("stringFromCodepoints", () => {
1276 it("[[Call]] makes the string", () => {
1277 assertStrictEquals(stringFromCodepoints(0x1F197), "🆗");
1278 });
1279
1280 it("[[Call]] throws with non‐integral arguments", () => {
1281 assertThrows(() => stringFromCodepoints(NaN));
1282 assertThrows(() => stringFromCodepoints(Infinity));
1283 assertThrows(() => stringFromCodepoints(0.1));
1284 });
1285
1286 it("[[Call]] throws with arguments out of range", () => {
1287 assertThrows(() => stringFromCodepoints(-1));
1288 assertThrows(() => stringFromCodepoints(0x110000));
1289 });
1290
1291 it("[[Construct]] throws an error", () => {
1292 assertThrows(() => new stringFromCodepoints([]));
1293 });
1294
1295 describe(".length", () => {
1296 it("[[Get]] returns the correct length", () => {
1297 assertStrictEquals(stringFromCodepoints.length, 1);
1298 });
1299 });
1300
1301 describe(".name", () => {
1302 it("[[Get]] returns the correct name", () => {
1303 assertStrictEquals(
1304 stringFromCodepoints.name,
1305 "stringFromCodepoints",
1306 );
1307 });
1308 });
1309 });
1310
1311 describe("stringIncludes", () => {
1312 it("[[Call]] returns whether the string includes the thing", () => {
1313 assertStrictEquals(
1314 stringIncludes("very success full", " success "),
1315 true,
1316 );
1317 assertStrictEquals(
1318 stringIncludes("very fail full", " success "),
1319 false,
1320 );
1321 });
1322
1323 it("[[Call]] accepts an offset", () => {
1324 assertStrictEquals(
1325 stringIncludes("maybe success full", " success ", 4),
1326 true,
1327 );
1328 assertStrictEquals(
1329 stringIncludes("maybe success full", " success ", 5),
1330 true,
1331 );
1332 assertStrictEquals(
1333 stringIncludes("maybe success full", " success ", 6),
1334 false,
1335 );
1336 });
1337
1338 it("[[Call]] returns true for an empty string test", () => {
1339 assertStrictEquals(stringIncludes("", ""), true);
1340 });
1341
1342 it("[[Construct]] throws an error", () => {
1343 assertThrows(() => new stringIncludes("", ""));
1344 });
1345
1346 describe(".length", () => {
1347 it("[[Get]] returns the correct length", () => {
1348 assertStrictEquals(stringIncludes.length, 2);
1349 });
1350 });
1351
1352 describe(".name", () => {
1353 it("[[Get]] returns the correct name", () => {
1354 assertStrictEquals(stringIncludes.name, "stringIncludes");
1355 });
1356 });
1357 });
1358
1359 describe("stringMatch", () => {
1360 it("[[Call]] does the match akin to String::match", () => {
1361 assertEquals(
1362 [...stringMatch("very success full", /([sc]+[ue]?)+/)],
1363 ["success", "ss"],
1364 );
1365 assertEquals(
1366 [...stringMatch("very success full", /([sc]+)[ue]?/g)],
1367 ["su", "cce", "ss"],
1368 );
1369 });
1370
1371 it("[[Construct]] throws an error", () => {
1372 assertThrows(() => new stringMatch("", /(?:)/));
1373 });
1374
1375 describe(".length", () => {
1376 it("[[Get]] returns the correct length", () => {
1377 assertStrictEquals(stringMatch.length, 2);
1378 });
1379 });
1380
1381 describe(".name", () => {
1382 it("[[Get]] returns the correct name", () => {
1383 assertStrictEquals(stringMatch.name, "stringMatch");
1384 });
1385 });
1386 });
1387
1388 describe("stringMatchAll", () => {
1389 it("[[Call]] does the match akin to String::matchAll", () => {
1390 assertEquals(
1391 [...stringMatchAll("very success full", /([sc]+)[ue]?/g)].map((
1392 match,
1393 ) => [...match]),
1394 [["su", "s"], ["cce", "cc"], ["ss", "ss"]],
1395 );
1396 });
1397
1398 it("[[Construct]] throws an error", () => {
1399 assertThrows(() => new stringMatchAll("", /(?:)/g));
1400 });
1401
1402 describe(".length", () => {
1403 it("[[Get]] returns the correct length", () => {
1404 assertStrictEquals(stringMatchAll.length, 2);
1405 });
1406 });
1407
1408 describe(".name", () => {
1409 it("[[Get]] returns the correct name", () => {
1410 assertStrictEquals(stringMatchAll.name, "stringMatchAll");
1411 });
1412 });
1413 });
1414
1415 describe("stringNormalize", () => {
1416 it("[[Call]] normalizes the string properly", () => {
1417 assertStrictEquals(stringNormalize("ẛ", "NFC"), "\u1E9B");
1418 assertStrictEquals(stringNormalize("ẛ", "NFD"), "\u017F\u0307");
1419 assertStrictEquals(stringNormalize("ẛ", "NFKC"), "\u1E61");
1420 assertStrictEquals(stringNormalize("ẛ", "NFKD"), "\u0073\u0307");
1421 });
1422
1423 it("[[Call]] assumes NFC", () => {
1424 assertStrictEquals(stringNormalize("\u017F\u0307"), "\u1E9B");
1425 });
1426
1427 it("[[Call]] throws with an invalid form", () => {
1428 assertThrows(() => stringNormalize("", "NFB"));
1429 });
1430
1431 it("[[Construct]] throws an error", () => {
1432 assertThrows(() => new stringNormalize("", "NFC"));
1433 });
1434
1435 describe(".length", () => {
1436 it("[[Get]] returns the correct length", () => {
1437 assertStrictEquals(stringNormalize.length, 1);
1438 });
1439 });
1440
1441 describe(".name", () => {
1442 it("[[Get]] returns the correct name", () => {
1443 assertStrictEquals(stringNormalize.name, "stringNormalize");
1444 });
1445 });
1446 });
1447
1448 describe("stringPadEnd", () => {
1449 it("[[Call]] pads the end of the string", () => {
1450 assertStrictEquals(stringPadEnd("xx", 3), "xx ");
1451 assertStrictEquals(stringPadEnd("xx", 3, "o"), "xxo");
1452 assertStrictEquals(stringPadEnd("", 3, "xo"), "xox");
1453 assertStrictEquals(stringPadEnd("xx", 3, ""), "xx");
1454 });
1455
1456 it("[[Construct]] throws an error", () => {
1457 assertThrows(() => new stringPadEnd("", 1));
1458 });
1459
1460 describe(".length", () => {
1461 it("[[Get]] returns the correct length", () => {
1462 assertStrictEquals(stringPadEnd.length, 2);
1463 });
1464 });
1465
1466 describe(".name", () => {
1467 it("[[Get]] returns the correct name", () => {
1468 assertStrictEquals(stringPadEnd.name, "stringPadEnd");
1469 });
1470 });
1471 });
1472
1473 describe("stringPadStart", () => {
1474 it("[[Call]] pads the start of the string", () => {
1475 assertStrictEquals(stringPadStart("xx", 3), " xx");
1476 assertStrictEquals(stringPadStart("xx", 3, "o"), "oxx");
1477 assertStrictEquals(stringPadStart("", 3, "xo"), "xox");
1478 assertStrictEquals(stringPadStart("xx", 3, ""), "xx");
1479 });
1480
1481 it("[[Construct]] throws an error", () => {
1482 assertThrows(() => new stringPadStart("", 1));
1483 });
1484
1485 describe(".length", () => {
1486 it("[[Get]] returns the correct length", () => {
1487 assertStrictEquals(stringPadStart.length, 2);
1488 });
1489 });
1490
1491 describe(".name", () => {
1492 it("[[Get]] returns the correct name", () => {
1493 assertStrictEquals(stringPadStart.name, "stringPadStart");
1494 });
1495 });
1496 });
1497
1498 describe("stringRepeat", () => {
1499 it("[[Call]] repeats the string", () => {
1500 assertStrictEquals(stringRepeat("xx", 3), "xxxxxx");
1501 assertStrictEquals(stringRepeat("", 3), "");
1502 assertStrictEquals(stringRepeat("xx", 0), "");
1503 });
1504
1505 it("[[Call]] throws for negative repititions", () => {
1506 assertThrows(() => stringRepeat("", -1));
1507 });
1508
1509 it("[[Call]] throws for infinite repititions", () => {
1510 assertThrows(() => stringRepeat("", Infinity));
1511 });
1512
1513 it("[[Construct]] throws an error", () => {
1514 assertThrows(() => new stringRepeat("", 1));
1515 });
1516
1517 describe(".length", () => {
1518 it("[[Get]] returns the correct length", () => {
1519 assertStrictEquals(stringRepeat.length, 2);
1520 });
1521 });
1522
1523 describe(".name", () => {
1524 it("[[Get]] returns the correct name", () => {
1525 assertStrictEquals(stringRepeat.name, "stringRepeat");
1526 });
1527 });
1528 });
1529
1530 describe("stringReplace", () => {
1531 it("[[Call]] does the replacement akin to String::replace", () => {
1532 assertStrictEquals(
1533 stringReplace("it’s a failure", "failure", "success"),
1534 "it’s a success",
1535 );
1536 assertStrictEquals(
1537 stringReplace(
1538 "very success full",
1539 /([sc]+)[ue]?/,
1540 ($) => $.length,
1541 ),
1542 "very 2ccess full",
1543 );
1544 assertStrictEquals(
1545 stringReplace(
1546 "very success full",
1547 /([sc]+)[ue]?/g,
1548 (...$s) =>
1549 `${$s[0].length}`.repeat($s[1].length) +
1550 $s[0].substring($s[1].length),
1551 ),
1552 "very 2u33e22 full",
1553 );
1554 });
1555
1556 it("[[Construct]] throws an error", () => {
1557 assertThrows(() => new stringReplace("", /(?:)/, ""));
1558 });
1559
1560 describe(".length", () => {
1561 it("[[Get]] returns the correct length", () => {
1562 assertStrictEquals(stringReplace.length, 3);
1563 });
1564 });
1565
1566 describe(".name", () => {
1567 it("[[Get]] returns the correct name", () => {
1568 assertStrictEquals(stringReplace.name, "stringReplace");
1569 });
1570 });
1571 });
1572
1573 describe("stringReplaceAll", () => {
1574 it("[[Call]] does the match akin to String::replaceAll", () => {
1575 assertStrictEquals(
1576 stringReplaceAll("it’s a failure failure", "failure", "success"),
1577 "it’s a success success",
1578 );
1579 assertStrictEquals(
1580 stringReplaceAll(
1581 "very success full",
1582 /([sc]+)[ue]?/g,
1583 (...$s) =>
1584 `${$s[0].length}`.repeat($s[1].length) +
1585 $s[0].substring($s[1].length),
1586 ),
1587 "very 2u33e22 full",
1588 );
1589 });
1590
1591 it("[[Construct]] throws an error", () => {
1592 assertThrows(() => new stringReplaceAll("", /(?:)/g));
1593 });
1594
1595 describe(".length", () => {
1596 it("[[Get]] returns the correct length", () => {
1597 assertStrictEquals(stringReplaceAll.length, 3);
1598 });
1599 });
1600
1601 describe(".name", () => {
1602 it("[[Get]] returns the correct name", () => {
1603 assertStrictEquals(stringReplaceAll.name, "stringReplaceAll");
1604 });
1605 });
1606 });
1607
1608 describe("stringSearch", () => {
1609 it("[[Call]] does the search akin to String::search", () => {
1610 assertStrictEquals(
1611 stringSearch("very success full", /([sc]+)[ue]?/),
1612 5,
1613 );
1614 assertStrictEquals(
1615 stringSearch("very fail full", /([sc]+)[ue]?/),
1616 -1,
1617 );
1618 });
1619
1620 it("[[Construct]] throws an error", () => {
1621 assertThrows(() => new stringSearch("", /(?:)/));
1622 });
1623
1624 describe(".length", () => {
1625 it("[[Get]] returns the correct length", () => {
1626 assertStrictEquals(stringSearch.length, 2);
1627 });
1628 });
1629
1630 describe(".name", () => {
1631 it("[[Get]] returns the correct name", () => {
1632 assertStrictEquals(stringSearch.name, "stringSearch");
1633 });
1634 });
1635 });
1636
1637 describe("stringSlice", () => {
1638 it("[[Call]] slices the string akin to String::search", () => {
1639 assertStrictEquals(
1640 stringSlice("very success full", 5, 12),
1641 "success",
1642 );
1643 assertStrictEquals(
1644 stringSlice("very success full", -12, -5),
1645 "success",
1646 );
1647 });
1648
1649 it("[[Construct]] throws an error", () => {
1650 assertThrows(() => new stringSlice("", 0, 0));
1651 });
1652
1653 describe(".length", () => {
1654 it("[[Get]] returns the correct length", () => {
1655 assertStrictEquals(stringSlice.length, 3);
1656 });
1657 });
1658
1659 describe(".name", () => {
1660 it("[[Get]] returns the correct name", () => {
1661 assertStrictEquals(stringSlice.name, "stringSlice");
1662 });
1663 });
1664 });
1665
1666 describe("stringSplit", () => {
1667 it("[[Call]] splits the string akin to String::split", () => {
1668 assertEquals(stringSplit("success", ""), [
1669 "s",
1670 "u",
1671 "c",
1672 "c",
1673 "e",
1674 "s",
1675 "s",
1676 ]);
1677 assertEquals(stringSplit("success", /(?<=[aeiou])(?=[^aeiou])/), [
1678 "su",
1679 "cce",
1680 "ss",
1681 ]);
1682 assertEquals(stringSplit("success", "failure"), ["success"]);
1683 });
1684
1685 it("[[Call]] recognizes a limit", () => {
1686 assertEquals(stringSplit("success", "", 4), ["s", "u", "c", "c"]);
1687 });
1688
1689 it("[[Construct]] throws an error", () => {
1690 assertThrows(() => new stringSplit("", ""));
1691 });
1692
1693 describe(".length", () => {
1694 it("[[Get]] returns the correct length", () => {
1695 assertStrictEquals(stringSplit.length, 3);
1696 });
1697 });
1698
1699 describe(".name", () => {
1700 it("[[Get]] returns the correct name", () => {
1701 assertStrictEquals(stringSplit.name, "stringSplit");
1702 });
1703 });
1704 });
1705
1706 describe("stringStartsWith", () => {
1707 it("[[Call]] returns whether the string starts with the thing", () => {
1708 assertStrictEquals(
1709 stringStartsWith("success is had", "success "),
1710 true,
1711 );
1712 assertStrictEquals(
1713 stringStartsWith("no success is had", "success "),
1714 false,
1715 );
1716 });
1717
1718 it("[[Call]] accepts an offset", () => {
1719 assertStrictEquals(
1720 stringStartsWith("much success is had", "success ", 5),
1721 true,
1722 );
1723 });
1724
1725 it("[[Call]] returns true for an empty string test", () => {
1726 assertStrictEquals(stringEndsWith("", ""), true);
1727 });
1728
1729 it("[[Construct]] throws an error", () => {
1730 assertThrows(() => new stringStartsWith("", ""));
1731 });
1732
1733 describe(".length", () => {
1734 it("[[Get]] returns the correct length", () => {
1735 assertStrictEquals(stringStartsWith.length, 2);
1736 });
1737 });
1738
1739 describe(".name", () => {
1740 it("[[Get]] returns the correct name", () => {
1741 assertStrictEquals(stringStartsWith.name, "stringStartsWith");
1742 });
1743 });
1744 });
1745
1746 describe("stringStartsWith", () => {
1747 it("[[Call]] returns the string value of a string literal", () => {
1748 assertStrictEquals(stringValue("success"), "success");
1749 });
1750
1751 it("[[Call]] returns the string value of a string object", () => {
1752 const string = new String("success");
1753 Object.defineProperties(string, {
1754 toString: { value: () => "failure" },
1755 valueOf: { value: () => "failure" },
1756 });
1757 assertStrictEquals(stringValue(string), "success");
1758 });
1759
1760 it("[[Call]] throws for non‐strings", () => {
1761 assertThrows(() => stringValue(Object.create(String.prototype)));
1762 });
1763
1764 it("[[Construct]] throws an error", () => {
1765 assertThrows(() => new stringValue(""));
1766 });
1767
1768 describe(".length", () => {
1769 it("[[Get]] returns the correct length", () => {
1770 assertStrictEquals(stringValue.length, 1);
1771 });
1772 });
1773
1774 describe(".name", () => {
1775 it("[[Get]] returns the correct name", () => {
1776 assertStrictEquals(stringValue.name, "stringValue");
1777 });
1778 });
1779 });
1780
1781 describe("stripAndCollapseASCIIWhitespace", () => {
1782 it("[[Call]] collapses mixed inner whitespace", () => {
1783 assertEquals(
1784 stripAndCollapseASCIIWhitespace("🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️"),
1785 "🅰️ 🅱️ 🆎 🅾️",
1786 );
1787 });
1788
1789 it("[[Call]] trims leading and trailing whitespace", () => {
1790 assertStrictEquals(
1791 stripAndCollapseASCIIWhitespace(
1792 "\f\r\n\r\n \n\t\f 🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️\n\f",
1793 ),
1794 "🅰️ 🅱️ 🆎 🅾️",
1795 );
1796 });
1797
1798 it("[[Call]] returns the empty string for strings of whitespace", () => {
1799 assertStrictEquals(
1800 stripAndCollapseASCIIWhitespace("\f\r\n\r\n \n\t\f \n\f"),
1801 "",
1802 );
1803 });
1804
1805 it("[[Call]] does not collapse other kinds of whitespace", () => {
1806 assertEquals(
1807 stripAndCollapseASCIIWhitespace("a\u202F\u205F\xa0\v\0\bb"),
1808 "a\u202F\u205F\xa0\v\0\bb",
1809 );
1810 });
1811
1812 it("[[Construct]] throws an error", () => {
1813 assertThrows(() => new stripAndCollapseASCIIWhitespace(""));
1814 });
1815
1816 describe(".length", () => {
1817 it("[[Get]] returns the correct length", () => {
1818 assertStrictEquals(stripAndCollapseASCIIWhitespace.length, 1);
1819 });
1820 });
1821
1822 describe(".name", () => {
1823 it("[[Get]] returns the correct name", () => {
1824 assertStrictEquals(
1825 stripAndCollapseASCIIWhitespace.name,
1826 "stripAndCollapseASCIIWhitespace",
1827 );
1828 });
1829 });
1830 });
1831
1832 describe("stripLeadingAndTrailingASCIIWhitespace", () => {
1833 it("[[Call]] trims leading and trailing whitespace", () => {
1834 assertStrictEquals(
1835 stripLeadingAndTrailingASCIIWhitespace(
1836 "\f\r\n\r\n \n\t\f 🅰️🅱️🆎🅾️\n\f",
1837 ),
1838 "🅰️🅱️🆎🅾️",
1839 );
1840 });
1841
1842 it("[[Call]] returns the empty string for strings of whitespace", () => {
1843 assertStrictEquals(
1844 stripLeadingAndTrailingASCIIWhitespace("\f\r\n\r\n \n\t\f \n\f"),
1845 "",
1846 );
1847 });
1848
1849 it("[[Call]] does not trim other kinds of whitespace", () => {
1850 assertEquals(
1851 stripLeadingAndTrailingASCIIWhitespace(
1852 "\v\u202F\u205Fx\0\b\xa0",
1853 ),
1854 "\v\u202F\u205Fx\0\b\xa0",
1855 );
1856 });
1857
1858 it("[[Call]] does not adjust inner whitespace", () => {
1859 assertEquals(
1860 stripLeadingAndTrailingASCIIWhitespace("a b"),
1861 "a b",
1862 );
1863 });
1864
1865 it("[[Construct]] throws an error", () => {
1866 assertThrows(() => new stripLeadingAndTrailingASCIIWhitespace(""));
1867 });
1868
1869 describe(".length", () => {
1870 it("[[Get]] returns the correct length", () => {
1871 assertStrictEquals(
1872 stripLeadingAndTrailingASCIIWhitespace.length,
1873 1,
1874 );
1875 });
1876 });
1877
1878 describe(".name", () => {
1879 it("[[Get]] returns the correct name", () => {
1880 assertStrictEquals(
1881 stripLeadingAndTrailingASCIIWhitespace.name,
1882 "stripLeadingAndTrailingASCIIWhitespace",
1883 );
1884 });
1885 });
1886 });
1887
1888 describe("substring", () => {
1889 it("[[Call]] returns the substring", () => {
1890 assertStrictEquals(
1891 substring("success", 0),
1892 "success",
1893 );
1894 assertStrictEquals(
1895 substring("very success full", 5, 12),
1896 "success",
1897 );
1898 });
1899
1900 it("[[Construct]] throws an error", () => {
1901 assertThrows(() => new substring("", 0));
1902 });
1903
1904 describe(".length", () => {
1905 it("[[Get]] returns the correct length", () => {
1906 assertStrictEquals(substring.length, 3);
1907 });
1908 });
1909
1910 describe(".name", () => {
1911 it("[[Get]] returns the correct name", () => {
1912 assertStrictEquals(substring.name, "substring");
1913 });
1914 });
1915 });
1916
1917 describe("toString", () => {
1918 it("[[Call]] converts to a string", () => {
1919 assertStrictEquals(
1920 toString({
1921 toString() {
1922 return "success";
1923 },
1924 }),
1925 "success",
1926 );
1927 });
1928
1929 it("[[Call]] throws when provided a symbol", () => {
1930 assertThrows(() => toString(Symbol()));
1931 });
1932
1933 it("[[Construct]] throws an error", () => {
1934 assertThrows(() => new toString(""));
1935 });
1936
1937 describe(".length", () => {
1938 it("[[Get]] returns the correct length", () => {
1939 assertStrictEquals(toString.length, 1);
1940 });
1941 });
1942
1943 describe(".name", () => {
1944 it("[[Get]] returns the correct name", () => {
1945 assertStrictEquals(toString.name, "toString");
1946 });
1947 });
1948 });
This page took 0.514017 seconds and 5 git commands to generate.