UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 5.21 kB
{"version":3,"file":"compare.mjs","names":[],"sources":["../src/compare.ts"],"sourcesContent":["/* ===========================================================\n COMPARE\n - EQUALS\n - COVER\n - LESS\n - FACTORY FUNCTIONS\n=========================================================== */\nimport { Atomic } from \"@typia/interface\";\n\nimport { NoTransformConfigurationError } from \"./transformers/NoTransformConfigurationError\";\n\nexport type Cover<T> = T extends Atomic.Type | null | undefined\n ? T\n : T extends (...args: any[]) => any\n ? T\n : T extends readonly (infer U)[]\n ? Cover<U>[]\n : T extends object\n ? { [K in keyof T]?: Cover<T[K]> }\n : T;\n\n/* -----------------------------------------------------------\n EQUALS\n----------------------------------------------------------- */\n/**\n * Compares two values of type `T`.\n *\n * Performs a type-directed deep equality comparison. Object properties are\n * compared by the declared structure of `T`; extra runtime properties are not\n * part of the comparison. When `T` (or a nested object type) declares an\n * `equals(y: T): boolean` method, that method is used instead of the structural\n * comparison.\n *\n * @template T Type of values to compare\n * @param x Left value\n * @param y Right value\n * @returns Whether both values are equal by structure\n */\nexport function equals<T>(x: T, y: T): boolean;\n\n/** @internal */\nexport function equals(): never {\n NoTransformConfigurationError(\"compare.equals\");\n}\n\n/**\n * Compares whether `x` covers the defined structure of `y`.\n *\n * Object properties whose value is `undefined` in `y` are ignored. Arrays and\n * tuples still require identical lengths and compare element-by-element.\n *\n * @template T Type of value to compare\n * @param x Full value\n * @param y Partial value to match\n * @returns Whether `x` covers `y`\n */\nexport function cover<T>(x: T, y: Cover<T>): boolean;\n\n/** @internal */\nexport function cover(): never {\n NoTransformConfigurationError(\"compare.cover\");\n}\n\n/* -----------------------------------------------------------\n LESS\n----------------------------------------------------------- */\n/**\n * Tests whether `x` precedes `y`.\n *\n * Performs a type-directed lexicographic comparison: the properties of `T` are\n * walked in declaration order, depth-first, and the scalar leaves (`number` /\n * `boolean` / `string` / `bigint`) are compared one by one. The result is\n * decided by the first leaf where `x` and `y` differ; if every leaf is equal,\n * the result is `false` (equal is not \"less\"). When `T` (or a nested object\n * type) declares a `less(y: T): boolean` method, that method is used instead of\n * the structural comparison.\n *\n * The ordering is total and deterministic, so it composes into an\n * `Array.prototype.sort` comparator:\n *\n * ```ts\n * const cmp = (a: T, b: T): number =>\n * typia.compare.less(a, b) ? -1 : typia.compare.less(b, a) ? 1 : 0;\n * ```\n *\n * @template T Type of values to compare\n * @param x Left value\n * @param y Right value\n * @returns Whether `x` precedes `y`\n */\nexport function less<T>(x: T, y: T): boolean;\n\n/** @internal */\nexport function less(): never {\n NoTransformConfigurationError(\"compare.less\");\n}\n\n/* -----------------------------------------------------------\n FACTORY FUNCTIONS\n----------------------------------------------------------- */\n/**\n * Creates reusable {@link equals} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createEquals(): never;\n\n/**\n * Creates reusable {@link equals} function.\n *\n * @template T Type of values to compare\n * @returns Reusable equality function\n */\nexport function createEquals<T>(): (x: T, y: T) => boolean;\n\n/** @internal */\nexport function createEquals(): never {\n NoTransformConfigurationError(\"compare.createEquals\");\n}\n\n/**\n * Creates reusable {@link cover} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createCover(): never;\n\n/**\n * Creates reusable {@link cover} function.\n *\n * @template T Type of value to compare\n * @returns Reusable cover function\n */\nexport function createCover<T>(): (x: T, y: Cover<T>) => boolean;\n\n/** @internal */\nexport function createCover(): never {\n NoTransformConfigurationError(\"compare.createCover\");\n}\n\n/**\n * Creates reusable {@link less} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createLess(): never;\n\n/**\n * Creates reusable {@link less} function.\n *\n * @template T Type of values to compare\n * @returns Reusable ordering function\n */\nexport function createLess<T>(): (x: T, y: T) => boolean;\n\n/** @internal */\nexport function createLess(): never {\n NoTransformConfigurationError(\"compare.createLess\");\n}\n"],"mappings":";;;;;;;;;;;;AAyCA,SAAgB,SAAgB;CAC9B,8BAA8B,gBAAgB;AAChD;;AAgBA,SAAgB,QAAe;CAC7B,8BAA8B,eAAe;AAC/C;;AAgCA,SAAgB,OAAc;CAC5B,8BAA8B,cAAc;AAC9C;;AAqBA,SAAgB,eAAsB;CACpC,8BAA8B,sBAAsB;AACtD;;AAkBA,SAAgB,cAAqB;CACnC,8BAA8B,qBAAqB;AACrD;;AAkBA,SAAgB,aAAoB;CAClC,8BAA8B,oBAAoB;AACpD"}