typedash
Version:
modern, type-safe collection of utility functions
1 lines • 4.93 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/hasKey/hasKey.ts","../../src/functions/isArray/isArray.ts","../../src/functions/get/get.ts"],"names":[],"mappings":";AAaO,SAAS,OAGd,OAAU,KAAyC;AACnD,MAAI,OAAO,UAAU,YAAY,SAAS,MAAM;AAC9C,WAAO;AAAA,EACT;AAEA,SAAO,OAAO;AAChB;;;ACnBO,IAAM,UAAmB,MAAM;;;ACoD/B,SAAS,IACd,QACA,MACc;AACd,QAAM,OAA0B,QAAQ,IAAI,IAAI,OAAO,KAAK,MAAM,GAAG;AAErE,SAAO,KAAK,OAAgB,CAAC,OAAO,QAAQ;AAC1C,QAAI,OAAO,OAAiB,GAAG,GAAG;AAChC,aAAQ,MAAsC,GAAG;AAAA,IACnD;AAGA,WAAO;AAAA,EACT,GAAG,MAAgB;AACrB","sourcesContent":["import { KeysOfUnion, StringWithAutocomplete } from '../../types';\n\n/**\n * Returns whether the input value has the specified key.\n * @param value The value to check.\n * @param key The key to check for.\n * @returns Whether the input value has the specified key.\n * @example\n * ```ts\n * hasKey({ a: 1 }, 'a') // true\n * hasKey({ a: 1 }, 'b') // false\n * ```\n */\nexport function hasKey<\n T extends object,\n K extends StringWithAutocomplete<KeysOfUnion<T> & string> | PropertyKey,\n>(value: T, key: K): value is T & Record<K, unknown> {\n if (typeof value !== 'object' || value == null) {\n return false;\n }\n\n return key in value;\n}\n","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 type { Get } from 'type-fest';\n\nimport { Many, ObjectPath } from '../../types';\nimport { hasKey } from '../hasKey';\nimport { isArray } from '../isArray';\n\n/**\n * Gets the value at path of object.\n * Allows accessing properties in unions of objects and getting undefined if the property is not present.\n * @param object The object to query.\n * @param path The path of the property to get.\n * @returns The value at path of object.\n * @example\n * ```ts\n * get({ a: 1, b: 2, c: 3 }, 'b') // 2\n * get({\n * name: \"John Doe\",\n * address: {\n * street: \"123 Main St\",\n * city: \"Anytown\",\n * }\n * }, 'address.city') // \"Anytown\"\n * ```\n */\nexport function get<T, Path extends ObjectPath<T>>(\n object: T,\n path: Path\n): Get<T, Path>;\n/**\n * Gets the value at path of object.\n * Allows accessing properties in unions of objects and getting undefined if the property is not present.\n * @param object The object to query.\n * @param path The path of the property to get.\n * @returns The value at path of object.\n * @example\n * ```ts\n * get({\n * name: \"John Doe\",\n * address: {\n * street: \"123 Main St\",\n * city: \"Anytown\",\n * }\n * }, ['address', 'city']) // \"Anytown\"\n * ```\n */\nexport function get<T, Path extends Many<string>>(\n object: T,\n path: Path\n): Get<T, Path>;\n/**\n * Implementation of all overloads.\n * @param object The object to query.\n * @param path The path of the property to get.\n * @returns The value at path of object.\n */\nexport function get<T, Path extends ObjectPath<T> | Many<string>>(\n object: T,\n path: Path\n): Get<T, Path> {\n const keys: readonly string[] = isArray(path) ? path : path.split('.');\n\n return keys.reduce<unknown>((value, key) => {\n if (hasKey(value as object, key)) {\n return (value as Record<typeof key, unknown>)[key];\n }\n\n // eslint-disable-next-line unicorn/no-useless-undefined -- default value, explicitly declare it\n return undefined;\n }, object as object) as Get<T, Path>;\n}\n"]}