All files / src/common js-utils.ts

25% Statements 2/8
0% Branches 0/7
0% Functions 0/2
14.28% Lines 1/7

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      3x                              
/**
 * Will throw error on access of NOT defined properties
 */
export const toSafeRecord = <V, R extends Record<string | number, V>>(
  record: R,
  errorFn: (property: string | number) => Error
): Required<R> =>
  new Proxy(record, {
    get: (target, p: string) => {
      // this are properties which JS runtime checks internally
      if (p === "then" || p === "toJSON") {
        return target[p];
      } else Iif (!Reflect.has(target, p) || target[p] === undefined) {
        throw errorFn(p);
      }
      return target[p];
    },
  }) as Required<R>;