All files / core/src/utility type-of.ts

100% Statements 289/289
100% Branches 17/17
100% Functions 16/16
100% Lines 289/289

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 2901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 20x 20x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 32x 32x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 31x 31x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 98x 98x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 978x 978x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 173x 173x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 809x 809x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 114x 114x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 14x 14x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 14x 14x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 228x 228x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 666x 666x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 14x 14x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 27x 27x 71x 71x 71x 71x 71x 71x 71x 71x 71x 3218x 3218x 71x 71x 71x 71x 71x 71x 71x 1x 1x 1x 1x  
/**
 * Strict type-assertion and runtime type-checking utility. When type-checking
 * variables in the global scope, e.g., `window` or `process`, make use of the
 * `globalThis` object.
 *
 * @example
 * Type-check global context:
 * ```ts
 * import { TypeOf } from '@sgrud/core';
 *
 * TypeOf.process(globalThis.process); // true if running in node context
 * TypeOf.window(globalThis.window);   // true if running in browser context
 * ```
 */
export abstract class TypeOf {
 
  /**
   * Type-check `value` for `unknown[]`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `unknown[]`.
   *
   * @example
   * Type-check `null` for `unknown[]`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.array(null); // false
   * ```
   */
  public static array(value: unknown): value is unknown[] {
    return this.test('Array', value);
  }
 
  /**
   * Type-check `value` for `boolean`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `boolean`.
   *
   * @example
   * Type-check `null` for `boolean`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.boolean(null); // false
   * ```
   */
  public static boolean(value: unknown): value is boolean {
    return this.test('Boolean', value);
  }
 
  /**
   * Type-check `value` for `Date`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `Date`.
   *
   * @example
   * Type-check `null` for `Date`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.date(null); // false
   * ```
   */
  public static date(value: unknown): value is Date {
    return this.test('Date', value);
  }
 
  /**
   * Type-check `value` for `Function`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `Function`.
   *
   * @example
   * Type-check `null` for `Function`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.function(null); // false
   * ```
   */
  public static function(value: unknown): value is Function {
    return this.test('Function', value);
  }
 
  /**
   * Type-check `value` for `null`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `null`.
   *
   * @example
   * Type-check `null` for `null`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.null(null); // true
   * ```
   */
  public static null(value: unknown): value is null {
    return this.test('Null', value);
  }
 
  /**
   * Type-check `value` for `number`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `number`.
   *
   * @example
   * Type-check `null` for `number`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.number(null); // false
   * ```
   */
  public static number(value: unknown): value is number {
    return this.test('Number', value);
  }
 
  /**
   * Type-check `value` for `object`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `object`.
   *
   * @example
   * Type-check `null` for `object`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.object(null); // false
   * ```
   */
  public static object(value: unknown): value is object {
    return this.test('Object', value);
  }
 
  /**
   * Type-check `value` for `NodeJS.Process`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `NodeJS.Process`.
   *
   * @example
   * Type-check `null` for `NodeJS.Process`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.process(null); // false
   * ```
   */
  public static process(value: unknown): value is NodeJS.Process {
    return this.test('process', value);
  }
 
  /**
   * Type-check `value` for `Promise<unknown>`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `Promise<unknown>`.
   *
   * @example
   * Type-check `null` for `Promise<unknown>`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.promise(null); // false
   * ```
   */
  public static promise(value: unknown): value is Promise<unknown> {
    return this.test('Promise', value);
  }
 
  /**
   * Type-check `value` for `RegExp`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `RegExp`.
   *
   * @example
   * Type-check `null` for `RegExp`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.regex(null); // false
   * ```
   */
  public static regex(value: unknown): value is RegExp {
    return this.test('RegExp', value);
  }
 
  /**
   * Type-check `value` for `string`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `string`.
   *
   * @example
   * Type-check `null` for `string`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.string(null); // false
   * ```
   */
  public static string(value: unknown): value is string {
    return this.test('String', value);
  }
 
  /**
   * Type-check `value` for `undefined`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `undefined`.
   *
   * @example
   * Type-check `null` for `undefined`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.undefined(null); // false
   * ```
   */
  public static undefined(value: unknown): value is undefined {
    return this.test('Undefined', value);
  }
 
  /**
   * Type-check `value` for `URL`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `URL`.
   *
   * @example
   * Type-check `null` for `URL`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.url(null); // false
   * ```
   */
  public static url(value: unknown): value is URL {
    return this.test('URL', value);
  }
 
  /**
   * Type-check `value` for `Window`.
   *
   * @param value - The `value` to type-check.
   * @returns Whether `value` is of type `Window`.
   *
   * @example
   * Type-check `null` for `Window`:
   * ```ts
   * import { TypeOf } from '@sgrud/core';
   *
   * TypeOf.window(null); // false
   * ```
   */
  public static window(value: unknown): value is Window {
    return this.test('Window', value);
  }
 
  /**
   * Type-check `value` for `type`.
   *
   * @param type - The `type` to check for.
   * @param value - The `value` to type-check.
   * @returns Whether `value` is `type`.
   */
  private static test(type: string, value: unknown): boolean {
    return Object.prototype.toString.call(value) === `[object ${type}]`;
  }
 
  /**
   * Private **constructor** (which should never be called).
   *
   * @throws A {@link TypeError} upon construction.
   */
  private constructor() {
    throw new TypeError('TypeOf.constructor');
  }
 
}