misc

utils. Namespace

misc

Description:
  • A collection of miscellaneous utility functions. Provides functions for clamping numbers, grouping arrays, and other common tasks. These functions help with data manipulation and organization, making it easier to work with collections of data.

Methods

(static) clamp(number, min, max) → {number}

Description:
  • Restricts a number to be within a specific range.
Examples
const clampedValue = misc.clamp(15, 10, 20);
console.log(clampedValue); // Outputs: 15
const clampedValue = misc.clamp(25, 10, 20);
console.log(clampedValue); // Outputs: 20
Parameters:
Name Type Description
number number The number to clamp.
min number The minimum boundary.
max number The maximum boundary.
Returns:
Type
number

(static) groupBy(array, keyOrFn) → {object}

Description:
  • Groups the elements of an array into an object based on a key or function.
Examples
const grouped = misc.groupBy([{ id: 1, category: 'A' }, { id: 2, category: 'B' }, { id: 3, category: 'A' }], 'category');
console.log(grouped);
// Outputs: { A: [{ id: 1, category: 'A' }, { id: 3, category: 'A' }], B: [{ id: 2, category: 'B' }] }
const grouped = misc.groupBy([{ id: 1, value: 10 }, { id: 2, value: 20 }, { id: 3, value: 10 }], item => item.value);
console.log(grouped);
// Outputs: { 10: [{ id: 1, value: 10 }, { id: 3, value: 10 }], 20: [{ id: 2, value: 20 }] }
Parameters:
Name Type Description
array Array.<object> The array to group.
keyOrFn string | function The key string or a function to determine the group.
Returns:
Type
object