UNPKG

woltlab-compiler

Version:

A compiler for WoltLab-Package Archives and WoltLab-Package Components

100 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); /** * Represents a collection which is bidirectional. */ class BidirectionalCollection extends Array { /** * Initializes a new instance of the `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 */ push(...items) { let result = 0; for (let item of items) { if (this.Add(item)) { result++; } } return result; } /** * @inheritdoc */ pop() { let result = this[this.length - 1]; return this.Remove(result) ? result : undefined; } /** * @inheritdoc */ shift() { let result = this[0]; return this.Remove(result) ? result : undefined; } /** * @inheritdoc */ unshift(...items) { let result = 0; for (let i = items.length - 1; i >= 0; i--) { if (this.Add(items[i])) { result++; } } return result; } /** * Securely adds an item. * * @param item * The item to add. */ Add(item) { if (this.GetParent(item) !== this.Owner) { super.push(item); if (!util_1.isNullOrUndefined(item)) { this.SetParent(item, this.Owner); } return true; } else { return false; } } /** * Securely removes an item. * * @param item * The item to remove. */ Remove(item) { if (this.GetParent(item) === this.Owner) { super.splice(this.indexOf(item), 1); if (!util_1.isNullOrUndefined(item)) { this.SetParent(item, null); } return true; } else { return false; } } } exports.BidirectionalCollection = BidirectionalCollection; //# sourceMappingURL=BidirectionalCollection.js.map