All files / src/shared types.ts

76.92% Statements 40/52
40% Branches 6/15
46.15% Functions 6/13
70% Lines 28/40

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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                          47x             47x 14x     47x 12x     47x       47x       47x       47x       47x               47x     325x     47x     94x                   47x 47x 47x 47x   47x 47x   47x 47x   47x       14x       14x 194x 92x     14x    
export type JsonValue = string | number | boolean | null;
 
declare const __brand: unique symbol;
type Brand<B> = { [__brand]: B };
export type Branded<T, B> = T & Brand<B>;
 
export type DeepReadonly<T> = {
  readonly [Key in keyof T]: T[Key] extends unknown[] | Record<string, unknown>
    ? DeepReadonly<T[Key]>
    : T[Key];
};
 
/* KeysMatching<T, V>
 *
 * Find all keys in T which have the type V
 * e.g. KeysMatching<Actor, boolean> to return "animate" | "isPinned"
 */
export type KeysMatching<T, V> = {
  [K in keyof T]-?: T[K] extends V ? K : never;
}[keyof T];
 
export const isStringArray = (value: unknown): value is string[] => {
  Iif (!Array.isArray(value)) {
    return false;
  }
  return value.every(isString);
};
 
export const isString = (value: unknown): value is string => {
  return typeof value === "string";
};
 
export const isNumber = (value: unknown): value is number => {
  return typeof value === "number" && !isNaN(value) && isFinite(value);
};
 
export const isBoolean = (value: unknown): value is boolean => {
  return typeof value === "boolean";
};
 
export const isUndefined = (value: unknown): value is undefined => {
  return value === undefined;
};
 
export const isMaybeString = (value: unknown): value is string | undefined => {
  return isString(value) || isUndefined(value);
};
 
export const isMaybeNumber = (value: unknown): value is number | undefined => {
  return isNumber(value) || isUndefined(value);
};
 
export const ensureType = <T>(
  value: unknown,
  fallback: T,
  isType: (value: unknown) => value is T
): T => {
  return isType(value) ? value : fallback;
};
 
export const ensureTypeGenerator = <T>(
  isType: (value: unknown) => value is T
): ((value: unknown, fallback: T) => T) => {
  return (value: unknown, fallback: T) => (isType(value) ? value : fallback);
};
 
export const ensurePromisedTypeGenerator = <T>(
  isType: (value: unknown) => value is T
): ((promise: Promise<unknown>, fallback: T) => Promise<T>) => {
  return async (promise: Promise<unknown>, fallback: T): Promise<T> => {
    try {
      const value = await promise;
      return isType(value) ? value : fallback;
    } catch {
      return fallback;
    }
  };
};
 
export const ensureString = ensureTypeGenerator(isString);
export const ensureStringArray = ensureTypeGenerator(isStringArray);
export const ensureNumber = ensureTypeGenerator(isNumber);
export const ensureBoolean = ensureTypeGenerator(isBoolean);
 
export const ensurePromisedString = ensurePromisedTypeGenerator(isString);
export const ensurePromisedNumber = ensurePromisedTypeGenerator(isNumber);
 
export const ensureMaybeString = ensureTypeGenerator(isMaybeString);
export const ensureMaybeNumber = ensureTypeGenerator(isMaybeNumber);
 
export const omit = <T, O extends keyof T>(
  obj: T,
  ...keys: O[]
): Omit<T, O> => {
  const ret = {} as {
    [K in keyof typeof obj]: typeof obj[K];
  };
  let key: keyof typeof obj;
  for (key in obj) {
    if (!keys.includes(key as O)) {
      ret[key] = obj[key];
    }
  }
  return ret;
};