UNPKG

@manuth/woltlab-compiler

Version:

A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components

123 lines 2.7 kB
/** * Represents a collection which is bidirectional. * * @template TParent * The type of the parent of the collection. * * @template TChild * The type of the children of the collection. */ export class BidirectionalCollection extends Array { /** * Initializes a new instance of the {@link BidirectionalCollection `BidirectionalCollection<TParent, TChild>`} class. * * @param owner * The owner of the collection. */ constructor(owner) { super(); this.owner = owner; } /** * Gets the owner of the collection. */ get Owner() { return this.owner; } /** * @inheritdoc * * @param items * The new elements of the Array. * * @returns * The new length of the array. */ push(...items) { for (let item of items) { this.Add(item); } return this.length; } /** * @inheritdoc * * @returns * The removed item. */ pop() { let index = this.length - 1; let result = this[index]; return this.Remove(index) ? result : undefined; } /** * @inheritdoc * * @returns * The removed element. */ shift() { let index = 0; let result = this[index]; return this.Remove(index) ? result : undefined; } /** * @inheritdoc * * @param items * Elements to insert at the start of the array. * * @returns * The new length of the array. */ unshift(...items) { for (let item of items) { this.Add(item); } return this.length; } /** * Securely adds an item. * * @param item * The item to add. * * @returns * A value indicating whether the item could be added. */ Add(item) { if (this.GetParent(item) !== this.Owner) { super.push(item); if (item) { this.SetParent(item, this.Owner); } return true; } else { return false; } } /** * Securely removes an item. * * @param index * The index of the item to remove. * * @returns * A value indicating whether the element could be removed. */ Remove(index) { let item = this[index]; if (this.GetParent(item) === this.Owner) { super.splice(index, 1); if (item) { this.SetParent(item, null); } return true; } else { return false; } } } //# sourceMappingURL=BidirectionalCollection.js.map