All files / src/typers number.ts

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

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 126 127 128 129 130 131 132 133 134    19x   83733x     20725x       19x   24x 24x     4x 4x 4x       19x   802x 802x     202x 202x 202x       19x   2x 2x     2x 2x 2x       19x   2x     2x       19x   2x 2x     2x 2x 2x       19x   2x 2x     2x 2x 2x       19x   2x 2x     2x 2x 2x       19x   1x 1x     1x 1x 1x       19x   1x 1x     1x 1x 1x       19x                         19x 12x      
import { NumberMaker, Typer } from "../types";
 
export const uint8: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setUint8(ctx.i++, data);
  },
  decode(ctx) {
    return ctx.view.getUint8(ctx.i++);
  }
};
 
export const uint16: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setUint16(ctx.i, data);
    ctx.i += 2;
  },
  decode(ctx) {
    const data = ctx.view.getUint16(ctx.i);
    ctx.i += 2;
    return data;
  }
};
 
export const uint32: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setUint32(ctx.i, data);
    ctx.i += 4;
  },
  decode(ctx) {
    const data = ctx.view.getUint32(ctx.i);
    ctx.i += 4;
    return data;
  }
};
 
export const bigUint64: Typer<bigint> = {
  encode(ctx, data) {
    ctx.view.setBigUint64(ctx.i, data);
    ctx.i += 8;
  },
  decode(ctx) {
    const data = ctx.view.getBigUint64(ctx.i);
    ctx.i += 8;
    return data;
  }
};
 
export const int8: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setInt8(ctx.i++, data);
  },
  decode(ctx) {
    return ctx.view.getInt8(ctx.i++);
  }
};
 
export const int16: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setInt16(ctx.i, data);
    ctx.i += 2;
  },
  decode(ctx) {
    const data = ctx.view.getInt16(ctx.i);
    ctx.i += 2;
    return data;
  }
};
 
export const int32: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setInt32(ctx.i, data);
    ctx.i += 4;
  },
  decode(ctx) {
    const data = ctx.view.getInt32(ctx.i);
    ctx.i += 4;
    return data;
  }
};
 
export const bigInt64: Typer<bigint> = {
  encode(ctx, data) {
    ctx.view.setBigInt64(ctx.i, data);
    ctx.i += 8;
  },
  decode(ctx) {
    const data = ctx.view.getBigInt64(ctx.i);
    ctx.i += 8;
    return data;
  }
};
 
export const float32: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setFloat32(ctx.i, data);
    ctx.i += 4;
  },
  decode(ctx) {
    const data = ctx.view.getFloat32(ctx.i);
    ctx.i += 4;
    return data;
  }
};
 
export const float64: Typer<number> = {
  encode(ctx, data) {
    ctx.view.setFloat64(ctx.i, data);
    ctx.i += 8;
  },
  decode(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: NumberMaker = (kind, size) =>
  mappings[
    `${kind}${size}` as keyof typeof mappings
  ] as ReturnType<NumberMaker>;