UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 1.73 kB
{"version":3,"file":"min-D1hft82I.cjs","names":[],"sources":["../src/functions/min/min.ts"],"sourcesContent":["import type { Maybe } from '../../types';\n\n/**\n * Computes the maximum value of array. If array is empty or nil, `undefined` is returned.\n * @param array The array to iterate over.\n * @returns The maximum value in the array, or `undefined` if the array is empty or nil.\n */\nexport function min(array: Maybe<readonly number[]>): number;\n/**\n * Computes the minimum value of array. If array is empty or nil, `undefined` is returned.\n * @param array The array to iterate over.\n * @param valueExtractor An optional function used to extract a numeric value from each element.\n * @returns The element with the minimum value in the array according to the `valueExtractor`, or `undefined` if the array is empty or nil.\n */\nexport function min<T>(\n array: Maybe<readonly T[]>,\n valueExtractor: (value: T) => number\n): T | undefined;\n/**\n * Implementation for all overloads.\n * @param array The array to iterate over.\n * @param valueExtractor The function used to extract a numeric value from each element.\n * @returns The minimum value in the array, or `undefined` if the array is empty or nil.\n */\nexport function min<T>(\n array: Maybe<readonly T[]>,\n valueExtractor: (value: T) => number = (value) => value as unknown as number\n): T | number | undefined {\n if (array == null || array.length === 0) {\n return undefined;\n }\n\n return array.reduce((a, b) =>\n valueExtractor(a) < valueExtractor(b) ? a : b\n );\n}\n"],"mappings":";;;;;;;;AAwBA,SAAgB,IACd,OACA,kBAAwC,UAAU,OAC1B;AACxB,KAAI,SAAS,QAAQ,MAAM,WAAW,EACpC;AAGF,QAAO,MAAM,QAAQ,GAAG,MACtB,eAAe,EAAE,GAAG,eAAe,EAAE,GAAG,IAAI,EAC7C"}