typedash
Version:
modern, type-safe collection of utility functions
1 lines • 3.46 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/createTypeGuard/createTypeGuard.ts","../../src/functions/intersection/intersection.ts"],"names":[],"mappings":";AAeO,SAAS,gBAId,QAAyE;AACzE,QAAM,YAAY,IAAI,IAAa,MAAM;AACzC,SAAO,SAAS,UAAU,GAAuC;AAC/D,WAAO,UAAU,IAAI,CAAC;AAAA,EACxB;AACF;;;ACWO,SAAS,aACd,QACA,QACA,YACK;AAEL,MAAI,CAAC,YAAY;AACf,UAAM,eAAe,gBAAgB,MAAM;AAE3C,WAAO,OAAO,OAAO,YAAY;AAAA,EACnC;AAEA,SAAO,OAAO;AAAA,IAAO,CAAC,OACpB,OAAO,KAAK,CAAC,OAAO,WAAW,IAAI,EAAE,CAAC;AAAA,EACxC;AACF","sourcesContent":["/**\n * Creates a type guard that checks if the given type is assignable to the given type.\n * @param values The values to check against.\n * @template {TInput} The type to check against, `unknown` by default. Pass in if you want to have a narrowed type for the type predicate (e.g. `string`).\n * @returns A type guard that checks if the given type is assignable to the given type.\n * @example\n * ```ts\n * const isValidValue = createTypeGuard(['foo', 'bar']);\n *\n * const value: unknown = '...';\n * if (isValidValue(value)) {\n * // ✅ value is of type `'foo' | 'bar'`\n * }\n * ```\n */\nexport function createTypeGuard<\n const TKnownValue,\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents -- without this we can't have a default type of `unknown`\n TInput extends TKnownValue | unknown = unknown,\n>(values: Iterable<TKnownValue>): (v: TInput) => v is TInput & TKnownValue {\n const setValues = new Set<unknown>(values);\n return function predicate(v: unknown): v is TInput & TKnownValue {\n return setValues.has(v);\n };\n}\n","import { createTypeGuard } from '../createTypeGuard';\n\n/**\n * Gets the intersection between two arrays.\n * @param array1 The first array.\n * @param array2 The second array.\n * @param comparator A function that determines whether two items are equal.\n * @returns An array with the intersection between the two arrays.\n * @example\n * ```ts\n * intersection([1, 2, 3], [2, 3, 4]) // [2, 3]\n * ```\n */\nexport function intersection<const T, const S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): S[];\n/**\n * Gets the intersection between two arrays.\n * @param array1 The first array.\n * @param array2 The second array.\n * @param comparator A function that determines whether two items are equal.\n * @returns An array with the intersection between the two arrays.\n * @example\n * ```ts\n * intersection([1, 2, 3], [2, 3, 4]) // [2, 3]\n * ```\n */\nexport function intersection<T, S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): S[];\n// eslint-disable-next-line jsdoc/require-jsdoc -- implementation of the overload signatures\nexport function intersection<T, S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): S[] {\n // no comparator provided - we can do a quicker check using `Object.is` via a Set.\n if (!comparator) {\n const isArray2Item = createTypeGuard(array2);\n // eslint-disable-next-line unicorn/no-array-callback-reference -- smaller bundle size, and we know that we're not using the other arguments in our implementation\n return array1.filter(isArray2Item);\n }\n\n return array1.filter((a1): a1 is Extract<T, S> =>\n array2.some((a2) => comparator(a1, a2))\n );\n}\n\ntype Comparator<T, S extends T> =\n | ((a: T, b: S) => a is S)\n | ((a: T, b: S) => boolean);\n"]}