@ariesclark/number
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -1,7 +0,6 @@ | ||
export * from "./types"; | ||
export declare const EPSILON: number, MAX_SAFE_INTEGER: number, MAX_VALUE: number, MIN_SAFE_INTEGER: number, MIN_VALUE: number, NEGATIVE_INFINITY: number, NaN: number, POSITIVE_INFINITY: number, isFinite: (number: unknown) => boolean, isInteger: (number: unknown) => boolean, isNaN: (number: unknown) => boolean, isSafeInteger: (number: unknown) => boolean, parseFloat: (string: string) => number, parseInt: (string: string, radix?: number | undefined) => number; | ||
export * from "./methods/milliseconds"; | ||
export * from "./methods/clamp"; | ||
export * from "./methods/pseudo-random-float"; | ||
export * from "./methods/random-float"; | ||
export * from "./methods/random-function"; | ||
export * from "./methods/random-int"; | ||
export * from "./methods/timestamp"; |
@@ -1,9 +0,8 @@ | ||
export * from "./types"; | ||
export const { EPSILON, MAX_SAFE_INTEGER, MAX_VALUE, MIN_SAFE_INTEGER, MIN_VALUE, NEGATIVE_INFINITY, | ||
// eslint-disable-next-line no-shadow-restricted-names | ||
NaN, POSITIVE_INFINITY, isFinite, isInteger, isNaN, isSafeInteger, parseFloat, parseInt } = Number; | ||
export * from "./methods/milliseconds"; | ||
export * from "./methods/clamp"; | ||
export * from "./methods/pseudo-random-float"; | ||
export * from "./methods/random-float"; | ||
export * from "./methods/random-function"; | ||
export * from "./methods/random-int"; | ||
export * from "./methods/timestamp"; |
@@ -1,5 +0,13 @@ | ||
import type { RandomFunction } from "../types"; | ||
import type { RandomFunction } from "./random-function"; | ||
/** | ||
* Create a pseudo-random number generator from a seed. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { pseudoRandomFloat, randomFunction, randomInt } from "@ariesclark/number"; | ||
* | ||
* randomFunction(pseudoRandomFloat("hello world")); | ||
* randomInt(); // Predictable random number, based on the seed. | ||
* ``` | ||
*/ | ||
export declare function pseudoRandomFloat(seed: string): RandomFunction; |
@@ -36,2 +36,10 @@ /* eslint-disable unicorn/prefer-code-point */ | ||
* Create a pseudo-random number generator from a seed. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { pseudoRandomFloat, randomFunction, randomInt } from "@ariesclark/number"; | ||
* | ||
* randomFunction(pseudoRandomFloat("hello world")); | ||
* randomInt(); // Predictable random number, based on the seed. | ||
* ``` | ||
*/ | ||
@@ -38,0 +46,0 @@ export function pseudoRandomFloat(seed) { |
@@ -1,6 +0,8 @@ | ||
import type { RandomFunction } from "../types"; | ||
import type { RandomFunction } from "./random-function"; | ||
/** | ||
* A random number generator function that returns a float between 0 and 1 using {@link crypto.getRandomValues}. | ||
* A random number generator function that returns a float between 0 and 1. | ||
* Internally uses {@link crypto.getRandomValues} if available, or {@link Math.random} as a fallback on unsupported platforms. | ||
* | ||
* @returns A random float between 0 and 1. | ||
*/ | ||
export declare const randomFloat: RandomFunction; |
/** | ||
* A random number generator function that returns a float between 0 and 1 using {@link crypto.getRandomValues}. | ||
* A random number generator function that returns a float between 0 and 1. | ||
* Internally uses {@link crypto.getRandomValues} if available, or {@link Math.random} as a fallback on unsupported platforms. | ||
* | ||
* @returns A random float between 0 and 1. | ||
*/ | ||
export const randomFloat = () => { | ||
if (typeof crypto === "undefined" || !crypto.getRandomValues) | ||
return Math.random(); | ||
const [a] = crypto.getRandomValues(new Uint32Array(1)); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return a / Math.pow(2, 32); | ||
}; |
@@ -1,8 +0,17 @@ | ||
import type { RandomFunction } from "../types"; | ||
import { type RandomFunction } from "./random-function"; | ||
/** | ||
* Get a random value between `minimum` and `maximum`. | ||
* | ||
* @param minimum The minimum value, inclusive, defaults to `0`. | ||
* @param maximum The maximum value, inclusive, defaults to `100`. | ||
*/ | ||
export declare function randomInt(minimum?: number, maximum?: number, random?: RandomFunction): number; | ||
export declare function randomInt( | ||
/** | ||
* The minimum value, inclusive, defaults to `0` | ||
*/ | ||
minimum?: number, | ||
/** | ||
* The maximum value, inclusive, defaults to `100` | ||
*/ | ||
maximum?: number, | ||
/** | ||
* The random number generator function. | ||
*/ | ||
random?: RandomFunction): number; |
@@ -1,10 +0,19 @@ | ||
import { randomFloat } from "./random-float"; | ||
import { randomFunction } from "./random-function"; | ||
/** | ||
* Get a random value between `minimum` and `maximum`. | ||
* | ||
* @param minimum The minimum value, inclusive, defaults to `0`. | ||
* @param maximum The maximum value, inclusive, defaults to `100`. | ||
*/ | ||
export function randomInt(minimum = 0, maximum = 100, random = randomFloat) { | ||
export function randomInt( | ||
/** | ||
* The minimum value, inclusive, defaults to `0` | ||
*/ | ||
minimum = 0, | ||
/** | ||
* The maximum value, inclusive, defaults to `100` | ||
*/ | ||
maximum = 100, | ||
/** | ||
* The random number generator function. | ||
*/ | ||
random = randomFunction()) { | ||
return Math.floor(random() * (maximum - minimum + 1)) + minimum; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"sideEffects": false, | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Fast, efficient, and easy-to-use number extensions for TypeScript.", | ||
@@ -8,0 +8,0 @@ "files": [ |
110
README.md
@@ -49,2 +49,9 @@ <div align="center"> | ||
</a> | ||
<a href="https://npm.im/@ariesclark/time"> | ||
<img | ||
src="https://files.aries.fyi/2024/04/01/d668dcdee6a6b8ce.png" | ||
alt="@ariesclark/time" | ||
width="32%" | ||
/> | ||
</a> | ||
</div> | ||
@@ -64,8 +71,32 @@ | ||
### `MaybeSuffix` (type) | ||
### `clamp` (function) | ||
### `Milliseconds` (type) | ||
Clamps a number within the range specified by the minimum and maximum values. | ||
A duration in milliseconds. | ||
**Parameters:** | ||
- /\*\* | ||
* The value to clamp. | ||
\*/ | ||
value (`number`) | ||
- /\*\* | ||
* The minimum value, inclusive. | ||
\*/ | ||
minimum (`number`) | ||
- /\*\* | ||
* The maximum value, inclusive. | ||
\*/ | ||
maximum (`number`) | ||
**returns:** number | ||
### `randomFloat` (function) | ||
[object Object],[object Object],[object Object],[object Object],[object Object] | ||
### `RandomFunction` (type) | ||
@@ -75,36 +106,26 @@ | ||
### `Duration` (type) | ||
### `defaultRandom` (variable: RandomFunction) | ||
A duration value. | ||
The default random number generator function. | ||
Supports the following formats in both short and long form: | ||
### `randomFunction` (function) | ||
- `ms` (milliseconds) e.g. `20ms` | ||
- `s` (seconds) e.g. `1s` or `1 second` | ||
- `m` (minutes) e.g. `2m` or `2 minutes` | ||
- `h` (hours) e.g. `3h` or `3 hours` | ||
- `d` (days) e.g. `4d` or `4 days` | ||
- `w` (weeks) e.g. `5w` or `5 weeks` | ||
- `y` (years) e.g. `6y` or `6 years` | ||
Get the current random number generator function, or set a new one. | ||
### `milliseconds` (function) | ||
**Parameters:** | ||
[object Object],[object Object],[object Object] | ||
- newValue (`RandomFunction`) - The new random number generator function, or `undefined` to get the current value, or `null` to reset to the default. | ||
**Parameters:** | ||
**returns:** RandomFunction | ||
- value (`Duration`) | ||
```typescript | ||
import { randomFunction } from '@ariesclark/number/random-function'; | ||
import { randomInt } from '@ariesclark/number/random-int'; | ||
**returns:** number | ||
randomInt(); // Random number between 0 and 100. | ||
randomFunction(() => 0.5); | ||
```tsx | ||
ms('5m'); // 300000, 5 minutes in milliseconds, short form. | ||
ms(300000); // 300000, 5 minutes in milliseconds, pass-through. | ||
ms('2 hours'); // 7200000, 2 hours in milliseconds, long form. | ||
randomInt(); // Always 50. | ||
``` | ||
### `ms` (variable) | ||
[object Object],[object Object],[object Object] | ||
### `pseudoRandomFloat` (function) | ||
@@ -120,5 +141,12 @@ | ||
### `randomFloat` (function) | ||
```typescript | ||
import { | ||
pseudoRandomFloat, | ||
randomFunction, | ||
randomInt | ||
} from '@ariesclark/number'; | ||
[object Object],[object Object],[object Object] | ||
randomFunction(pseudoRandomFloat('hello world')); | ||
randomInt(); // Predictable random number, based on the seed. | ||
``` | ||
@@ -131,22 +159,20 @@ ### `randomInt` (function) | ||
- minimum (`number`) - The minimum value, inclusive, defaults to `0`. | ||
- maximum (`number`) - The maximum value, inclusive, defaults to `100`. | ||
- random (`RandomFunction`) | ||
- /\*\* | ||
**returns:** number | ||
* The minimum value, inclusive, defaults to `0` | ||
\*/ | ||
minimum (`number`) | ||
### `Timestamp` (type) | ||
- /\*\* | ||
[object Object],[object Object],[object Object],[object Object],[object Object] | ||
* The maximum value, inclusive, defaults to `100` | ||
\*/ | ||
maximum (`number`) | ||
### `InvalidTimestampError` (variable) | ||
- /\*\* | ||
### `timestamp` (function) | ||
* The random number generator function. | ||
\*/ | ||
random (`RandomFunction`) | ||
[object Object],[object Object],[object Object] | ||
**Parameters:** | ||
- timestamp (`Timestamp`) | ||
**returns:** number | ||
@@ -153,0 +179,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12205
237
181
16
1