@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
88 lines (87 loc) • 2.65 kB
TypeScript
/**
* Functions for dealing with numbers.
* @module
*/
/** Returns `true` if the given value is a float number, `false` otherwise. */
export declare function isFloat(value: unknown): boolean;
/**
* Returns `true` if the given value is a numeric value, `false` otherwise. A numeric value is a
* number, a bigint, or a string that can be converted to a number or bigint.
*
* **NOTE:** `NaN` is not considered numeric.
*
* @param strict Only returns `true` when the value is of type `number`.
*
* @example
* ```ts
* import { isNumeric } from "@ayonli/jsext/number";
*
* console.log(isNumeric(42)); // true
* console.log(isNumeric(42n)); // true
* console.log(isNumeric("42")); // true
*
* console.log(isNumeric(NaN)); // false
* console.log(isNumeric(42n, true)); // false
* console.log(isNumeric("42", true)); // false
* ```
*/
export declare function isNumeric(value: unknown, strict?: boolean): boolean;
/**
* Return `true` if a number is between the given range (inclusive).
*
* This function is the same as `value >= min && value <= max`.
*/
export declare function isBetween(value: number, [min, max]: [number, number]): boolean;
/**
* Returns a random integer ranged from `min` to `max` (inclusive).
*
* @example
* ```ts
* import { random } from "@ayonli/jsext/number";
*
* console.log(random(1, 5)); // 1, 2, 3, 4, or 5
* ```
*/
export declare function random(min: number, max: number): number;
/**
* Generates a sequence of numbers from `min` to `max` (inclusive).
*
* @example
* ```ts
* import { range } from "@ayonli/jsext/number";
*
* for (const i of range(1, 5)) {
* console.log(i);
* }
* // output:
* // 1
* // 2
* // 3
* // 4
* // 5
* ```
*/
export declare function range(min: number, max: number, step?: number): Generator<number, void, unknown>;
/**
* Creates a generator that produces sequential numbers from `1` to
* `Number.MAX_SAFE_INTEGER`, useful for generating unique IDs.
*
* @param loop Repeat the sequence when the end is reached.
*
* @example
* ```ts
* import { serial } from "@ayonli/jsext/number";
*
* const idGenerator = serial();
*
* console.log(idGenerator.next().value); // 1
* console.log(idGenerator.next().value); // 2
* console.log(idGenerator.next().value); // 3
* ```
*/
export declare function serial(loop?: boolean): Generator<number, void, unknown>;
/**
* Creates a generator that produces sequential numbers from `min` to `max` (inclusive).
* @deprecated use {@link range} and {@link serial} instead.
*/
export declare function sequence(min: number, max: number, step?: number, loop?: boolean): Generator<number, void, unknown>;