All files / src/decoders array.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 2/2
100% Lines 24/24

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        31x   31x   31x 55x   55x 55x   55x 4x 4x 4x 27x   51x 3x   48x   48x 1x     47x 157x 157x             28x 28x 28x 28x 27x    
import { DecodeContext } from '../decode';
import { isBooleanType, ValueType } from '../types';
 
export function decodeArrayGroups(array: any[], context: DecodeContext): void {
	const { reader, decoders } = context;
 
	let i = 0;
 
	while (reader.readUintVar() !== ValueType.END) {
		reader.position--;
 
		const type: ValueType = reader.readUintVar();
		let length = reader.readUintVar();
 
		if (isBooleanType(type)) {
			const bitset = reader.readBitset(length);
			let j = 0;
			while (j < length) {
				array[i++] = bitset[j++];
			}
		} else if (type === ValueType.UNDEFINED) {
			i += length;
		} else {
			const decodeMethod = decoders.get(type);
 
			if (!decodeMethod) {
				throw `Decoder method not found for object type: ${type} in array decoding`;
			}
 
			while (length--) {
				const item = decodeMethod(context);
				array[i++] = item;
			}
		}
	}
}
 
export function decodeArray(context: DecodeContext): Array<any> {
	const { links } = context;
	const array: any[] = [];
	links.push(array);
	decodeArrayGroups(array, context);
	return array;
}