📈 Returns the minimum and maximum values in an array.

Syntax

TypeScript
import { bounds } from '@opentf/std';

bounds<T>(
  arr: T[] = [],
  by: (val: T) => number | string = (x: T) => x as unknown as number | string
): [T, T] | null

Parameters

  • arr: The source array.

  • by: An optional function to extract the numeric or string value for comparison. Defaults to identity function.

Returns

An array containing the minimum and maximum values or null if the array is empty (after compacting).

Examples

TypeScript
bounds([]) //=> null

bounds([1, undefined, 2, null, 3]) //=> [1, 3]

bounds([1, 2, -3, 4, 5]) //=> [-3, 5]

bounds(['a', 'b', 'c', 'z', 'd']) //=> ['a', 'z']

bounds(['apple', 'mango', 'grapes']) //=> ['apple', 'mango']

const arr = [
  {
    name: 'x',
    age: 10,
  },
  {
    name: 'y',
    age: 16,
  },
  {
    name: 'z',
    age: 13,
  },
  { name: 'y2', age: 16 },
];
bounds(arr, (f) => f.age)
//=> 
// [
//   {
//     name: 'x',
//     age: 10,
//   },
//   {
//     name: 'y',
//     age: 16,
//   }
// ]
Last updated on
Edit this page