diff --git a/src/players/array.ts b/src/players/array.ts index ddb25ae..dbfbdc5 100644 --- a/src/players/array.ts +++ b/src/players/array.ts @@ -1,7 +1,7 @@ -import IndexOverload from '../util/index-overload.js'; +import DataStructure from '../util/data-structure.js'; import Player from './player.js'; -export default class PlayerArray extends IndexOverload { +export default class PlayerArray extends DataStructure { constructor() { super(); } @@ -10,15 +10,22 @@ export default class PlayerArray extends IndexOverload { * @param idx Player ID */ public get(pid: number): Player { - console.log('Player Array indexed by ' + pid) - return new Player() + console.log('Player Array indexed by ' + pid); + return new Player(); } - /* <- Not a description + /* * The PlayerArray does not provide setters, hence * this method is not to be implemented. */ protected set(idx: number, value: Player): Player { throw new Error('Method not implemented.'); } + + /** + * + */ + public override *iter() { + yield this[0]; + } } diff --git a/src/util/index-overload.ts b/src/util/data-structure.ts similarity index 54% rename from src/util/index-overload.ts rename to src/util/data-structure.ts index e6a7292..9bc24d2 100644 --- a/src/util/index-overload.ts +++ b/src/util/data-structure.ts @@ -1,3 +1,5 @@ +import util from 'util'; + export default abstract class IndexOverload { [idx: number]: T; @@ -15,7 +17,7 @@ export default abstract class IndexOverload { * console.log(indexable[0]) * console.log(indexable.get(0)) // Is the same * ``` - * + * * To expose any of the methods, the visibility * needs to be set to `public` from `protected`, * or can be kept hidden from outside access. @@ -40,9 +42,9 @@ export default abstract class IndexOverload { /** * Method provided by `IndexOverload` class, acts as * a redirect for indexed getters. - * + * * @example - * + * * ```ts * let indexable: IndexOverload * @@ -57,9 +59,9 @@ export default abstract class IndexOverload { /** * Method provided by `IndexOverload` class, acts as * a redirect for indexed setters. - * + * * @example - * + * * ```ts * let indexable: IndexOverload * @@ -71,4 +73,72 @@ export default abstract class IndexOverload { * @param value New value */ protected abstract set(idx: number, value: T): T; + + /** + * A method that returns the default iterator for + * an object. Called by the semantics of the for-of + * statement. + */ + [Symbol.iterator]() { + return this.iter(); + } + + /** + * A method that returns an iterator for the current + * class. This is used by the iterator symbol to generate + * a for loop from the data structure. + * + * @example + * + * ```ts + * class Collection extends DataStructure { + * constructor() { + * super(); + * this[0] = 1; + * this[1] = 3; + * this[2] = 6; + * } + * + * public override *iter() { + * let i = 0; + * while (this[i] !== undefined) + * yield this[i++]; + * return i; + * } + * } + * + * for (const element of new Collection()) { + * console.log(element); + * } + * ``` + */ + protected abstract iter(): Generator; + + /** + * Per default this function is called when printing + * out the object to the console. + * + * @link https://stackoverflow.com/a/40699119/16002144 + */ + public [util.inspect.custom](): string { + return this.toString(); + } + + /** + * Returns a string representation of the data structure. + * The return string can either by array like or entirely + * different. + * + * The same method is called when printing out to the + * console. + * + * @example + * + * ``` + * console.log(object.toString()); // [T1, T2, ...] + * ``` + */ + public toString(): string { + return Array.from(this.iter()).toString(); + } }