UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 10.1 kB
{"version":3,"sources":["../../src/functions/isArray/isArray.ts","../../src/functions/castArrayIfDefined/castArrayIfDefined.ts","../../src/functions/castArray/castArray.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","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"]}