X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/bf3fe705a9d5f717b3c1794a12726e926ece7ecc..2e6fac40384b2e17dd3a6b8700abc787b0b57479:/iterable.js diff --git a/iterable.js b/iterable.js index d89e35f..6b45000 100644 --- a/iterable.js +++ b/iterable.js @@ -1,19 +1,24 @@ -// ♓🌟 Piscēs ∷ iterable.js -// ==================================================================== -// -// Copyright © 2023 Lady [@ Lady’s Computer]. -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at . +// SPDX-FileCopyrightText: 2023, 2025 Lady +// SPDX-License-Identifier: MPL-2.0 +/** + * ⁌ ♓🧩 Piscēs ∷ iterable.js + * + * Copyright © 2023, 2025 Lady [@ Ladys Computer]. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at . + */ import { bind, call, identity } from "./function.js"; import { + defineOwnDataProperty, defineOwnProperty, getPrototype, objectCreate, + setPropertyValues, } from "./object.js"; -import { ITERATOR, TO_STRING_TAG } from "./value.js"; +import { ITERATOR, TO_STRING_TAG, UNDEFINED } from "./value.js"; export const { /** @@ -28,7 +33,7 @@ export const { * the resulting iterator. * * The resulting function also takes a second argument, which will be - * used as the `this` value when calling the provided generator + * used as the this value when calling the provided generator * function, if provided. * * ※ The returned function is an ordinary nonconstructible (arrow) @@ -51,7 +56,7 @@ export const { * the resulting iterator. * * The resulting function also takes a second argument, which will be - * used as the `this` value when calling the provided generator + * used as the this value when calling the provided generator * function, if provided. * * ※ The returned function is an ordinary nonconstructible (arrow) @@ -80,7 +85,7 @@ export const { * the resulting iterator. * * The resulting function also takes a second argument, which will be - * used as the `this` value when calling the provided generator + * used as the this value when calling the provided generator * function, if provided. * * ※ The returned function is an ordinary nonconstructible (arrow) @@ -103,7 +108,7 @@ export const { * the resulting iterator. * * The resulting function also takes a second argument, which will be - * used as the `this` value when calling the provided generator + * used as the this value when calling the provided generator * function, if provided. * * ※ The returned function is an ordinary nonconstructible (arrow) @@ -126,11 +131,11 @@ export const { * the resulting iterator. * * The resulting function also takes a second argument, which will be - * used as the `this` value when calling the provided generator + * used as the this value when calling the provided generator * function, if provided. * * ※ This iterator function iterates over characters; use - * `arrayIteratorFunction` to iterate over code units. + * arrayIteratorFunction to iterate over code units. * * ※ The returned function is an ordinary nonconstructible (arrow) * function which, when called with a string, returns an iterator. @@ -172,7 +177,7 @@ export const { * An iterator generated by an iterator function. * * This class provides the internal data structure of all the - * iterator functions as well as the `::next` behaviour they all use. + * iterator functions as well as the `::next´ behaviour they all use. * * ※ This class extends the identity function to allow for arbitrary * construction of its superclass instance based on the provided @@ -195,7 +200,7 @@ export const { * * ☡ It is not possible to type·check the provided next method or * generator function to ensure that they actually are correct and - * appropriately callable; if they aren’t, an error will be thrown + * appropriately callable; if they aren¦t, an error will be thrown * when attempting to yield the first value. */ constructor( @@ -218,8 +223,8 @@ export const { const baseIteratorNext = this.#baseIteratorNext; const generateNext = this.#generateNext; while (true) { - // This function sometimes needs to repeat its processing steps - // in the case that a generator function was provided. + // This function some·times needs to repeat its processing + // steps in the case that a generator function was provided. // // To avoid potentially large amounts of recursive calls, it is // defined in a loop which will exit the first time a suitable @@ -228,10 +233,10 @@ export const { const nextIterator = this.#nextIterator; if (baseIterator === null) { // The base iterator has already been exhausted. - return { value: undefined, done: true }; + return { value: UNDEFINED, done: true }; } else if (nextIterator === null) { // This iterator is not currently yielding values from the - // provided generator function, either because it doesn’t + // provided generator function, either because it doesn¦t // exist or because its values have been exhausted. // // Get the next value in the base iterator and either provide @@ -281,7 +286,9 @@ export const { continue; } else { // The current iterator of values has yielded another - // value; reyield it. + // value. + // + // Reyield it. return { value, done: false }; } } @@ -296,20 +303,20 @@ export const { objectCreate( iteratorPrototype, { - next: { + next: setPropertyValues(objectCreate(null), { configurable: true, enumerable: false, value: function next() { return call(iteratorNext, this, []); }, writable: true, - }, - [TO_STRING_TAG]: { + }), + [TO_STRING_TAG]: setPropertyValues(objectCreate(null), { configurable: true, enumerable: false, value: stringTag, writable: false, - }, + }), }, ); @@ -326,69 +333,101 @@ export const { const iteratorFunction = ( makeBaseIterator, baseIteratorNext, - generateNext = null, + iteratorFunctionName, + generateNext, stringTag = "Iterator", ) => { const prototype = makePrototype(stringTag); // intentionally cached - return ($, thisArg = undefined) => - new Iterator( - prototype, - call(makeBaseIterator, $, []), - baseIteratorNext, - generateNext === null ? null : bind(generateNext, thisArg, []), - ); + return defineOwnProperty( + ($, thisArg = UNDEFINED) => + new Iterator( + prototype, + call(makeBaseIterator, $, []), + baseIteratorNext, + generateNext == null + ? null + : bind(generateNext, thisArg, []), + ), + "name", + defineOwnDataProperty( + objectCreate(null), + "value", + iteratorFunctionName, + ), + ); }; return { arrayIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, - [arrayIterator, arrayIteratorNext], + UNDEFINED, + [arrayIterator, arrayIteratorNext, "values"], ), "name", - { value: "arrayIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "arrayIteratorFunction", + ), ), generatorIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, + UNDEFINED, [ function () { return this(); }, generatorIteratorNext, + "yields", ], ), "name", - { value: "generatorIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "generatorIteratorFunction", + ), ), mapIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, - [mapIterator, mapIteratorNext], + UNDEFINED, + [mapIterator, mapIteratorNext, "entries"], ), "name", - { value: "mapIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "mapIteratorFunction", + ), ), setIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, - [setIterator, setIteratorNext], + UNDEFINED, + [setIterator, setIteratorNext, "values"], ), "name", - { value: "setIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "setIteratorFunction", + ), ), stringIteratorFunction: defineOwnProperty( bind( iteratorFunction, - undefined, - [stringIterator, stringIteratorNext], + UNDEFINED, + [stringIterator, stringIteratorNext, "characters"], ), "name", - { value: "stringIteratorFunction" }, + defineOwnDataProperty( + objectCreate(null), + "value", + "stringIteratorFunction", + ), ), }; })();