UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

125 lines (109 loc) 2.61 kB
/** * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved. * Node module: @schukai/monster * * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3). * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html * * For those who do not wish to adhere to the AGPLv3, a commercial license is available. * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms. * For more information about purchasing a commercial license, please contact Volker Schukai. * * SPDX-License-Identifier: AGPL-3.0 */ import { isArray, isInstance } from "./is.mjs"; import { Node } from "./node.mjs"; import { validateInstance } from "./validate.mjs"; import { instanceSymbol } from "../constants.mjs"; export { NodeList }; /** * You can create the instance via the monster namespace `new Monster.Types.NodeList()`. * * @license AGPLv3 * @since 1.26.0 * @copyright Volker Schukai * @summary A NodeList class */ class NodeList extends Set { /** * @throws {Error} invalid value type * @param {NodeList|Node|Array<Node>}values */ constructor(values) { super(); if (values === undefined) return; if (isArray(values)) { values.forEach((value) => this.add(value)); } else if (isInstance(values, NodeList)) { values.forEach((value) => this.add(value)); } else if (isInstance(values, Node)) { this.add(values); } else { throw new Error("invalid value type"); } } /** * This method is called by the `instanceof` operator. * @return {symbol} * @since 2.1.0 */ static get [instanceSymbol]() { return Symbol.for("@schukai/monster/types/node-list"); } /** * * @param {Node} node * @return {Monster.Types.NodeList} */ add(node) { super.add(validateInstance(node, Node)); return this; } /** * @param {Node} node * @return {NodeList} */ remove(node) { super.delete(validateInstance(node, Node)); return this; } /** * @param {Node} node * @return {boolean} */ has(node) { return super.has(validateInstance(node, Node)); } /** * @return {NodeList} */ clear() { super.clear(); return this; } /** * @return {NodeList} */ toArray() { return Array.from(this); } /** * @return {NodeList} */ toJSON() { return this.toArray(); } /** * @return {NodeList} */ toString() { const parts = []; for (const node of this.toArray()) { parts.push(node.toString()); } return parts.join("\n"); } get length() { return super.size; } }