UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

67 lines (66 loc) 1.87 kB
import type { Inclusiveness } from '../types.js'; export declare function _randomInt(minIncl: number, maxIncl: number): number; /** * Returns random item from an array. * Should be used on non-empty arrays! (otherwise will return undefined, * which is not reflected in the output type) */ export declare function _randomArrayItem<T>(array: T[]): T; /** * Convenience function to "throttle" some code - run it less often. * * @example * * if (_runLessOften(10)) { * // this code will run only 10% of the time * } */ export declare function _runLessOften(percent: number): boolean; /** * _isBetween(-10, 1, 5) // false * _isBetween(1, 1, 5) // true * _isBetween(3, 1, 5) // true * _isBetween(5, 1, 5) // false * _isBetween(7, 1, 5) // false * * Also works with strings: * _isBetween('2020-01-03', '2020-01-01', '2020-01-05') // true */ export declare function _isBetween<T extends number | string>(x: T, min: T, max: T, incl: Inclusiveness): boolean; export declare function _clamp(x: number, minIncl: number, maxIncl: number): number; /** * Same as .toFixed(), but conveniently casts the output to Number. * * @example * * _toFixed(1.2345, 2) * // 1.23 * * _toFixed(1.10, 2) * // 1.1 */ export declare function _toFixed(n: number, fractionDigits: number): number; /** * Same as .toPrecision(), but conveniently casts the output to Number. * * @example * * _toPrecision(1634.56, 1) * // 2000 * * _toPrecision(1634.56, 2) * // 1600 */ export declare function _toPrecision(n: number, precision: number): number; /** * @example * * _round(1634, 1000) // 2000 * _round(1634, 500) // 1500 * _round(1634, 100) // 1600 * _round(1634, 10) // 1630 * _round(1634, 1) // 1634 * _round(1634.5678, 0.1) // 1634.6 * _round(1634.5678, 0.01) // 1634.57 */ export declare function _round(n: number, precisionUnit: number): number;