UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 12.6 kB
{"version":3,"sources":["../../src/functions/isArray/isArray.ts","../../src/functions/castArrayIfDefined/castArrayIfDefined.ts","../../src/functions/castArray/castArray.ts","../../src/functions/orderBy/orderBy.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;;;AC9FO,SAAS,QACd,OACA,WACA,QACU;AACV,MAAI,SAAS;AAAM,WAAO,CAAC;AAE3B,QAAM,sBAAsB,UAAU,SAAS,EAAE;AAAA,IAAI,CAAC,aACpD,OAAO,aAAa,aAChB,WACA,CAAC,UAAkB,MAAM,QAAQ;AAAA,EACvC;AAEA,QAAM,mBAAmB,UAAU,MAAM;AAEzC,SAAO,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,GAAG,MAAM;AAC/B,eAAW,CAAC,OAAO,QAAQ,KAAK,oBAAoB,QAAQ,GAAG;AAC7D,YAAM,kBAAkB,iBAAiB,KAAK,KAAK;AACnD,YAAM,QAAQ,oBAAoB,SAAS,KAAK;AAEhD,YAAM,SAAS,SAAS,CAAC;AACzB,YAAM,SAAS,SAAS,CAAC;AAGzB,UAAI,SAAU;AAAS,eAAO,KAAK;AACnC,UAAI,SAAU;AAAS,eAAO,IAAI;AAAA,IAEpC;AAEA,WAAO;AAAA,EACT,CAAC;AACH","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","import type { Primitive } from 'type-fest';\n\nimport { KeysOfUnion, Many, Maybe } from '../../types';\nimport { castArray } from '../castArray';\n\n/**\n * Sorts an array of objects by one or more properties, in ascending or descending order.\n * @param array The array of objects to sort.\n * @param iterators The property or properties to sort by. Can be a key of `TValue` or a function that returns a comparable value.\n * @param orders The order or orders to sort by. Can be \"asc\" or \"desc\". Defaults to \"asc\".\n * @returns A new array of objects sorted by the specified properties and orders.\n */\nexport function orderBy<TValue>(\n array: Maybe<readonly TValue[]>,\n iterators: Many<OrderByIterator<TValue>>,\n orders?: Many<Order>\n): TValue[] {\n if (array == null) return [];\n\n const normalizedIteratees = castArray(iterators).map((iteratee) =>\n typeof iteratee === 'function'\n ? iteratee\n : (value: TValue) => value[iteratee]\n );\n\n const normalizedOrders = castArray(orders);\n\n return [...array].sort((a, b) => {\n for (const [index, iteratee] of normalizedIteratees.entries()) {\n const normalizedOrder = normalizedOrders[index] ?? 'asc';\n const order = normalizedOrder === 'desc' ? -1 : 1;\n\n const aValue = iteratee(a);\n const bValue = iteratee(b);\n\n /* eslint-disable @typescript-eslint/no-non-null-assertion -- JS can compare null and undefined using `<` and */\n if (aValue! < bValue!) return -1 * order;\n if (aValue! > bValue!) return 1 * order;\n /* eslint-enable @typescript-eslint/no-non-null-assertion */\n }\n\n return 0;\n });\n}\n\ntype Order = 'asc' | 'desc';\n\ntype OrderByIterator<TValue> =\n | ((value: TValue) => ComparableValue)\n | KeysOfUnion<TValue>;\n\ntype ComparableValue =\n | Exclude<Primitive, symbol>\n | { [Symbol.toPrimitive](): Primitive }\n | { valueOf(): Primitive }\n | { toString(): string }\n | { [Symbol.toStringTag]: string };\n"]}