typedash
Version:
modern, type-safe collection of utility functions
1 lines • 4.53 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/createTypeGuard/createTypeGuard.ts","../../src/functions/negate/negate.ts","../../src/functions/difference/difference.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;;;ACVO,SAAS,OACd,MACwC;AACxC,SAAO,IAAI,SAA2B;AACpC,UAAM,SAAS,KAAK,GAAG,IAAI;AAC3B,WAAO,CAAC;AAAA,EACV;AACF;;;ACiBO,SAAS,WACd,QACA,QACA,YACqE;AAErE,MAAI,CAAC,YAAY;AACf,UAAM,eAAe,gBAAgB,MAAM;AAC3C,WAAO,OAAO,OAAO,OAAO,YAAY,CAAC;AAAA,EAC3C;AAEA,SAAO,OAAO;AAAA,IACZ,CAAC,OAA4B,CAAC,OAAO,KAAK,CAAC,OAAO,WAAW,IAAI,EAAE,CAAC;AAAA,EACtE;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 { AnyFunction } from '../../types/_internal';\n\n/**\n * Returns a new function that negates the result of the input function.\n * @param func The input function to negate.\n * @returns A new function that negates the result of the input function.\n * @example\n * ```ts\n * const isEven = (num: number) => num % 2 === 0;\n * const isOdd = negate(isEven);\n * isOdd(1); // true\n * isOdd(2); // false\n * ```\n */\nexport function negate<Func extends AnyFunction<boolean>>(\n func: Func\n): (...args: Parameters<Func>) => boolean {\n return (...args: Parameters<Func>) => {\n const result = func(...args);\n return !result;\n };\n}\n","import type { IsLiteral } from 'type-fest';\n\nimport { createTypeGuard } from '../createTypeGuard';\nimport { negate } from '../negate';\n\n/**\n * Gets the difference 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 difference between the two arrays - items that are in the first array but not in the second.\n * @example\n * ```ts\n * difference([1, 2, 3], [2, 3, 4]) // [2, 3]\n * ```\n */\nexport function difference<const T, const S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): Array<IsLiteral<T> | IsLiteral<S> extends true ? Exclude<T, S> : T>;\n/**\n * Gets the difference 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 difference between the two arrays - items that are in the first array but not in the second.\n * @example\n * ```ts\n * difference([1, 2, 3], [2, 3, 4]) // [2, 3]\n * ```\n */\nexport function difference<T, S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): Array<IsLiteral<T> | IsLiteral<S> extends true ? Exclude<T, S> : T>;\n// eslint-disable-next-line jsdoc/require-jsdoc -- implementation of the overload signatures\nexport function difference<T, S extends T>(\n array1: readonly T[],\n array2: readonly S[],\n comparator?: Comparator<T, S>\n): Array<IsLiteral<T> | IsLiteral<S> extends true ? Exclude<T, S> : T> {\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 return array1.filter(negate(isArray2Item)) as Array<Exclude<T, S>>;\n }\n\n return array1.filter(\n (a1): a1 is Exclude<T, S> => !array2.some((a2) => comparator(a1, a2))\n );\n}\n\ntype Comparator<T, S extends T> =\n | ((a: T, b: S) => a is Exclude<T, S>)\n | ((a: T, b: S) => boolean);\n"]}