summaryrefslogtreecommitdiff
path: root/src/utils/anonymous.ts
blob: 58e5a0a3b0475d9f42c627de833f940406b48563 (plain)
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
export const identity = <T>(x: T): T => x;
export const defined = <T>(x: T | undefined | null): x is T =>
  x !== undefined && x !== null;
export const instanciate = <T, A>(C: new (arg: A) => T): (arg: A) => T => {
  return (arg: A): T => new C(arg);
};
export const get = <T extends Record<K, unknown>, K extends PropertyKey>(
  key: K,
): (obj: T) => T[K] =>
(obj: T): T[K] => obj[key];
export const getCall = <
  T extends Record<K, (...args: unknown[]) => unknown>,
  K extends PropertyKey,
>(
  key: K,
  ...args: Parameters<T[K]>
): (obj: T) => ReturnType<T[K]> =>
(obj: T): ReturnType<T[K]> => obj[key](...args) as ReturnType<T[K]>;
export const pass = <T>(fn: (x: T) => void): (x: T) => T => (x: T): T => {
  fn(x);
  return x;
};
export const equal = <T>(x: T): (y: T) => boolean => (y: T): boolean => x === y;
export const extremeBy = (arr: number[], mode: "max" | "min"): number =>
  Math[mode](...arr);
export const transform = <T, U>(x: T, y: (x: T) => U): U => y(x);