UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 2.33 kB
{"version":3,"sources":["../../src/functions/createTypeGuard/createTypeGuard.ts","../../src/functions/without/without.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;;;ACXO,SAAS,QACd,OACA,gBACsB;AACtB,QAAM,kBAAkB,gBAAgB,cAAc;AACtD,SAAO,MAAM,OAAO,CAAC,SAAgC,CAAC,gBAAgB,IAAI,CAAC;AAC7E","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 * Returns a new array containing all elements of the input array except the specified items to exclude.\n * @param array The input array to exclude items from.\n * @param itemsToExclude An iterable of items to exclude from the input array.\n * @returns A new array containing all elements of the input array except the specified items to exclude.\n * @example\n * ```ts\n * without([1, 2, 3], [2, 3]) // [1]\n * without(['a', 'b', 'c'], ['b', 'c']) // ['a']\n * without([1, 2, 3], []) // [1, 2, 3]\n */\nexport function without<T, const S extends T>(\n array: readonly T[],\n itemsToExclude: Iterable<S>\n): Array<Exclude<T, S>> {\n const isItemToExclude = createTypeGuard(itemsToExclude);\n return array.filter((item): item is Exclude<T, S> => !isItemToExclude(item));\n}\n"]}