All files / src/common_serdes size.ts

100% Statements 20/20
100% Branches 6/6
100% Functions 4/4
100% Lines 20/20

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 3819x     19x     15x 32x 32x 32x 17x   15x 15x           13x 13x 13x 29x 29x 29x   13x       19x 15x   13x 13x      
import { define } from "../define";
import { Serdes } from "../types";
 
export const usize: Serdes<number> = define(
  (ctx, data) => {
    // eslint-disable-next-line no-constant-condition
    while (true) {
      const byte = data & 0x7f;
      data >>= 7;
      if (data) {
        ctx.view.setUint8(ctx.i++, byte | 0x80);
      } else {
        ctx.view.setUint8(ctx.i++, byte);
        return;
      }
    }
  },
  (ctx) => {
    let byte: number,
      res = 0,
      off = 0;
    do {
      byte = ctx.view.getUint8(ctx.i++);
      res += (byte & 0x7f) << off;
      off += 7;
    } while (byte > 0x7f);
    return res;
  }
);
 
export const size: Serdes<number> = define(
  (ctx, data) => usize.ser(ctx, data >= 0 ? data * 2 : data * -2 - 1),
  (ctx) => {
    const num = usize.des(ctx);
    return num & 1 ? (num + 1) / -2 : num / 2;
  }
);