typedash
Version:
modern, type-safe collection of utility functions
1 lines • 17.7 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/isArray/isArray.ts","../../src/functions/castArrayIfDefined/castArrayIfDefined.ts","../../src/functions/castArray/castArray.ts","../../src/functions/createTypeGuard/createTypeGuard.ts","../../src/functions/_internal/filterObject/createObjectPredicate.ts","../../src/functions/objectEntries/objectEntries.ts","../../src/functions/_internal/filterObject/filterObject.ts","../../src/functions/negate/negate.ts","../../src/functions/omit/omit.ts"],"names":[],"mappings":";AAGO,IAAM,UAAmB,MAAM;;;AC4E/B,SAAS,mBACd,OAGA;AACA,MAAI,SAAS,MAAM;AACjB,WAAO;AAAA,EACT;AAEA,MAAI,QAAQ,KAAK,GAAG;AAClB,WAAO;AAAA,EACT;AAEA,SAAO,CAAC,KAAK;AACf;;;ACOO,SAAS,UACd,OAGA;AACA,SAAO,mBAAmB,SAAS,CAAC,CAAC;AACvC;;;AC3FO,SAAS,gBAId,QAAyE;AACzE,QAAM,YAAY,IAAI,IAAa,MAAM;AACzC,SAAO,SAAS,UAAU,GAAuC;AAC/D,WAAO,UAAU,IAAI,CAAC;AAAA,EACxB;AACF;;;ACJO,SAAS,sBAGd,uBAAyE;AACzE,SAAO,OAAO,0BAA0B,aACpC,wBACA,0BAA0B,qBAAqB;AACrD;AAQA,SAAS,0BAGP,YAAyC;AACzC,QAAM,kBAAkB,gBAAgB,UAAU,UAAU,CAAC;AAE7D,SAAO,CAAC,QAAQ,QAAQ,gBAAgB,GAAG;AAC7C;;;ACxCO,IAAM,gBAA8B,OAAO;;;ACG3C,SAAS,aACd,QACA,WACA;AACA,MAAI,UAAU,MAAM;AAClB,WAAO,CAAC;AAAA,EACV;AAEA,SAAO,OAAO;AAAA,IACZ,cAAc,MAAM,EAAE;AAAA,MAAO,CAAC,CAAC,KAAK,KAAK,MACvC;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ACRO,SAAS,OACd,MACwC;AACxC,SAAO,IAAI,SAA2B;AACpC,UAAM,SAAS,KAAK,GAAG,IAAI;AAC3B,WAAO,CAAC;AAAA,EACV;AACF;;;ACaO,SAAS,KAGd,QAAW,uBAAqD;AAChE,SAAO;AAAA,IACL;AAAA,IACA,OAAO,sBAAsB,qBAAqB,CAAC;AAAA,EACrD;AACF","sourcesContent":["import { Many, Maybe } from '../../types';\n\n// eslint-disable-next-line prefer-destructuring\nexport const isArray: IsArray = Array.isArray;\n\ninterface IsArray {\n /**\n * The same as `Array.isArray` but with a better type guard.\n * @param value The value to check.\n * @returns `true` if the value is an array, `false` otherwise.\n * @example\n * ```ts\n * isArray([1, 2, 3]) // true\n * isArray('foo') // false\n * ```\n */\n <T>(value: Maybe<Array<ArrayElement<T>>>): value is NonNullable<typeof value>;\n /**\n * The same as `Array.isArray` but with a better type guard.\n * @param value The value to check.\n * @returns `true` if the value is an array, `false` otherwise.\n * @example\n * ```ts\n * isArray([1, 2, 3]) // true\n * isArray('foo') // false\n * ```\n */\n <T>(\n value: Maybe<ReadonlyArray<ArrayElement<T>>>\n ): value is NonNullable<typeof value>;\n\n /**\n * The same as `Array.isArray` but with a better type guard.\n * @param value The value to check.\n * @returns `true` if the value is an array, `false` otherwise.\n * @example\n * ```ts\n * isArray([1, 2, 3]) // true\n * isArray('foo') // false\n * ```\n */\n <T>(value: Maybe<Many<T>>): value is NonNullable<readonly T[]>;\n /**\n * The same as `Array.isArray` but with a better type guard.\n * @param value The value to check.\n * @returns `true` if the value is an array, `false` otherwise.\n * @example\n * ```ts\n * isArray([1, 2, 3]) // true\n * isArray('foo') // false\n * ```\n */\n <T>(value: unknown): value is readonly T[];\n}\n\ntype ArrayElement<T> = T extends ReadonlyArray<infer U> ? U : never;\n","import { Many, Maybe } from '../../types';\nimport { isArray } from '../isArray';\n\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n * @example\n * ```ts\n * castArrayIfDefined(null) // null\n * ```\n */\nexport function castArrayIfDefined(value: null): null;\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @example\n * ```ts\n * castArrayIfDefined(undefined) // undefined\n * ```\n */\nexport function castArrayIfDefined(value: undefined): undefined;\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n * @example\n * ```ts\n * castArrayIfDefined([1, 2, 3]) // [1, 2, 3]\n * ```\n */\nexport function castArrayIfDefined<T>(value: readonly T[]): readonly T[];\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n * @example\n * ```ts\n * castArrayIfDefined([1, 2, 3]) // [1, 2, 3]\n * ```\n */\nexport function castArrayIfDefined<T>(value: T[]): T[];\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n * @example\n * ```ts\n * castArrayIfDefined([1, 2, 3]) // [1, 2, 3]\n * castArrayIfDefined(42) // [42]\n * ```\n */\nexport function castArrayIfDefined<T>(value: NonNullable<T>): T[];\n/**\n * Converts the given value to an array if it's not already one, or returns an value as-is if it's not defined (i.e. `null` or `undefined`).\n * @note If the value is already an array, it is returned as-is (same reference).\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n * @example\n * ```ts\n * castArrayIfDefined([1, 2, 3]) // [1, 2, 3]\n * castArrayIfDefined(42) // [42]\n * ```\n */\nexport function castArrayIfDefined<T>(\n value: Maybe<\n Many<NonNullable<T>, 'mutable'> | Many<NonNullable<T>, 'immutable'>\n >\n): Maybe<T[]>;\n/**\n * Implementation for all overloads.\n * @param value The value to convert to an array if it's not already one.\n * @returns An array containing the input value, or the input value itself if it is already an array, or `null` or `undefined` if the input value is `null` or `undefined`.\n */\nexport function castArrayIfDefined<T>(\n value: Maybe<\n Many<NonNullable<T>, 'mutable'> | Many<NonNullable<T>, 'immutable'>\n >\n) {\n if (value == null) {\n return value;\n }\n\n if (isArray(value)) {\n return value;\n }\n\n return [value];\n}\n","import { Many, Maybe } from '../../types';\nimport { castArrayIfDefined } from '../castArrayIfDefined';\n\n// NOTE: all JSDocs here are duplicated of one another since there's no way to inherit them at this time.\n// see https://github.com/microsoft/TypeScript/issues/407 for more info.\n\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(value: null): T[];\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(value: undefined): T[];\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(value: readonly T[]): readonly T[];\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(value: T[]): T[];\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(\n value: Maybe<\n Many<NonNullable<T>, 'mutable'> | Many<NonNullable<T>, 'immutable'>\n >\n): T[];\n/**\n * Casts the input value to an array if it is not already an array.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(value: NonNullable<T>): T[];\n/**\n * Implementation for all overloads.\n * @param value The input value to cast to an array.\n * @returns An array containing the input value, or the input value itself if it is already an array.\n * @example\n * ```ts\n * castArray('foo'); // ['foo']\n * castArray(['foo']); // ['foo']\n * castArray(null); // []\n * castArray(undefined); // []\n * ```\n */\nexport function castArray<T>(\n value: Maybe<\n Many<NonNullable<T>, 'mutable'> | Many<NonNullable<T>, 'immutable'>\n >\n) {\n return castArrayIfDefined(value ?? []);\n}\n","/**\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 {\n CastToString,\n KeysOfUnion,\n Many,\n PropertyValueOfUnion,\n} from '../../../types';\nimport { castArray } from '../../castArray';\nimport { createTypeGuard } from '../../createTypeGuard';\n\nexport function createObjectPredicate<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(properties: Many<K>): ObjectPredicate<T>;\nexport function createObjectPredicate<T extends object>(\n predicate: ObjectPredicate<T>\n): ObjectPredicate<T>;\nexport function createObjectPredicate<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(propertiesOrPredicate: Many<K> | ObjectPredicate<T>): ObjectPredicate<T>;\nexport function createObjectPredicate<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(propertiesOrPredicate: Many<K> | ObjectPredicate<T>): ObjectPredicate<T> {\n return typeof propertiesOrPredicate === 'function'\n ? propertiesOrPredicate\n : createPropertiesPredicate(propertiesOrPredicate);\n}\n\nexport type ObjectPredicate<T extends object> = (\n value: Exclude<PropertyValueOfUnion<T, KeysOfUnion<T>>, undefined>,\n key: CastToString<KeysOfUnion<T>>,\n object: T\n) => boolean;\n\nfunction createPropertiesPredicate<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(properties: Many<K>): ObjectPredicate<T> {\n const isKnownProperty = createTypeGuard(castArray(properties));\n\n return (_value, key) => isKnownProperty(key);\n}\n","import { CastToString, KeysOfUnion } from '../../types';\n\nexport const objectEntries: ObjetEntries = Object.entries;\n\n/**\n * Returns an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop. (The only important difference is that a for...in loop enumerates properties in the prototype chain as well.)\n * Same as `Object.entries()` but returns a typed array.\n * @param object An object whose enumerable own property [key, value] pairs are to be returned.\n * @returns An array of the given object's own enumerable string-keyed property [key, value] pairs.\n */\ntype ObjetEntries = <T extends object>(\n object: T\n) => Array<[CastToString<KeysOfUnion<T>>, T[keyof T]]>;\n","import { KeysOfUnion, Maybe, PropertyValueOfUnion } from '../../../types';\nimport { objectEntries } from '../../objectEntries';\n\nimport { ObjectPredicate } from './createObjectPredicate';\n\nexport function filterObject<T extends object>(\n object: Maybe<T>,\n predicate: ObjectPredicate<T>\n) {\n if (object == null) {\n return {};\n }\n\n return Object.fromEntries(\n objectEntries(object).filter(([key, value]) =>\n predicate(\n value as Exclude<PropertyValueOfUnion<T, KeysOfUnion<T>>, undefined>,\n key,\n object\n )\n )\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 { KeysOfUnion, Many, Maybe } from '../../types';\nimport {\n createObjectPredicate,\n filterObject,\n ObjectPredicate,\n} from '../_internal/filterObject';\nimport { negate } from '../negate';\n\n/**\n * Returns a new object with all properties except the specified properties from the input object.\n * @param object The input object to omit properties from.\n * @param properties An array of property names to omit from the input object.\n * @returns A new object with all properties except the specified properties from the input object.\n */\nexport function omit<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(object: Maybe<T>, properties: Many<K>): Omit<T, K>;\n/**\n * Returns a new object with all properties except the properties that satisfy the predicate function from the input object.\n * @param object The input object to omit properties from.\n * @param predicate A function that takes a property value and its key and returns a boolean indicating whether to omit the property or not.\n * @returns A new object with all properties except the properties that satisfy the predicate function from the input object.\n */\nexport function omit<T extends object>(\n object: Maybe<T>,\n predicate: ObjectPredicate<T>\n): Partial<T>;\n/**\n * Implementation of the omit function.\n * @param object The input object to omit properties from.\n * @param propertiesOrPredicate Either an array of property names to omit from the input object, or a function that takes a property value and its key and returns a boolean indicating whether to omit the property or not.\n * @returns A new object with all properties except the specified properties from the input object.\n */\nexport function omit<\n T extends object,\n const K extends keyof T | KeysOfUnion<T>,\n>(object: T, propertiesOrPredicate: Many<K> | ObjectPredicate<T>) {\n return filterObject(\n object,\n negate(createObjectPredicate(propertiesOrPredicate))\n );\n}\n"]}