All files / src/serdes asyncStruct.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 24 2519x     19x 201x 3x   305x 1808x         202x 202x 1204x   202x         19x 1x  
import { asyncDefine } from "../asyncDefine";
import { AsyncStructFactory, AsyncTupleFactory } from "../types";
 
export const asyncStruct: AsyncStructFactory = (definition) => {
  const obj = definition instanceof Array ? () => [] : () => ({});
  return asyncDefine(
    async (ctx, data) => {
      for (const key in definition) {
        await definition[key].ser(ctx, data[key]);
      }
    },
    async (ctx) => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const data = obj() as any;
      for (const key in definition) {
        data[key] = await definition[key].des(ctx);
      }
      return data;
    }
  );
};
 
export const asyncTuple: AsyncTupleFactory = (...defintion) =>
  asyncStruct(defintion);