|
| 1 | +import { DataType, List as ArrowList } from '@apache-arrow/esnext-esm'; |
| 2 | + |
| 3 | +import { Scalar } from './scalar.js'; |
| 4 | +import { isInvalid, NULL_VALUE } from './util.js'; |
| 5 | + |
| 6 | +type TVector<T extends Scalar<unknown>> = T[]; |
| 7 | + |
| 8 | +export class List<T extends Scalar<unknown>> implements Scalar<TVector<T>> { |
| 9 | + private _type: new (value?: unknown) => T; |
| 10 | + private _valid = false; |
| 11 | + private _value: TVector<T> = []; |
| 12 | + |
| 13 | + constructor(scalarType: new (value?: unknown) => T, initialValue?: unknown) { |
| 14 | + this._type = scalarType; |
| 15 | + if (!isInvalid(initialValue)) this.value = initialValue; |
| 16 | + } |
| 17 | + |
| 18 | + get dataType(): DataType { |
| 19 | + return new ArrowList(this._type.prototype.dataType); |
| 20 | + } |
| 21 | + |
| 22 | + set value(inputValue: unknown) { |
| 23 | + if (isInvalid(inputValue)) { |
| 24 | + this._valid = false; |
| 25 | + this._value = []; |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const inputArray = Array.isArray(inputValue) ? inputValue : [inputValue]; |
| 30 | + const temporaryVector: TVector<T> = []; |
| 31 | + |
| 32 | + if (inputArray.length > 0) { |
| 33 | + const firstItemScalar = new this._type(); |
| 34 | + firstItemScalar.value = inputArray[0]; |
| 35 | + const firstItemType = Object.getPrototypeOf(firstItemScalar).constructor; |
| 36 | + |
| 37 | + for (const item of inputArray) { |
| 38 | + const scalar = new this._type(); |
| 39 | + scalar.value = item; |
| 40 | + |
| 41 | + if (Object.getPrototypeOf(scalar).constructor !== firstItemType) { |
| 42 | + throw new Error( |
| 43 | + `Type mismatch: All items should be of the same type as the first item. Expected type ${ |
| 44 | + firstItemType.name |
| 45 | + }, but got ${Object.getPrototypeOf(scalar).constructor.name}`, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + temporaryVector.push(scalar); |
| 50 | + } |
| 51 | + |
| 52 | + this._value = temporaryVector; |
| 53 | + this._valid = true; // List becomes valid if we successfully process values |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + this._valid = true; // An empty list is valid |
| 58 | + } |
| 59 | + |
| 60 | + get valid(): boolean { |
| 61 | + return this._valid; |
| 62 | + } |
| 63 | + |
| 64 | + get value(): TVector<T> { |
| 65 | + return this._value; |
| 66 | + } |
| 67 | + |
| 68 | + toString(): string { |
| 69 | + if (!this._valid) { |
| 70 | + return NULL_VALUE; |
| 71 | + } |
| 72 | + return `[${this._value.map((v) => v.toString()).join(', ')}]`; |
| 73 | + } |
| 74 | + |
| 75 | + get length(): number { |
| 76 | + return this._value.length; |
| 77 | + } |
| 78 | + |
| 79 | + // If you need an equality method, you can add an equals method similar to the Python __eq__ |
| 80 | + equals(other: List<T>): boolean { |
| 81 | + if (!other) return false; |
| 82 | + if (this.constructor !== other.constructor) return false; |
| 83 | + if (this._valid !== other.valid) return false; |
| 84 | + return JSON.stringify(this._value) === JSON.stringify(other.value); // crude equality check for objects, might need refinement. |
| 85 | + } |
| 86 | +} |
0 commit comments