UNPKG

@web3r/flowerkit

Version:

Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).

1 lines 1.45 kB
{"version":3,"file":"index.mjs","sources":[""],"sourcesContent":["export type TGetMaxFromArrArgs = Parameters<typeof getMaxFromArr>;\r\n\r\nexport type TGetMaxFromArrReturn = ReturnType<typeof getMaxFromArr>;\r\n\r\n/**\r\n * Gets max number from an array of numbers\r\n * @param {number[]} arr Source array of numbers (must be non-empty)\r\n * @returns {number} Maximum value\r\n * @throws {TypeError} getMaxFromArr: expected a non-empty array of numbers\r\n * @example\r\n * // How to get max number from Array of numbers?\r\n * const arr = [ 100, 200, 300 ];\r\n * const max = getMaxFromArr(arr);\r\n * console.log(max); // 300\r\n */\r\nexport const getMaxFromArr = (arr: number[]): number => {\r\n const isValid = Array.isArray(arr)\r\n && arr.length > 0\r\n && arr.every((v) => typeof v === \"number\" && (Number.isFinite(v) || v === Infinity || v === -Infinity));\r\n if (!isValid) {\r\n throw new TypeError(\"getMaxFromArr: expected a non-empty array of numbers\");\r\n }\r\n return arr.reduce((p, v) => (p > v ? p : v));\r\n};\r\n"],"names":["getMaxFromArr","arr","isValid","Array","isArray","length","every","v","Number","isFinite","Infinity","TypeError","reduce","p"],"mappings":";;;;;;;;;;;AAeO,MAAMA,cAAiBC,MAC5B,MAAMC,QAAUC,MAAMC,QAAQH,MACzBA,IAAII,OAAS,GACbJ,IAAIK,MAAOC,UAAaA,IAAM,WAAaC,OAAOC,SAASF,IAAMA,IAAMG,UAAYH,KAAOG,WAC/F,IAAKR,QACH,MAAM,IAAIS,UAAU,wDAEtB,OAAOV,IAAIW,OAAO,CAACC,EAAGN,IAAOM,EAAIN,EAAIM,EAAIN"}