1 // ♓🌟 Piscēs ∷ string.test.js
2 // ====================================================================
4 // Copyright © 2022–2023 Lady [@ Lady’s Computer].
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/>.
19 } from "./dev-deps.js";
29 getFirstSubstringIndex
,
30 getLastSubstringIndex
,
36 splitOnASCIIWhitespace
,
56 stripAndCollapseASCIIWhitespace
,
57 stripLeadingAndTrailingASCIIWhitespace
,
62 describe("Matcher", () => {
63 it("[[Call]] throws an error", () => {
64 assertThrows(() => Matcher(""));
67 it("[[Construct]] accepts a string first argument", () => {
68 assert(new Matcher(""));
71 it("[[Construct]] accepts a unicode regular expression first argument", () => {
72 assert(new Matcher(/(?:)/u));
75 it("[[Construct]] throws with a non·unicode regular expression first argument", () => {
76 assertThrows(() => new Matcher(/(?:)/));
79 it("[[Construct]] creates a callable object", () => {
80 assertStrictEquals(typeof new Matcher(""), "function");
83 it("[[Construct]] creates a new Matcher", () => {
85 Object
.getPrototypeOf(new Matcher("")),
90 it("[[Construct]] creates an object which inherits from RegExp", () => {
91 assert(new Matcher("") instanceof RegExp
);
94 it("[[Construct]] throws when provided with a noncallable, non·null third argument", () => {
95 assertThrows(() => new Matcher("", undefined, "failure"));
98 describe(".length", () => {
99 it("[[Get]] returns the correct length", () => {
100 assertStrictEquals(Matcher
.length
, 1);
104 describe(".name", () => {
105 it("[[Get]] returns the correct name", () => {
106 assertStrictEquals(Matcher
.name
, "Matcher");
110 describe("::dotAll", () => {
111 it("[[Get]] returns true when the dotAll flag is present", () => {
112 assertStrictEquals(new Matcher(/(?:)/su).dotAll
, true);
115 it("[[Get]] returns false when the dotAll flag is not present", () => {
116 assertStrictEquals(new Matcher(/(?:)/u).dotAll
, false);
119 describe(".length", () => {
120 it("[[Get]] returns the correct length", () => {
122 Object
.getOwnPropertyDescriptor(
131 describe(".name", () => {
132 it("[[Get]] returns the correct name", () => {
134 Object
.getOwnPropertyDescriptor(
144 describe("::exec", () => {
145 it("[[Call]] returns the match object given a complete match", () => {
147 [...new Matcher(/.(?<wow>(?:.(?=.))*)(.)?/u).exec("success")],
148 ["success", "ucces", "s"],
152 /.(?<wow>(?:.(?=.))*)(.)?/u,
154 ($) => $ === "success",
156 ["success", "ucces", "s"],
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({
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);
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);
183 it("[[Call]] returns null given a partial match", () => {
184 assertStrictEquals(new Matcher("").exec("failure"), null);
187 it("[[Call]] returns null if the constraint fails", () => {
189 new Matcher(".*", undefined, () => false).exec(""),
194 describe(".length", () => {
195 it("[[Get]] returns the correct length", () => {
196 assertStrictEquals(Matcher
.prototype.exec
.length
, 1);
200 describe(".name", () => {
201 it("[[Get]] returns the correct name", () => {
202 assertStrictEquals(Matcher
.prototype.exec
.name
, "exec");
207 describe("::global", () => {
208 it("[[Get]] returns true when the global flag is present", () => {
209 assertStrictEquals(new Matcher(/(?:)/gu).global
, true);
212 it("[[Get]] returns false when the global flag is not present", () => {
213 assertStrictEquals(new Matcher(/(?:)/u).global
, false);
216 describe(".length", () => {
217 it("[[Get]] returns the correct length", () => {
219 Object
.getOwnPropertyDescriptor(
228 describe(".name", () => {
229 it("[[Get]] returns the correct name", () => {
231 Object
.getOwnPropertyDescriptor(
241 describe("::hasIndices", () => {
242 it("[[Get]] returns true when the hasIndices flag is present", () => {
243 assertStrictEquals(new Matcher(/(?:)/du).hasIndices
, true);
246 it("[[Get]] returns false when the hasIndices flag is not present", () => {
247 assertStrictEquals(new Matcher(/(?:)/u).hasIndices
, false);
250 describe(".length", () => {
251 it("[[Get]] returns the correct length", () => {
253 Object
.getOwnPropertyDescriptor(
262 describe(".name", () => {
263 it("[[Get]] returns the correct name", () => {
265 Object
.getOwnPropertyDescriptor(
275 describe("::ignoreCase", () => {
276 it("[[Get]] returns true when the ignoreCase flag is present", () => {
277 assertStrictEquals(new Matcher(/(?:)/iu).ignoreCase
, true);
280 it("[[Get]] returns false when the ignoreCase flag is not present", () => {
281 assertStrictEquals(new Matcher(/(?:)/u).ignoreCase
, false);
284 describe(".length", () => {
285 it("[[Get]] returns the correct length", () => {
287 Object
.getOwnPropertyDescriptor(
296 describe(".name", () => {
297 it("[[Get]] returns the correct name", () => {
299 Object
.getOwnPropertyDescriptor(
309 describe("::multiline", () => {
310 it("[[Get]] returns true when the multiline flag is present", () => {
311 assertStrictEquals(new Matcher(/(?:)/mu).multiline
, true);
314 it("[[Get]] returns false when the multiline flag is not present", () => {
315 assertStrictEquals(new Matcher(/(?:)/u).multiline
, false);
318 describe(".length", () => {
319 it("[[Get]] returns the correct length", () => {
321 Object
.getOwnPropertyDescriptor(
330 describe(".name", () => {
331 it("[[Get]] returns the correct name", () => {
333 Object
.getOwnPropertyDescriptor(
343 describe("::source", () => {
344 it("[[Get]] returns the RegExp source", () => {
345 assertStrictEquals(new Matcher("").source
, "(?:)");
346 assertStrictEquals(new Matcher(/.*/su).source
, ".*");
349 describe(".length", () => {
350 it("[[Get]] returns the correct length", () => {
352 Object
.getOwnPropertyDescriptor(
361 describe(".name", () => {
362 it("[[Get]] returns the correct name", () => {
364 Object
.getOwnPropertyDescriptor(
374 describe("::sticky", () => {
375 it("[[Get]] returns true when the sticky flag is present", () => {
376 assertStrictEquals(new Matcher(/(?:)/uy).sticky
, true);
379 it("[[Get]] returns false when the sticky flag is not present", () => {
380 assertStrictEquals(new Matcher(/(?:)/u).sticky
, false);
383 describe(".length", () => {
384 it("[[Get]] returns the correct length", () => {
386 Object
.getOwnPropertyDescriptor(
395 describe(".name", () => {
396 it("[[Get]] returns the correct name", () => {
398 Object
.getOwnPropertyDescriptor(
408 describe("::toString", () => {
409 it("[[Call]] returns the string source", () => {
410 assertStrictEquals(new Matcher(/(?:)/u).toString(), "/(?:)/u");
414 describe("::unicode", () => {
415 it("[[Get]] returns true when the unicode flag is present", () => {
416 assertStrictEquals(new Matcher(/(?:)/u).unicode
, true);
419 describe(".length", () => {
420 it("[[Get]] returns the correct length", () => {
422 Object
.getOwnPropertyDescriptor(
431 describe(".name", () => {
432 it("[[Get]] returns the correct name", () => {
434 Object
.getOwnPropertyDescriptor(
444 describe("~", () => {
445 it("[[Call]] returns true for a complete match", () => {
446 assertStrictEquals(new Matcher("")(""), true);
447 assertStrictEquals(new Matcher(/.*/su)("success\nyay"), true);
449 new Matcher(/.*/su, undefined, ($) => $ === "success")(
456 it("[[Call]] calls the constraint if the match succeeds", () => {
457 const constraint
= spy((_
) => true);
458 const matcher
= new Matcher("(.).*", undefined, constraint
);
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);
467 it("[[Call]] does not call the constraint if the match fails", () => {
468 const constraint
= spy((_
) => true);
469 const matcher
= new Matcher("", undefined, constraint
);
471 assertSpyCalls(constraint
, 0);
474 it("[[Call]] returns false for a partial match", () => {
475 assertStrictEquals(new Matcher("")("failure"), false);
476 assertStrictEquals(new Matcher(/.*/u)("failure\nno"), false);
479 it("[[Call]] returns false if the constraint fails", () => {
481 new Matcher(".*", undefined, () => false)(""),
487 describe("~lastIndex", () => {
488 it("[[Get]] returns zero", () => {
489 assertStrictEquals(new Matcher("").lastIndex
, 0);
492 it("[[Set]] fails", () => {
493 assertThrows(() => (new Matcher("").lastIndex
= 1));
497 describe("~length", () => {
498 it("[[Get]] returns one", () => {
499 assertStrictEquals(new Matcher("").length
, 1);
503 describe("~name", () => {
504 it("[[Get]] wraps the stringified regular expression if no name was provided", () => {
505 assertStrictEquals(new Matcher("").name
, "Matcher(/(?:)/u)");
507 new Matcher(/.*/gsu).name
,
512 it("[[Get]] uses the provided name if one was provided", () => {
513 assertStrictEquals(new Matcher("", "success").name
, "success");
518 describe("asciiLowercase", () => {
519 it("[[Call]] lowercases (just) A·S·C·I·I letters", () => {
520 assertStrictEquals(asciiLowercase("aBſÆss FtɁɂß"), "abſÆss ftɁɂß");
523 it("[[Construct]] throws an error", () => {
524 assertThrows(() => new asciiLowercase(""));
527 describe(".length", () => {
528 it("[[Get]] returns the correct length", () => {
529 assertStrictEquals(asciiLowercase
.length
, 1);
533 describe(".name", () => {
534 it("[[Get]] returns the correct name", () => {
535 assertStrictEquals(asciiLowercase
.name
, "asciiLowercase");
540 describe("asciiUppercase", () => {
541 it("[[Call]] uppercases (just) A·S·C·I·I letters", () => {
542 assertStrictEquals(asciiUppercase("aBſÆss FtɁɂß"), "ABſÆSS FTɁɂß");
545 it("[[Construct]] throws an error", () => {
546 assertThrows(() => new asciiUppercase(""));
549 describe(".length", () => {
550 it("[[Get]] returns the correct length", () => {
551 assertStrictEquals(asciiUppercase
.length
, 1);
555 describe(".name", () => {
556 it("[[Get]] returns the correct name", () => {
557 assertStrictEquals(asciiUppercase
.name
, "asciiUppercase");
562 describe("characters", () => {
563 it("[[Call]] returns an iterable", () => {
565 typeof characters("")[Symbol
.iterator
],
570 it("[[Call]] returns an iterator", () => {
571 assertStrictEquals(typeof characters("").next
, "function");
574 it("[[Call]] returns a string character iterator", () => {
576 characters("")[Symbol
.toStringTag
],
577 "String Character Iterator",
581 it("[[Call]] iterates over the characters", () => {
583 ...characters("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
597 it("[[Construct]] throws an error", () => {
598 assertThrows(() => new characters(""));
601 describe(".length", () => {
602 it("[[Get]] returns the correct length", () => {
603 assertStrictEquals(characters
.length
, 1);
607 describe(".name", () => {
608 it("[[Get]] returns the correct name", () => {
609 assertStrictEquals(characters
.name
, "characters");
614 describe("codeUnits", () => {
615 it("[[Call]] returns an iterable", () => {
617 typeof codeUnits("")[Symbol
.iterator
],
622 it("[[Call]] returns an iterator", () => {
623 assertStrictEquals(typeof codeUnits("").next
, "function");
626 it("[[Call]] returns a string code unit iterator", () => {
628 codeUnits("")[Symbol
.toStringTag
],
629 "String Code Unit Iterator",
633 it("[[Call]] iterates over the code units", () => {
635 ...codeUnits("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
651 it("[[Construct]] throws an error", () => {
652 assertThrows(() => new codeUnits(""));
655 describe(".length", () => {
656 it("[[Get]] returns the correct length", () => {
657 assertStrictEquals(codeUnits
.length
, 1);
661 describe(".name", () => {
662 it("[[Get]] returns the correct name", () => {
663 assertStrictEquals(codeUnits
.name
, "codeUnits");
668 describe("codepoints", () => {
669 it("[[Call]] returns an iterable", () => {
671 typeof codepoints("")[Symbol
.iterator
],
676 it("[[Call]] returns an iterator", () => {
677 assertStrictEquals(typeof codepoints("").next
, "function");
680 it("[[Call]] returns a string codepoint iterator", () => {
682 codepoints("")[Symbol
.toStringTag
],
683 "String Codepoint Iterator",
687 it("[[Call]] iterates over the codepoints", () => {
689 ...codepoints("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
703 it("[[Construct]] throws an error", () => {
704 assertThrows(() => new codepoints(""));
707 describe(".length", () => {
708 it("[[Get]] returns the correct length", () => {
709 assertStrictEquals(codepoints
.length
, 1);
713 describe(".name", () => {
714 it("[[Get]] returns the correct name", () => {
715 assertStrictEquals(codepoints
.name
, "codepoints");
720 describe("getCharacter", () => {
721 it("[[Call]] returns the character at the provided position", () => {
722 assertStrictEquals(getCharacter("Ii🎙🆗☺", 4), "🆗");
725 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
726 assertStrictEquals(getCharacter("Ii🎙🆗☺", 5), "\uDD97");
729 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
730 assertStrictEquals(getCharacter("Ii🎙🆗☺", -1), undefined);
731 assertStrictEquals(getCharacter("Ii🎙🆗☺", 7), undefined);
734 it("[[Construct]] throws an error", () => {
735 assertThrows(() => new getCharacter("a", 0));
738 describe(".length", () => {
739 it("[[Get]] returns the correct length", () => {
740 assertStrictEquals(getCharacter
.length
, 2);
744 describe(".name", () => {
745 it("[[Get]] returns the correct name", () => {
746 assertStrictEquals(getCharacter
.name
, "getCharacter");
751 describe("getCodeUnit", () => {
752 it("[[Call]] returns the code unit at the provided position", () => {
753 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 4), 0xD83C);
756 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
757 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 5), 0xDD97);
760 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
761 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", -1), undefined);
762 assertStrictEquals(getCodeUnit("Ii🎙🆗☺", 7), undefined);
765 it("[[Construct]] throws an error", () => {
766 assertThrows(() => new getCodeUnit("a", 0));
769 describe(".length", () => {
770 it("[[Get]] returns the correct length", () => {
771 assertStrictEquals(getCodeUnit
.length
, 2);
775 describe(".name", () => {
776 it("[[Get]] returns the correct name", () => {
777 assertStrictEquals(getCodeUnit
.name
, "getCodeUnit");
782 describe("getCodepoint", () => {
783 it("[[Call]] returns the character at the provided position", () => {
784 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 4), 0x1F197);
787 it("[[Call]] returns a low surrogate if the provided position splits a character", () => {
788 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 5), 0xDD97);
791 it("[[Call]] returns undefined for an out‐of‐bounds index", () => {
792 assertStrictEquals(getCodepoint("Ii🎙🆗☺", -1), undefined);
793 assertStrictEquals(getCodepoint("Ii🎙🆗☺", 7), undefined);
796 it("[[Construct]] throws an error", () => {
797 assertThrows(() => new getCodepoint("a", 0));
800 describe(".length", () => {
801 it("[[Get]] returns the correct length", () => {
802 assertStrictEquals(getCodepoint
.length
, 2);
806 describe(".name", () => {
807 it("[[Get]] returns the correct name", () => {
808 assertStrictEquals(getCodepoint
.name
, "getCodepoint");
813 describe("getFirstSubstringIndex", () => {
814 it("[[Call]] returns the index of the first match", () => {
815 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", "🆗"), 4);
818 it("[[Call]] returns −1 if no match is found", () => {
819 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", "🆗🆖"), -1);
822 it("[[Call]] returns 0 when provided with an empty string", () => {
823 assertStrictEquals(getFirstSubstringIndex("Ii🎙🆗☺🆗", ""), 0);
826 it("[[Construct]] throws an error", () => {
827 assertThrows(() => new getFirstSubstringIndex("", ""));
830 describe(".length", () => {
831 it("[[Get]] returns the correct length", () => {
832 assertStrictEquals(getFirstSubstringIndex
.length
, 2);
836 describe(".name", () => {
837 it("[[Get]] returns the correct name", () => {
839 getFirstSubstringIndex
.name
,
840 "getFirstSubstringIndex",
846 describe("getLastSubstringIndex", () => {
847 it("[[Call]] returns the index of the first match", () => {
848 assertStrictEquals(getLastSubstringIndex("Ii🎙🆗☺🆗", "🆗"), 7);
851 it("[[Call]] returns −1 if no match is found", () => {
852 assertStrictEquals(getLastSubstringIndex("Ii🎙🆗☺🆗", "🆖🆗"), -1);
855 it("[[Call]] returns the length when provided with an empty string", () => {
857 getLastSubstringIndex("Ii🎙🆗☺🆗", ""),
862 it("[[Construct]] throws an error", () => {
863 assertThrows(() => new getLastSubstringIndex("", ""));
866 describe(".length", () => {
867 it("[[Get]] returns the correct length", () => {
868 assertStrictEquals(getLastSubstringIndex
.length
, 2);
872 describe(".name", () => {
873 it("[[Get]] returns the correct name", () => {
875 getLastSubstringIndex
.name
,
876 "getLastSubstringIndex",
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");
887 it('[[Call]] uses "," if no separator is provided', () => {
888 assertStrictEquals(join([1, 2, 3, 4].values()), "1,2,3,4");
891 it("[[Call]] uses the empty sting for nullish values", () => {
893 join([null, , null, undefined].values(), "☂"),
898 it("[[Construct]] throws an error", () => {
899 assertThrows(() => new join([]));
902 describe(".length", () => {
903 it("[[Get]] returns the correct length", () => {
904 assertStrictEquals(join
.length
, 2);
908 describe(".name", () => {
909 it("[[Get]] returns the correct name", () => {
910 assertStrictEquals(join
.name
, "join");
915 describe("rawString", () => {
916 it("[[Call]] acts like String.raw", () => {
917 assertStrictEquals(rawString
`\nraw${" string"}`, "\\nraw string");
920 it("[[Construct]] throws an error", () => {
921 assertThrows(() => new rawString(["string"]));
924 describe(".length", () => {
925 it("[[Get]] returns the correct length", () => {
926 assertStrictEquals(rawString
.length
, 1);
930 describe(".name", () => {
931 it("[[Get]] returns the correct name", () => {
932 assertStrictEquals(rawString
.name
, "rawString");
937 describe("scalarValueString", () => {
938 it("[[Call]] replaces invalid values", () => {
940 scalarValueString("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
941 "Ii🎙\uFFFD\uFFFD\uFFFD\uFFFD🆗☺",
945 it("[[Construct]] throws an error", () => {
946 assertThrows(() => new scalarValueString(""));
949 describe(".length", () => {
950 it("[[Get]] returns the correct length", () => {
951 assertStrictEquals(scalarValueString
.length
, 1);
955 describe(".name", () => {
956 it("[[Get]] returns the correct name", () => {
957 assertStrictEquals(scalarValueString
.name
, "scalarValueString");
962 describe("scalarValues", () => {
963 it("[[Call]] returns an iterable", () => {
965 typeof scalarValues("")[Symbol
.iterator
],
970 it("[[Call]] returns an iterator", () => {
971 assertStrictEquals(typeof scalarValues("").next
, "function");
974 it("[[Call]] returns a string scalar value iterator", () => {
976 scalarValues("")[Symbol
.toStringTag
],
977 "String Scalar Value Iterator",
981 it("[[Call]] iterates over the scalar values", () => {
983 ...scalarValues("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"),
997 it("[[Construct]] throws an error", () => {
998 assertThrows(() => new scalarValues(""));
1001 describe(".length", () => {
1002 it("[[Get]] returns the correct length", () => {
1003 assertStrictEquals(scalarValues
.length
, 1);
1007 describe(".name", () => {
1008 it("[[Get]] returns the correct name", () => {
1009 assertStrictEquals(scalarValues
.name
, "scalarValues");
1014 describe("splitOnASCIIWhitespace", () => {
1015 it("[[Call]] splits on sequences of spaces", () => {
1017 splitOnASCIIWhitespace("🅰️ 🅱️ 🆎 🅾️"),
1018 ["🅰️", "🅱️", "🆎", "🅾️"],
1022 it("[[Call]] splits on sequences of tabs", () => {
1024 splitOnASCIIWhitespace("🅰️\t\t\t🅱️\t🆎\t\t🅾️"),
1025 ["🅰️", "🅱️", "🆎", "🅾️"],
1029 it("[[Call]] splits on sequences of carriage returns", () => {
1031 splitOnASCIIWhitespace("🅰️\r\r\r🅱️\r🆎\r\r🅾️"),
1032 ["🅰️", "🅱️", "🆎", "🅾️"],
1036 it("[[Call]] splits on sequences of newlines", () => {
1038 splitOnASCIIWhitespace("🅰️\r\r\r🅱️\r🆎\r\r🅾️"),
1039 ["🅰️", "🅱️", "🆎", "🅾️"],
1043 it("[[Call]] splits on sequences of form feeds", () => {
1045 splitOnASCIIWhitespace("🅰️\f\f\f🅱️\f🆎\f\f🅾️"),
1046 ["🅰️", "🅱️", "🆎", "🅾️"],
1050 it("[[Call]] splits on mixed whitespace", () => {
1052 splitOnASCIIWhitespace("🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️"),
1053 ["🅰️", "🅱️", "🆎", "🅾️"],
1057 it("[[Call]] returns an array of just the empty string for the empty string", () => {
1058 assertEquals(splitOnASCIIWhitespace(""), [""]);
1061 it("[[Call]] returns a single token if there are no spaces", () => {
1062 assertEquals(splitOnASCIIWhitespace("abcd"), ["abcd"]);
1065 it("[[Call]] does not split on other kinds of whitespace", () => {
1067 splitOnASCIIWhitespace("a\u202F\u205F\xa0\v\0\bb"),
1068 ["a\u202F\u205F\xa0\v\0\bb"],
1072 it("[[Call]] trims leading and trailing whitespace", () => {
1074 splitOnASCIIWhitespace(
1075 "\f\r\n\r\n \n\t🅰️\f \t\n🅱️\r🆎\n\f🅾️\n\f",
1077 ["🅰️", "🅱️", "🆎", "🅾️"],
1081 it("[[Construct]] throws an error", () => {
1082 assertThrows(() => new splitOnASCIIWhitespace(""));
1085 describe(".length", () => {
1086 it("[[Get]] returns the correct length", () => {
1087 assertStrictEquals(splitOnASCIIWhitespace
.length
, 1);
1091 describe(".name", () => {
1092 it("[[Get]] returns the correct name", () => {
1094 splitOnASCIIWhitespace
.name
,
1095 "splitOnASCIIWhitespace",
1101 describe("splitOnCommas", () => {
1102 it("[[Call]] splits on commas", () => {
1104 splitOnCommas("🅰️,🅱️,🆎,🅾️"),
1105 ["🅰️", "🅱️", "🆎", "🅾️"],
1109 it("[[Call]] returns an array of just the empty string for the empty string", () => {
1110 assertEquals(splitOnCommas(""), [""]);
1113 it("[[Call]] returns a single token if there are no commas", () => {
1114 assertEquals(splitOnCommas("abcd"), ["abcd"]);
1117 it("[[Call]] splits into empty strings if there are only commas", () => {
1118 assertEquals(splitOnCommas(",,,"), ["", "", "", ""]);
1121 it("[[Call]] trims leading and trailing whitespace", () => {
1123 splitOnCommas("\f\r\n\r\n \n\t🅰️,🅱️,🆎,🅾️\n\f"),
1124 ["🅰️", "🅱️", "🆎", "🅾️"],
1127 splitOnCommas("\f\r\n\r\n \n\t,,,\n\f"),
1132 it("[[Call]] removes whitespace from the split tokens", () => {
1135 "\f\r\n\r\n \n\t🅰️\f , \t\n🅱️,\r\n\r🆎\n\f,🅾️\n\f",
1137 ["🅰️", "🅱️", "🆎", "🅾️"],
1140 splitOnCommas("\f\r\n\r\n \n\t\f , \t\n,\r\n\r\n\f,\n\f"),
1145 it("[[Construct]] throws an error", () => {
1146 assertThrows(() => new splitOnCommas(""));
1149 describe(".length", () => {
1150 it("[[Get]] returns the correct length", () => {
1151 assertStrictEquals(splitOnCommas
.length
, 1);
1155 describe(".name", () => {
1156 it("[[Get]] returns the correct name", () => {
1157 assertStrictEquals(splitOnCommas
.name
, "splitOnCommas");
1162 describe("stringCatenate", () => {
1163 it("[[Call]] catenates the values", () => {
1164 assertStrictEquals(stringCatenate("the", " values"), "the values");
1167 it("[[Call]] returns an empty string when called with no values", () => {
1168 assertStrictEquals(stringCatenate(), "");
1171 it('[[Call]] uses "undefined" when explicitly provided undefined', () => {
1173 stringCatenate(undefined, undefined),
1174 "undefinedundefined",
1178 it('[[Call]] uses "null" when provided null', () => {
1179 assertStrictEquals(stringCatenate(null, null), "nullnull");
1182 it("[[Construct]] throws an error", () => {
1183 assertThrows(() => new stringCatenate());
1186 describe(".length", () => {
1187 it("[[Get]] returns the correct length", () => {
1188 assertStrictEquals(stringCatenate
.length
, 2);
1192 describe(".name", () => {
1193 it("[[Get]] returns the correct name", () => {
1194 assertStrictEquals(stringCatenate
.name
, "stringCatenate");
1199 describe("stringEndsWith", () => {
1200 it("[[Call]] returns whether the string ends with the thing", () => {
1202 stringEndsWith("very success", " success"),
1205 assertStrictEquals(stringEndsWith("very fail", " success"), false);
1208 it("[[Call]] accepts an offset", () => {
1210 stringEndsWith("very successful", " success", 12),
1215 it("[[Call]] returns true for an empty string test", () => {
1216 assertStrictEquals(stringEndsWith("", ""), true);
1219 it("[[Construct]] throws an error", () => {
1220 assertThrows(() => new stringEndsWith("", ""));
1223 describe(".length", () => {
1224 it("[[Get]] returns the correct length", () => {
1225 assertStrictEquals(stringEndsWith
.length
, 2);
1229 describe(".name", () => {
1230 it("[[Get]] returns the correct name", () => {
1231 assertStrictEquals(stringEndsWith
.name
, "stringEndsWith");
1236 describe("stringFromCodeUnits", () => {
1237 it("[[Call]] makes the string", () => {
1239 stringFromCodeUnits(0xD83C, 0xDD97),
1244 it("[[Call]] throws with non‐integral arguments", () => {
1245 assertThrows(() => stringFromCodeUnits(NaN
));
1246 assertThrows(() => stringFromCodeUnits(Infinity
));
1247 assertThrows(() => stringFromCodeUnits(0.1));
1250 it("[[Call]] throws with arguments out of range", () => {
1251 assertThrows(() => stringFromCodeUnits(-1));
1252 assertThrows(() => stringFromCodeUnits(0x10000));
1255 it("[[Construct]] throws an error", () => {
1256 assertThrows(() => new stringFromCodeUnits([]));
1259 describe(".length", () => {
1260 it("[[Get]] returns the correct length", () => {
1261 assertStrictEquals(stringFromCodeUnits
.length
, 1);
1265 describe(".name", () => {
1266 it("[[Get]] returns the correct name", () => {
1268 stringFromCodeUnits
.name
,
1269 "stringFromCodeUnits",
1275 describe("stringFromCodepoints", () => {
1276 it("[[Call]] makes the string", () => {
1277 assertStrictEquals(stringFromCodepoints(0x1F197), "🆗");
1280 it("[[Call]] throws with non‐integral arguments", () => {
1281 assertThrows(() => stringFromCodepoints(NaN
));
1282 assertThrows(() => stringFromCodepoints(Infinity
));
1283 assertThrows(() => stringFromCodepoints(0.1));
1286 it("[[Call]] throws with arguments out of range", () => {
1287 assertThrows(() => stringFromCodepoints(-1));
1288 assertThrows(() => stringFromCodepoints(0x110000));
1291 it("[[Construct]] throws an error", () => {
1292 assertThrows(() => new stringFromCodepoints([]));
1295 describe(".length", () => {
1296 it("[[Get]] returns the correct length", () => {
1297 assertStrictEquals(stringFromCodepoints
.length
, 1);
1301 describe(".name", () => {
1302 it("[[Get]] returns the correct name", () => {
1304 stringFromCodepoints
.name
,
1305 "stringFromCodepoints",
1311 describe("stringIncludes", () => {
1312 it("[[Call]] returns whether the string includes the thing", () => {
1314 stringIncludes("very success full", " success "),
1318 stringIncludes("very fail full", " success "),
1323 it("[[Call]] accepts an offset", () => {
1325 stringIncludes("maybe success full", " success ", 4),
1329 stringIncludes("maybe success full", " success ", 5),
1333 stringIncludes("maybe success full", " success ", 6),
1338 it("[[Call]] returns true for an empty string test", () => {
1339 assertStrictEquals(stringIncludes("", ""), true);
1342 it("[[Construct]] throws an error", () => {
1343 assertThrows(() => new stringIncludes("", ""));
1346 describe(".length", () => {
1347 it("[[Get]] returns the correct length", () => {
1348 assertStrictEquals(stringIncludes
.length
, 2);
1352 describe(".name", () => {
1353 it("[[Get]] returns the correct name", () => {
1354 assertStrictEquals(stringIncludes
.name
, "stringIncludes");
1359 describe("stringMatch", () => {
1360 it("[[Call]] does the match akin to String::match", () => {
1362 [...stringMatch("very success full", /([sc]+[ue]?)+/)],
1366 [...stringMatch("very success full", /([sc]+)[ue]?/g)],
1367 ["su", "cce", "ss"],
1371 it("[[Construct]] throws an error", () => {
1372 assertThrows(() => new stringMatch("", /(?:)/));
1375 describe(".length", () => {
1376 it("[[Get]] returns the correct length", () => {
1377 assertStrictEquals(stringMatch
.length
, 2);
1381 describe(".name", () => {
1382 it("[[Get]] returns the correct name", () => {
1383 assertStrictEquals(stringMatch
.name
, "stringMatch");
1388 describe("stringMatchAll", () => {
1389 it("[[Call]] does the match akin to String::matchAll", () => {
1391 [...stringMatchAll("very success full", /([sc]+)[ue]?/g)].map((
1394 [["su", "s"], ["cce", "cc"], ["ss", "ss"]],
1398 it("[[Construct]] throws an error", () => {
1399 assertThrows(() => new stringMatchAll("", /(?:)/g));
1402 describe(".length", () => {
1403 it("[[Get]] returns the correct length", () => {
1404 assertStrictEquals(stringMatchAll
.length
, 2);
1408 describe(".name", () => {
1409 it("[[Get]] returns the correct name", () => {
1410 assertStrictEquals(stringMatchAll
.name
, "stringMatchAll");
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");
1423 it("[[Call]] assumes NFC", () => {
1424 assertStrictEquals(stringNormalize("\u017F\u0307"), "\u1E9B");
1427 it("[[Call]] throws with an invalid form", () => {
1428 assertThrows(() => stringNormalize("", "NFB"));
1431 it("[[Construct]] throws an error", () => {
1432 assertThrows(() => new stringNormalize("", "NFC"));
1435 describe(".length", () => {
1436 it("[[Get]] returns the correct length", () => {
1437 assertStrictEquals(stringNormalize
.length
, 1);
1441 describe(".name", () => {
1442 it("[[Get]] returns the correct name", () => {
1443 assertStrictEquals(stringNormalize
.name
, "stringNormalize");
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");
1456 it("[[Construct]] throws an error", () => {
1457 assertThrows(() => new stringPadEnd("", 1));
1460 describe(".length", () => {
1461 it("[[Get]] returns the correct length", () => {
1462 assertStrictEquals(stringPadEnd
.length
, 2);
1466 describe(".name", () => {
1467 it("[[Get]] returns the correct name", () => {
1468 assertStrictEquals(stringPadEnd
.name
, "stringPadEnd");
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");
1481 it("[[Construct]] throws an error", () => {
1482 assertThrows(() => new stringPadStart("", 1));
1485 describe(".length", () => {
1486 it("[[Get]] returns the correct length", () => {
1487 assertStrictEquals(stringPadStart
.length
, 2);
1491 describe(".name", () => {
1492 it("[[Get]] returns the correct name", () => {
1493 assertStrictEquals(stringPadStart
.name
, "stringPadStart");
1498 describe("stringRepeat", () => {
1499 it("[[Call]] repeats the string", () => {
1500 assertStrictEquals(stringRepeat("xx", 3), "xxxxxx");
1501 assertStrictEquals(stringRepeat("", 3), "");
1502 assertStrictEquals(stringRepeat("xx", 0), "");
1505 it("[[Call]] throws for negative repititions", () => {
1506 assertThrows(() => stringRepeat("", -1));
1509 it("[[Call]] throws for infinite repititions", () => {
1510 assertThrows(() => stringRepeat("", Infinity
));
1513 it("[[Construct]] throws an error", () => {
1514 assertThrows(() => new stringRepeat("", 1));
1517 describe(".length", () => {
1518 it("[[Get]] returns the correct length", () => {
1519 assertStrictEquals(stringRepeat
.length
, 2);
1523 describe(".name", () => {
1524 it("[[Get]] returns the correct name", () => {
1525 assertStrictEquals(stringRepeat
.name
, "stringRepeat");
1530 describe("stringReplace", () => {
1531 it("[[Call]] does the replacement akin to String::replace", () => {
1533 stringReplace("it’s a failure", "failure", "success"),
1538 "very success full",
1546 "very success full",
1549 `${$s[0].length}`.repeat($s
[1].length
) +
1550 $s
[0].substring($s
[1].length
),
1552 "very 2u33e22 full",
1556 it("[[Construct]] throws an error", () => {
1557 assertThrows(() => new stringReplace("", /(?:)/, ""));
1560 describe(".length", () => {
1561 it("[[Get]] returns the correct length", () => {
1562 assertStrictEquals(stringReplace
.length
, 3);
1566 describe(".name", () => {
1567 it("[[Get]] returns the correct name", () => {
1568 assertStrictEquals(stringReplace
.name
, "stringReplace");
1573 describe("stringReplaceAll", () => {
1574 it("[[Call]] does the match akin to String::replaceAll", () => {
1576 stringReplaceAll("it’s a failure failure", "failure", "success"),
1577 "it’s a success success",
1581 "very success full",
1584 `${$s[0].length}`.repeat($s
[1].length
) +
1585 $s
[0].substring($s
[1].length
),
1587 "very 2u33e22 full",
1591 it("[[Construct]] throws an error", () => {
1592 assertThrows(() => new stringReplaceAll("", /(?:)/g));
1595 describe(".length", () => {
1596 it("[[Get]] returns the correct length", () => {
1597 assertStrictEquals(stringReplaceAll
.length
, 3);
1601 describe(".name", () => {
1602 it("[[Get]] returns the correct name", () => {
1603 assertStrictEquals(stringReplaceAll
.name
, "stringReplaceAll");
1608 describe("stringSearch", () => {
1609 it("[[Call]] does the search akin to String::search", () => {
1611 stringSearch("very success full", /([sc]+)[ue]?/),
1615 stringSearch("very fail full", /([sc]+)[ue]?/),
1620 it("[[Construct]] throws an error", () => {
1621 assertThrows(() => new stringSearch("", /(?:)/));
1624 describe(".length", () => {
1625 it("[[Get]] returns the correct length", () => {
1626 assertStrictEquals(stringSearch
.length
, 2);
1630 describe(".name", () => {
1631 it("[[Get]] returns the correct name", () => {
1632 assertStrictEquals(stringSearch
.name
, "stringSearch");
1637 describe("stringSlice", () => {
1638 it("[[Call]] slices the string akin to String::search", () => {
1640 stringSlice("very success full", 5, 12),
1644 stringSlice("very success full", -12, -5),
1649 it("[[Construct]] throws an error", () => {
1650 assertThrows(() => new stringSlice("", 0, 0));
1653 describe(".length", () => {
1654 it("[[Get]] returns the correct length", () => {
1655 assertStrictEquals(stringSlice
.length
, 3);
1659 describe(".name", () => {
1660 it("[[Get]] returns the correct name", () => {
1661 assertStrictEquals(stringSlice
.name
, "stringSlice");
1666 describe("stringSplit", () => {
1667 it("[[Call]] splits the string akin to String::split", () => {
1668 assertEquals(stringSplit("success", ""), [
1677 assertEquals(stringSplit("success", /(?<=[aeiou])(?=[^aeiou])/), [
1682 assertEquals(stringSplit("success", "failure"), ["success"]);
1685 it("[[Call]] recognizes a limit", () => {
1686 assertEquals(stringSplit("success", "", 4), ["s", "u", "c", "c"]);
1689 it("[[Construct]] throws an error", () => {
1690 assertThrows(() => new stringSplit("", ""));
1693 describe(".length", () => {
1694 it("[[Get]] returns the correct length", () => {
1695 assertStrictEquals(stringSplit
.length
, 3);
1699 describe(".name", () => {
1700 it("[[Get]] returns the correct name", () => {
1701 assertStrictEquals(stringSplit
.name
, "stringSplit");
1706 describe("stringStartsWith", () => {
1707 it("[[Call]] returns whether the string starts with the thing", () => {
1709 stringStartsWith("success is had", "success "),
1713 stringStartsWith("no success is had", "success "),
1718 it("[[Call]] accepts an offset", () => {
1720 stringStartsWith("much success is had", "success ", 5),
1725 it("[[Call]] returns true for an empty string test", () => {
1726 assertStrictEquals(stringEndsWith("", ""), true);
1729 it("[[Construct]] throws an error", () => {
1730 assertThrows(() => new stringStartsWith("", ""));
1733 describe(".length", () => {
1734 it("[[Get]] returns the correct length", () => {
1735 assertStrictEquals(stringStartsWith
.length
, 2);
1739 describe(".name", () => {
1740 it("[[Get]] returns the correct name", () => {
1741 assertStrictEquals(stringStartsWith
.name
, "stringStartsWith");
1746 describe("stringStartsWith", () => {
1747 it("[[Call]] returns the string value of a string literal", () => {
1748 assertStrictEquals(stringValue("success"), "success");
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" },
1757 assertStrictEquals(stringValue(string
), "success");
1760 it("[[Call]] throws for non‐strings", () => {
1761 assertThrows(() => stringValue(Object
.create(String
.prototype)));
1764 it("[[Construct]] throws an error", () => {
1765 assertThrows(() => new stringValue(""));
1768 describe(".length", () => {
1769 it("[[Get]] returns the correct length", () => {
1770 assertStrictEquals(stringValue
.length
, 1);
1774 describe(".name", () => {
1775 it("[[Get]] returns the correct name", () => {
1776 assertStrictEquals(stringValue
.name
, "stringValue");
1781 describe("stripAndCollapseASCIIWhitespace", () => {
1782 it("[[Call]] collapses mixed inner whitespace", () => {
1784 stripAndCollapseASCIIWhitespace("🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️"),
1789 it("[[Call]] trims leading and trailing whitespace", () => {
1791 stripAndCollapseASCIIWhitespace(
1792 "\f\r\n\r\n \n\t\f 🅰️\f \t\n🅱️\r\n\r🆎\n\f🅾️\n\f",
1798 it("[[Call]] returns the empty string for strings of whitespace", () => {
1800 stripAndCollapseASCIIWhitespace("\f\r\n\r\n \n\t\f \n\f"),
1805 it("[[Call]] does not collapse other kinds of whitespace", () => {
1807 stripAndCollapseASCIIWhitespace("a\u202F\u205F\xa0\v\0\bb"),
1808 "a\u202F\u205F\xa0\v\0\bb",
1812 it("[[Construct]] throws an error", () => {
1813 assertThrows(() => new stripAndCollapseASCIIWhitespace(""));
1816 describe(".length", () => {
1817 it("[[Get]] returns the correct length", () => {
1818 assertStrictEquals(stripAndCollapseASCIIWhitespace
.length
, 1);
1822 describe(".name", () => {
1823 it("[[Get]] returns the correct name", () => {
1825 stripAndCollapseASCIIWhitespace
.name
,
1826 "stripAndCollapseASCIIWhitespace",
1832 describe("stripLeadingAndTrailingASCIIWhitespace", () => {
1833 it("[[Call]] trims leading and trailing whitespace", () => {
1835 stripLeadingAndTrailingASCIIWhitespace(
1836 "\f\r\n\r\n \n\t\f 🅰️🅱️🆎🅾️\n\f",
1842 it("[[Call]] returns the empty string for strings of whitespace", () => {
1844 stripLeadingAndTrailingASCIIWhitespace("\f\r\n\r\n \n\t\f \n\f"),
1849 it("[[Call]] does not trim other kinds of whitespace", () => {
1851 stripLeadingAndTrailingASCIIWhitespace(
1852 "\v\u202F\u205Fx\0\b\xa0",
1854 "\v\u202F\u205Fx\0\b\xa0",
1858 it("[[Call]] does not adjust inner whitespace", () => {
1860 stripLeadingAndTrailingASCIIWhitespace("a b"),
1865 it("[[Construct]] throws an error", () => {
1866 assertThrows(() => new stripLeadingAndTrailingASCIIWhitespace(""));
1869 describe(".length", () => {
1870 it("[[Get]] returns the correct length", () => {
1872 stripLeadingAndTrailingASCIIWhitespace
.length
,
1878 describe(".name", () => {
1879 it("[[Get]] returns the correct name", () => {
1881 stripLeadingAndTrailingASCIIWhitespace
.name
,
1882 "stripLeadingAndTrailingASCIIWhitespace",
1888 describe("substring", () => {
1889 it("[[Call]] returns the substring", () => {
1891 substring("success", 0),
1895 substring("very success full", 5, 12),
1900 it("[[Construct]] throws an error", () => {
1901 assertThrows(() => new substring("", 0));
1904 describe(".length", () => {
1905 it("[[Get]] returns the correct length", () => {
1906 assertStrictEquals(substring
.length
, 3);
1910 describe(".name", () => {
1911 it("[[Get]] returns the correct name", () => {
1912 assertStrictEquals(substring
.name
, "substring");
1917 describe("toString", () => {
1918 it("[[Call]] converts to a string", () => {
1929 it("[[Call]] throws when provided a symbol", () => {
1930 assertThrows(() => toString(Symbol()));
1933 it("[[Construct]] throws an error", () => {
1934 assertThrows(() => new toString(""));
1937 describe(".length", () => {
1938 it("[[Get]] returns the correct length", () => {
1939 assertStrictEquals(toString
.length
, 1);
1943 describe(".name", () => {
1944 it("[[Get]] returns the correct name", () => {
1945 assertStrictEquals(toString
.name
, "toString");