UNPKG

@kakasoo/proto-typescript

Version:

Utility types and implementations based on JavaScript prototypes.

115 lines 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypedArray = void 0; const prototypes_1 = require("../prototypes"); const typed_number_class_1 = require("./typed-number.class"); const typed_object_class_1 = require("./typed-object.class"); const typed_string_class_1 = require("./typed-string.class"); class TypedArray extends typed_object_class_1.TypedObject { array; constructor(data) { super(data); this.array = data; /** * mapping for index signature */ data.forEach((el, i) => { this[i] = el; }); } refine(data) { return new TypedArray(data); } [Symbol.iterator]() { let i = 0; return { next: () => { return i === this.array.length ? { done: true, value: undefined } : { value: this.array.at(i++), done: false }; }, }; } /** * It only returns the 0th index without subtracting the elements inside the actual container. * @todo Add an option parameter for if you want to cause the type after the element has been removed from the interior of the container to be inferred. */ shift() { return this.at(0); } /** * Only return the last index without subtracting the elements inside the actual container. * @todo Add an option parameter for if you want to cause the type after the element has been removed from the interior of the container to be inferred. */ pop() { return this.at(this.array.length - 1); } /** * @inheritdoc * @example * ```ts * new TypedArray([1, 2, 3] as const).unshift(4 as const); // [4, 1, 2, 3] * ``` * * @param items * @returns Unlike JavaScript's Array.prototype.unshift, it returns a new TypeArray instance rather than the length of the inserted data. */ unshift(...items) { const initialValue = prototypes_1.ArrayPrototype.unshift(this.array, ...items); return new TypedArray(initialValue); } /** * @inheritdoc * * I'm reviewing whether internal functions that are different from the existing som should be provided by default for type inference. * @todo add function named `IsSameElement` for type inference. */ some(predicate) { return prototypes_1.ArrayPrototype.some(this.array, predicate); } /** * @inheritdoc * @example * ```ts * new TypedArray([1, 2, 3] as const).push(4 as const); // [1, 2, 3, 4] * ``` * * @param items * @returns Unlike JavaScript's Array.prototype.push, it returns a new TypeArray instance rather than the length of the inserted data. * * @todo * Supporting TypedString and TypedNumber for items that are element type of items. * I'm wondering if this is the right type reasoning or if it should still be in the form of the Wrapper class. */ push(...items) { const initialValue = prototypes_1.ArrayPrototype.push(this.array, ...items); return new TypedArray(initialValue); } /** * @inheritDoc */ at(index = new typed_number_class_1.TypedNumber()) { const primitiveIndex = this.isTypedClass(index) ? index.toPrimitive() : index; return this.array.at(primitiveIndex); } /** * type-safe join. * @example * ```ts * new TypedArray([1, 2, 3] as const).join(','); // '1,2,3' * ``` * * @inheritdoc * @todo support `TypedString` type as Separator */ join(separator = ',') { const primitiveSeparator = this.isTypedClass(separator) ? separator.toPrimitive() : separator; const initialValue = prototypes_1.ArrayPrototype.join(this.array, primitiveSeparator); return new typed_string_class_1.TypedString(initialValue); } toPrimitive() { return [...this.array]; } } exports.TypedArray = TypedArray; //# sourceMappingURL=typed-array.class.js.map