Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 8x 8x 8x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 8x 1x 1x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x | import { purry } from './purry'; import { sortBy } from './sortBy'; /** * Returns a new array containing items that have maximum numeric values defined by `fn` function. * @param array - List of items * @param fn - Selector function * @signature * P.minBy(arrayOfNumbers) * @signature * P.minBy(array, fn) * @signature * P.minBy(fn)(array) * @signature * P.minBy()(arrayOfNumbers) * @example * P.minBy([1,2,3,4,5,6,7,7]) //=> [1] * P.minBy([{ data: 5, score: 2 }, { data: 6, score: 5 }], (q) => q.data * q.score) //=> [{ data: 5, score: 2 }] * @category Number, Pipe */ export function minBy(array: readonly number[]): number[]; export function minBy<T>(array: readonly T[], fn: (item: T) => number): T[]; export function minBy(): (array: readonly number[]) => number[]; export function minBy<T>(fn: (item: T) => number): (array: readonly T[]) => T[]; export function minBy() { return purry(_minBy, arguments); } function _minBy<T>(array: readonly T[], fn: (item: T) => number): T[] { const items = sortBy(array, fn); const minArr: T[] = []; let minV: number = 0; for (const i of items) { if (minArr.length === 0) { minArr.push(i); minV = fn(i); continue; } Iif (minV === fn(i)) { minArr.push(i); continue; } break; } return minArr; } /** * Returns a new array containing items that have maximum numeric values defined by `fn` function. * @param array - List of items * @param fn - Selector function * @signature * P.maxBy(arrayOfNumbers) * @signature * P.maxBy(array, fn) * @signature * P.maxBy(fn)(array) * @signature * P.maxBy()(arrayOfNumbers) * @example * P.maxBy([1,2,3,4,5,6,7,7]) //=> [7] * P.maxBy([{ data: 5, score: 2 }, { data: 6, score: 5 }], (q) => q.data * q.score) //=> [{ data: 6, score: 5 }] * @category Number */ export function maxBy(array: readonly number[]): number[]; export function maxBy<T>(array: readonly T[], fn: (item: T) => number): T[]; export function maxBy<T>(fn: (item: T) => number): (array: readonly T[]) => T[]; export function maxBy(): (array: readonly number[]) => number[]; export function maxBy() { return purry(_maxBy, arguments); } function _maxBy<T>(array: readonly T[], fn?: (item: T) => number): T[] { const def = fn || ((q: T) => Number(q)); const items = sortBy(array, q => -1 * def(q)); const minArr: T[] = []; let maxV: number = 0; for (const i of items) { if (minArr.length === 0) { minArr.push(i); maxV = def(i); continue; } Iif (maxV === def(i)) { minArr.push(i); continue; } break; } return minArr; } |