Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "types/array"

Index

Type aliases

Diff

Diff: any

Name

Name: "array"

Variables

Const ArrayType

ArrayType: ArrayTypeConstructor = <any> class<T, M extends Type<T> = Type<T>> {readonly name: Name = name;readonly itemType!: M;readonly minLength?: number;readonly maxLength!: number;private _options: Lazy<ArrayTypeOptions<T, M>>;constructor(options: Lazy<ArrayTypeOptions<T, M>>) {this._options = options;if (typeof options !== "function") {this._applyOptions();} else {lazyProperties(this, this._applyOptions, ["itemType", "minLength", "maxLength"]);}}// TODO: Dynamically add with prototype?read<R>(reader: Reader<R>, raw: R): T[] {const itemType: M = this.itemType;const minLength: number | undefined = this.minLength;const maxLength: number | undefined = this.maxLength;return reader.readList(raw, readVisitor({fromList<RI>(input: Iterable<RI>, itemReader: Reader<RI>): T[] {let invalid: undefined | Map<number, Error> = undefined;const result: T[] = [];let i: number = 0;for (const rawItem of input) {if (maxLength !== undefined && i === maxLength) {throw createMaxArrayLengthError([...input], maxLength);}try {const item: T = itemType.read!(itemReader, rawItem);if (invalid === undefined) {result.push(item);}} catch (err) {if (invalid === undefined) {invalid = new Map();}invalid.set(i, err);}i++;}if (invalid !== undefined) {throw createInvalidArrayItemsError(invalid);}if (minLength !== undefined && i < minLength) {throw createMinArrayLengthError([...input], minLength);}return result;},}));}// TODO: Dynamically add with prototype?write<W>(writer: Writer<W>, value: T[]): W {return writer.writeList(value.length, <IW>(index: number, itemWriter: Writer<IW>): IW => {if (this.itemType.write === undefined) {throw new Incident("NotWritable", {type: this.itemType});}return this.itemType.write(itemWriter, value[index]);});}testError(value: T[]): Error | undefined {if (!Array.isArray(value)) {return createInvalidTypeError("array", value);}if (this.maxLength !== undefined && value.length > this.maxLength) {return createMaxArrayLengthError(value, this.maxLength);}if (this.minLength !== undefined && value.length < this.minLength) {return createMinArrayLengthError(value, this.minLength);}const invalid: Map<number, Error> = new Map();const itemCount: number = value.length;for (let i: number = 0; i < itemCount; i++) {const error: Error | undefined = testError(this.itemType, value[i]);if (error !== undefined) {invalid.set(i, error);}}if (invalid.size !== 0) {return createInvalidArrayItemsError(invalid);}return undefined;}test(val: T[]): val is T[] {if (!Array.isArray(val)|| (this.maxLength !== undefined && val.length > this.maxLength)|| (this.minLength !== undefined && val.length < this.minLength)) {return false;}for (const item of val) {if (!this.itemType.test(item)) {return false;}}return true;}equals(val1: T[], val2: T[]): boolean {if (val2.length !== val1.length) {return false;}for (let i: number = 0; i < val1.length; i++) {if (!this.itemType.equals(val2[i], val1[i])) {return false;}}return true;}clone(val: T[]): T[] {return val.map((item: T): T => this.itemType.clone(item));}private _applyOptions(): void {if (this._options === undefined) {throw createLazyOptionsError(this);}const options: ArrayTypeOptions<T, M> = typeof this._options === "function" ? this._options() : this._options;const itemType: M = options.itemType;const minLength: number | undefined = options.minLength;const maxLength: number = options.maxLength;Object.assign(this, {itemType, minLength, maxLength});}}

Const name

name: Name = "array"

Generated using TypeDoc