All files / src/typers struct.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 6/6
100% Lines 11/11

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    19x 101x 3x   208x 1027x       102x 102x 504x   102x         19x 1x  
import { StructMaker, TupleMaker, TypeOf } from "..";
 
export const struct: StructMaker = (definition) => {
  const obj = definition instanceof Array ? () => [] : () => ({});
  return {
    encode(ctx, data) {
      for (const key in definition) {
        definition[key].encode(ctx, data[key]);
      }
    },
    decode(ctx) {
      const data = obj() as TypeOf<typeof this>;
      for (const key in definition) {
        data[key] = definition[key].decode(ctx);
      }
      return data;
    }
  };
};
 
export const tuple: TupleMaker = (...definition) =>
  struct(definition);