typedash
Version:
modern, type-safe collection of utility functions
1 lines • 1.77 kB
Source Map (JSON)
{"version":3,"file":"assert-d3rr9bN5.cjs","names":["compact"],"sources":["../src/functions/assert/assert.ts"],"sourcesContent":["import { compact } from '../compact';\n\n/**\n * Asserts that a condition is true, throwing an Error if it is not.\n * @param condition A condition that should be true\n * @param message A message to use in the Error that will be thrown if the condition is falsy\n * @throws An `AssertionError` if the condition is falsy\n * @example\n * ```ts\n * function doWork(value: number | undefined) {\n * assert(value !== undefined, 'value should be defined');\n * // value is now narrowed to number\n *\n * return value + 1;\n * }\n * ```\n * @example\n * ```ts\n * assert(1 === 1); // OK\n * assert(1 === 2); // throws AssertionError\n * assert(1 === 2, '1 should equal 2'); // throws AssertionError: 1 should equal 2\n * ```\n */\nexport function assert(\n condition?: unknown,\n message?: string\n): asserts condition {\n if (\n // biome-ignore lint/complexity/noArguments: simpler than anything else to be honest\n arguments.length === 0\n ) {\n return;\n }\n\n if (!condition) {\n throw new AssertionError(message);\n }\n}\n\n/**\n * An error that is thrown when an assertion is not satisfied.\n * Thrown by {@link assert}.\n */\nexport class AssertionError extends Error {\n constructor(message?: string) {\n super(\n compact([\n 'Assertion not satisfied',\n message ? `: \"${message}\"` : '',\n ]).join('')\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,OACd,WACA,SACmB;AACnB,KAEE,UAAU,WAAW,EAErB;AAGF,KAAI,CAAC,UACH,OAAM,IAAI,eAAe,QAAQ;;;;;;AAQrC,IAAa,iBAAb,cAAoC,MAAM;CACxC,YAAY,SAAkB;AAC5B,QACEA,wBAAQ,CACN,2BACA,UAAU,MAAM,QAAQ,KAAK,GAC9B,CAAC,CAAC,KAAK,GAAG,CACZ"}