import type {JsonArray, JsonValue} from '@croct/json'; import type {JsonValueNode} from './valueNode'; import {JsonStructureNode, StructureDelimiter} from './structureNode'; import type {JsonCompositeDefinition, JsonCompositeNode, PartialJsonCompositeDefinition} from './compositeNode'; import {JsonValueFactory} from './factory'; import {JsonError} from '../error'; export interface JsonArrayDefinition extends JsonCompositeDefinition { readonly elements: readonly JsonValueNode[]; } export class JsonArrayNode extends JsonStructureNode implements JsonArrayDefinition { private readonly elementNodes: JsonValueNode[]; public constructor(definition: PartialJsonCompositeDefinition) { super(definition); this.elementNodes = [...definition.elements]; } public static of(...elements: ReadonlyArray): JsonArrayNode { return new JsonArrayNode({elements: elements.map(JsonValueFactory.create)}); } public update(other: JsonValueNode | JsonValue): JsonValueNode { if (!(other instanceof JsonArrayNode) && !Array.isArray(other)) { return JsonValueFactory.create(other); } const otherElements = other instanceof JsonArrayNode ? other.elements : other; const elements = this.elementNodes.splice(0); for (let index = 0; index < otherElements.length; index++) { this.push( index < elements.length ? elements[index].update(otherElements[index]) : otherElements[index], ); } if (otherElements.length < elements.length) { this.splice(otherElements.length, elements.length - otherElements.length); } return this; } protected getList(): JsonCompositeNode[] { return [...this.elementNodes]; } protected getDelimiter(): StructureDelimiter { return StructureDelimiter.ARRAY; } protected getMaxDepth(): number { return 1; } public get elements(): readonly JsonValueNode[] { return [...this.elementNodes]; } public get(index: number): JsonValueNode; public get(index: number, type: new (...args: any[]) => T): T; public get(index: number, type?: new (...args: any[]) => T): JsonValueNode { const element = this.elementNodes[index]; if (type !== undefined && !(element instanceof type)) { throw new JsonError(`Expected ${type.name} at index ${index}, but got ${element.constructor.name}.`); } return element; } public set(index: number, element: JsonValue | JsonValueNode): void { if (index < 0 || index >= this.elementNodes.length) { throw new Error(`Index ${index} is out of bounds.`); } this.elementNodes[index] = JsonValueFactory.create(element); } public clear(): void { this.elementNodes.length = 0; } public delete(index: number): void { if (index < 0 || index >= this.elementNodes.length) { throw new Error(`Index ${index} is out of bounds.`); } this.splice(index, 1); } public unshift(...elements: Array): void { this.elementNodes.unshift(...elements.map(JsonValueFactory.create)); } public push(...elements: Array): void { this.elementNodes.push(...elements.map(JsonValueFactory.create)); } public shift(): JsonValueNode | undefined { return this.elementNodes.shift(); } public pop(): JsonValueNode | undefined { return this.elementNodes.pop(); } public splice(start: number, deleteCount: number, ...elements: Array): JsonValueNode[] { return this.elementNodes.splice(start, deleteCount, ...elements.map(JsonValueFactory.create)); } public clone(): JsonArrayNode { const clones: Map = new Map(); for (const element of this.elementNodes) { clones.set(element, element.clone()); } return new JsonArrayNode({ elements: [...clones.values()], children: this.children.map(child => clones.get(child as JsonValueNode) ?? child.clone()), location: this.location, }); } public isEquivalent(other: JsonValueNode): boolean { if (!(other instanceof JsonArrayNode)) { return false; } if (this.elements.length !== other.elements.length) { return false; } return this.elements.every((element, index) => other.elements[index].isEquivalent(element)); } public toJSON(): JsonArray { return this.elements.map(element => element.toJSON()); } } JsonValueFactory.register( 'array', elements => new JsonArrayNode({ elements: elements.map(JsonValueFactory.create), }), );