All files / src/encoders index.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                            21x                                                                               348x   348x   348x 348x   348x               348x    
import { detectValue } from '../detectors/index';
import { EncodeContext } from '../encode';
import { ValueType } from '../types';
import { encodeArray } from './array';
import { encodeArrayBuffer } from './array-buffer';
import { encodeBigint } from './bigint';
import { encodeFalse, encodeTrue } from './boolean';
import { encodeDate } from './date';
import { encodeFunction } from './function';
import { encodeLinkIndex, encodeLinkIndexLast } from './link';
import { encodeMap } from './map';
import { encodeNull } from './null';
import {
	encodeFloat,
	encodeInt,
	encodeNaN,
	encodeNegativeInfinity,
	encodePositiveInfinity,
} from './number';
import { encodeObject } from './object';
import { encodeRegExp } from './regexp';
import { encodeSet } from './set';
import { encodeString } from './string';
import { encodeSymbol } from './symbol';
import { encodeTypedArray } from './typed-array';
import { encodeUndefined } from './undefined';
import { encodeValueIndex, encodeValueIndexLast } from './value';
 
export type EncodeMethod = (value: any, context: EncodeContext) => void;
 
export const ENCODERS = new Map<ValueType, EncodeMethod>([
	// eslint-disable-next-line no-use-before-define
	[ValueType.ANY, encodeValue],
	[ValueType.VALUE_INDEX, encodeValueIndex],
	[ValueType.VALUE_INDEX_LAST, encodeValueIndexLast],
	[ValueType.LINK_INDEX, encodeLinkIndex],
	[ValueType.LINK_INDEX_LAST, encodeLinkIndexLast],
	[ValueType.NULL, encodeNull],
	[ValueType.UNDEFINED, encodeUndefined],
	[ValueType.TRUE, encodeTrue],
	[ValueType.FALSE, encodeFalse],
	[ValueType.NAN, encodeNaN],
	[ValueType.POSITIVE_INFINITY, encodePositiveInfinity],
	[ValueType.NEGATIVE_INFINITY, encodeNegativeInfinity],
	[ValueType.INT, encodeInt],
	[ValueType.FLOAT, encodeFloat],
	[ValueType.BIGINT, encodeBigint],
	[ValueType.STRING, encodeString],
	[ValueType.ARRAY, encodeArray],
	[ValueType.OBJECT, encodeObject],
	[ValueType.SYMBOL, encodeSymbol],
	[ValueType.FUNCTION, encodeFunction],
	[ValueType.SET, encodeSet],
	[ValueType.MAP, encodeMap],
	[ValueType.ARRAY_BUFFER, encodeArrayBuffer],
	[ValueType.UINT8_CLAMPED_ARRAY, encodeTypedArray],
	[ValueType.UINT8_ARRAY, encodeTypedArray],
	[ValueType.UINT16_ARRAY, encodeTypedArray],
	[ValueType.UINT32_ARRAY, encodeTypedArray],
	[ValueType.INT8_ARRAY, encodeTypedArray],
	[ValueType.INT16_ARRAY, encodeTypedArray],
	[ValueType.INT32_ARRAY, encodeTypedArray],
	[ValueType.FLOAT32_ARRAY, encodeTypedArray],
	[ValueType.FLOAT64_ARRAY, encodeTypedArray],
	[ValueType.DATA_VIEW, encodeTypedArray],
	[ValueType.DATE, encodeDate],
	[ValueType.REG_EXP, encodeRegExp],
]);
 
export function encodeValue(value: any, context: EncodeContext) {
	const { writer, encoders, replacer } = context;
 
	value = replacer(value);
 
	const type = detectValue(value, context);
	writer.writeUintVar(type);
 
	const encodeMethod = encoders.get(type);
 
	/* istanbul ignore next */
	if (!encodeMethod) {
		/* istanbul ignore next */
		throw `Encoder method not found for type: ${type}`;
	}
 
	encodeMethod(value, context);
}