All files / src/typers asyncStruct.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   212x 1241x       102x 102x 604x   102x         19x 1x  
import { AsyncStructMaker, AsyncTupleMaker, TypeOf } from "../types";
 
export const asyncStruct: AsyncStructMaker = (definition) => {
  const obj = definition instanceof Array ? () => [] : () => ({});
  return {
    async encode(ctx, data) {
      for (const key in definition) {
        await definition[key].encode(ctx, data[key]);
      }
    },
    async decode(ctx) {
      const data = obj() as TypeOf<typeof this>;
      for (const key in definition) {
        data[key] = await definition[key].decode(ctx);
      }
      return data;
    }
  };
};
 
export const asyncTuple: AsyncTupleMaker = (...defintion) =>
  asyncStruct(defintion);