From: Lady Date: Sat, 11 Nov 2023 03:12:06 +0000 (-0500) Subject: Rename scalarValueString → toScalarValueString X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/5255537dba0f1fceaf0bc50c93ae5c4e6a2af66b?ds=sidebyside;hp=5f5b986d80f54cca184a03a7f5ddb17a19abacbf Rename scalarValueString → toScalarValueString Also update the implementation to use the new `String::toWellFormed` method. --- diff --git a/string.js b/string.js index 774261d..0c603ac 100644 --- a/string.js +++ b/string.js @@ -21,11 +21,8 @@ import { import { defineOwnProperties, getOwnPropertyDescriptors, - getPrototype, - objectCreate, setPrototype, } from "./object.js"; -import { ITERATOR } from "./value.js"; const RE = RegExp; const { prototype: rePrototype } = RE; @@ -330,13 +327,6 @@ export const { * with U+FFFD. */ scalarValues, - - /** - * Returns the result of converting the provided value to a string of - * scalar values by replacing (unpaired) surrogate values with - * U+FFFD. - */ - scalarValueString, } = (() => { const generateCharacters = function* (character) { yield character; @@ -368,31 +358,12 @@ export const { bind(generateCodepoints, { allowSurrogates: false }, []), "String Scalar Value Iterator", ); - const { - next: scalarValuesNext, - } = getPrototype(scalarValuesIterator("")); - const scalarValueIterablePrototype = { - [ITERATOR]() { - return { - next: bind( - scalarValuesNext, - scalarValuesIterator(this.source), - [], - ), - }; - }, - }; return { characters: ($) => charactersIterator(`${$}`), codeUnits: ($) => codeUnitsIterator(`${$}`), codepoints: ($) => codepointsIterator(`${$}`), scalarValues: ($) => scalarValuesIterator(`${$}`), - scalarValueString: ($) => - stringFromCodepoints(...objectCreate( - scalarValueIterablePrototype, - { source: { value: `${$}` } }, - )), }; })(); @@ -760,6 +731,16 @@ export const substring = createCallableFunction( stringPrototype.substring, ); +/** + * Returns the result of converting the provided value to a string of + * scalar values by replacing (unpaired) surrogate values with + * U+FFFD. + */ +export const toScalarValueString = createCallableFunction( + String.prototype.toWellFormed, + { name: "toScalarValueString" }, +); + /** * Returns the result of converting the provided value to a string. * diff --git a/string.test.js b/string.test.js index 4f257f9..e98a040 100644 --- a/string.test.js +++ b/string.test.js @@ -32,7 +32,6 @@ import { Matcher, rawString, scalarValues, - scalarValueString, splitOnASCIIWhitespace, splitOnCommas, stringCatenate, @@ -56,6 +55,7 @@ import { stripAndCollapseASCIIWhitespace, stripLeadingAndTrailingASCIIWhitespace, substring, + toScalarValueString, toString, } from "./string.js"; @@ -934,31 +934,6 @@ describe("rawString", () => { }); }); -describe("scalarValueString", () => { - it("[[Call]] replaces invalid values", () => { - assertStrictEquals( - scalarValueString("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"), - "Ii🎙\uFFFD\uFFFD\uFFFD\uFFFD🆗☺", - ); - }); - - it("[[Construct]] throws an error", () => { - assertThrows(() => new scalarValueString("")); - }); - - describe(".length", () => { - it("[[Get]] returns the correct length", () => { - assertStrictEquals(scalarValueString.length, 1); - }); - }); - - describe(".name", () => { - it("[[Get]] returns the correct name", () => { - assertStrictEquals(scalarValueString.name, "scalarValueString"); - }); - }); -}); - describe("scalarValues", () => { it("[[Call]] returns an iterable", () => { assertStrictEquals( @@ -1914,6 +1889,34 @@ describe("substring", () => { }); }); +describe("toScalarValueString", () => { + it("[[Call]] replaces invalid values", () => { + assertStrictEquals( + toScalarValueString("Ii🎙\uDFFF\uDD96\uD83C\uD800🆗☺"), + "Ii🎙\uFFFD\uFFFD\uFFFD\uFFFD🆗☺", + ); + }); + + it("[[Construct]] throws an error", () => { + assertThrows(() => new toScalarValueString("")); + }); + + describe(".length", () => { + it("[[Get]] returns the correct length", () => { + assertStrictEquals(toScalarValueString.length, 1); + }); + }); + + describe(".name", () => { + it("[[Get]] returns the correct name", () => { + assertStrictEquals( + toScalarValueString.name, + "toScalarValueString", + ); + }); + }); +}); + describe("toString", () => { it("[[Call]] converts to a string", () => { assertStrictEquals(