All files / src/noeval_serdes number.ts

100% Statements 59/59
100% Branches 0/0
100% Functions 21/21
100% Lines 58/58

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1262x     2x 2x 2x     2x   2x 2x     2x 2x 2x       2x   2x 2x     2x 2x 2x       2x   2x 2x     2x 2x 2x       2x 2x 2x     2x   2x 2x     2x 2x 2x       2x   2x 2x     2x 2x 2x       2x   2x 2x     2x 2x 2x       2x   1x 1x     1x 1x 1x       2x   1x 1x     1x 1x 1x       2x                         2x   12x  
import { define } from "../define";
import { NumberFactory, Serdes } from "../types";
 
export const uint8: Serdes<number> = define(
  (ctx, data) => ctx.view.setUint8(ctx.i++, data),
  (ctx) => ctx.view.getUint8(ctx.i++)
);
 
export const uint16: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setUint16(ctx.i, data);
    ctx.i += 2;
  },
  (ctx) => {
    const data = ctx.view.getUint16(ctx.i);
    ctx.i += 2;
    return data;
  }
);
 
export const uint32: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setUint32(ctx.i, data);
    ctx.i += 4;
  },
  (ctx) => {
    const data = ctx.view.getUint32(ctx.i);
    ctx.i += 4;
    return data;
  }
);
 
export const bigUint64: Serdes<bigint> = define(
  (ctx, data) => {
    ctx.view.setBigUint64(ctx.i, data);
    ctx.i += 8;
  },
  (ctx) => {
    const data = ctx.view.getBigUint64(ctx.i);
    ctx.i += 8;
    return data;
  }
);
 
export const int8: Serdes<number> = define(
  (ctx, data) => ctx.view.setInt8(ctx.i++, data),
  (ctx) => ctx.view.getInt8(ctx.i++)
);
 
export const int16: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setInt16(ctx.i, data);
    ctx.i += 2;
  },
  (ctx) => {
    const data = ctx.view.getInt16(ctx.i);
    ctx.i += 2;
    return data;
  }
);
 
export const int32: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setInt32(ctx.i, data);
    ctx.i += 4;
  },
  (ctx) => {
    const data = ctx.view.getInt32(ctx.i);
    ctx.i += 4;
    return data;
  }
);
 
export const bigInt64: Serdes<bigint> = define(
  (ctx, data) => {
    ctx.view.setBigInt64(ctx.i, data);
    ctx.i += 8;
  },
  (ctx) => {
    const data = ctx.view.getBigInt64(ctx.i);
    ctx.i += 8;
    return data;
  }
);
 
export const float32: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setFloat32(ctx.i, data);
    ctx.i += 4;
  },
  (ctx) => {
    const data = ctx.view.getFloat32(ctx.i);
    ctx.i += 4;
    return data;
  }
);
 
export const float64: Serdes<number> = define(
  (ctx, data) => {
    ctx.view.setFloat64(ctx.i, data);
    ctx.i += 8;
  },
  (ctx) => {
    const data = ctx.view.getFloat64(ctx.i);
    ctx.i += 8;
    return data;
  }
);
 
const mappings = {
  uint8,
  uint16,
  uint32,
  int8,
  int16,
  int32,
  float32,
  float64,
  bigUint64,
  bigInt64
} as const;
 
export const number: NumberFactory = (kind, bitSize) =>
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  mappings[`${kind}${bitSize}` as keyof typeof mappings] as any;