* An iterator object for iterating over code values (either code
* units or codepoints) in a string.
*
- * ※ This constructor is not exposed.
+ * ※ This class is not exposed, although its methods are (through
+ * the prototypes of string code value iterator objects).
*/
const StringCodeValueIterator = class extends identity {
#allowSurrogates;
return {
codeUnits: ($) =>
- new StringCodeValueIterator(call(arrayIterator, $, [])),
+ new StringCodeValueIterator(call(arrayIterator, `${$}`, [])),
codepoints: ($) =>
new StringCodeValueIterator(
- call(stringIterator, $, []),
+ call(stringIterator, `${$}`, []),
true,
),
scalarValues: ($) =>
new StringCodeValueIterator(
- call(stringIterator, $, []),
+ call(stringIterator, `${$}`, []),
false,
),
scalarValueString: ($) =>
stringFromCodepoints(...objectCreate(
scalarValueIterablePrototype,
- { source: { value: $ } },
+ { source: { value: `${$}` } },
)),
};
})();
* value according to the algorithm of String::substring.
*/
export const substring = makeCallable(String.prototype.substring);
+
+/**
+ * Returns the result of converting the provided value to a string.
+ *
+ * ☡ This method throws for symbols and other objects without a string
+ * representation.
+ */
+export const toString = ($) => `${$}`;
splitOnCommas,
stripAndCollapseASCIIWhitespace,
stripLeadingAndTrailingASCIIWhitespace,
+ toString,
} from "./string.js";
describe("Matcher", () => {
);
});
});
+
+describe("toString", () => {
+ it("[[Call]] converts to a string", () => {
+ assertStrictEquals(
+ toString({
+ toString() {
+ return "success";
+ },
+ }),
+ "success",
+ );
+ });
+
+ it("[[Call]] throws when provided a symbol", () => {
+ assertThrows(() => toString(Symbol()));
+ });
+});