All files / src/msgpack map.ts

100% Statements 18/18
100% Branches 0/0
100% Functions 2/2
100% Lines 16/16

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  19x 19x 19x   19x             12x 12x 12x 12x 207444x 207444x 207442x       19x         8x 8x 101012x   8x    
import { Context, Encoding, Ser } from "../types";
import { pack } from "./pack";
import { packSize } from "./size";
import { unpack } from "./unpack";
 
export function packMap(
  ctx: Context,
  data: Record<string | number, unknown>,
  floatHead: number,
  float: Ser<number>,
  str: Ser<string>
) {
  const keys = Object.keys(data);
  const { length } = keys;
  packSize(ctx, length, 0x80, 0xde);
  for (let i = 0; i < length; i++) {
    const key = keys[i];
    pack(ctx, key, floatHead, float, str);
    pack(ctx, data[key], floatHead, float, str);
  }
}
 
export function unpackMapBody(
  ctx: Context,
  size: number,
  encoding: Encoding<string>
) {
  const data: Record<string, unknown> = {};
  for (let i = 0; i < size; i++) {
    data[unpack(ctx, encoding) as string] = unpack(ctx, encoding);
  }
  return data;
}