All files / src/serdes struct.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 6/6
100% Lines 12/12

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 2419x   19x 206x 5x   340x 1682x         207x 207x 1024x   207x         19x 1x  
import { define, StructFactory, TupleFactory } from "..";
 
export const struct: StructFactory = (definition) => {
  const obj = definition instanceof Array ? () => [] : () => ({});
  return define(
    (ctx, data) => {
      for (const key in definition) {
        definition[key].ser(ctx, data[key]);
      }
    },
    (ctx) => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const data = obj() as any;
      for (const key in definition) {
        data[key] = definition[key].des(ctx);
      }
      return data;
    }
  );
};
 
export const tuple: TupleFactory = (...definition) =>
  struct(definition);