All files / packages/value/src list.ts

83.33% Statements 15/18
50% Branches 2/4
85.71% Functions 6/7
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                        55x       136x       43x 43x         43x           48x       48x   48x 119x 119x     48x       42x 42x 107x       42x                  
import { create } from '@bufbuild/protobuf'
import * as Ydb from '@ydbjs/api/value'
 
import { type Type, TypeKind } from './type.js'
import type { Value } from './value.js'
import { NullType } from './null.js'
 
export class ListType implements Type {
	readonly item: Type
	#typeInstance?: Ydb.Type
 
	constructor(item: Type) {
		this.item = item
	}
 
	get kind(): TypeKind.LIST {
		return TypeKind.LIST
	}
 
	encode(): Ydb.Type {
		Eif (!this.#typeInstance) {
			this.#typeInstance = create(Ydb.TypeSchema, {
				type: { case: 'listType', value: { item: this.item.encode() } },
			})
		}
 
		return this.#typeInstance
	}
}
 
export class List<T extends Value = Value> implements Value<ListType> {
	readonly type: ListType
	readonly items: T[] = []
	#valueInstance?: Ydb.Value
 
	constructor(...items: T[]) {
		let type: Type = new NullType()
 
		for (let item of items) {
			type = item.type
			this.items.push(item)
		}
 
		this.type = new ListType(type)
	}
 
	encode(): Ydb.Value {
		Eif (!this.#valueInstance) {
			this.#valueInstance = create(Ydb.ValueSchema, {
				items: this.items.map((item) => item.encode()),
			})
		}
 
		return this.#valueInstance
	}
 
	*[Symbol.iterator](): Iterator<T> {
		for (let i = 0; i < this.items.length; i++) {
			yield this.items[i]!
		}
	}
}