@helpers4/number 2.0.0-beta.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/lib/index.d.ts +107 -2
- package/lib/index.js +105 -2
- package/lib/index.js.map +1 -1
- package/llms.txt +170 -2
- package/meta/api.json +180 -2
- package/meta/examples.json +55 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -13,7 +13,11 @@ A set of helpers for working with URLs.
|
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
15
|
- clamp
|
|
16
|
+
- formatCompact
|
|
16
17
|
- formatSize
|
|
18
|
+
- inRange
|
|
19
|
+
- lerp
|
|
20
|
+
- mean
|
|
17
21
|
- random
|
|
18
22
|
- roundTo
|
|
19
23
|
- sum
|
|
@@ -23,10 +27,12 @@ A set of helpers for working with URLs.
|
|
|
23
27
|
|
|
24
28
|
<!-- AUTOMATIC-SIBLINGS -->
|
|
25
29
|
- [array](../array)
|
|
30
|
+
- [ci](../ci)
|
|
26
31
|
- [commit](../commit)
|
|
27
32
|
- [date](../date)
|
|
28
33
|
- [function](../function)
|
|
29
|
-
- [
|
|
34
|
+
- [id](../id)
|
|
35
|
+
- [markdown](../markdown)
|
|
30
36
|
- [object](../object)
|
|
31
37
|
- [observable](../observable)
|
|
32
38
|
- [promise](../promise)
|
|
@@ -42,10 +48,12 @@ A set of helpers for working with URLs.
|
|
|
42
48
|
| Name | Package | Source Code | Description |
|
|
43
49
|
|------|---------|-------------|-------------|
|
|
44
50
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
51
|
+
| ci | [@helpers4/ci](https://www.npmjs.com/package/@helpers4/ci) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/ci) | CI/CD workflow status utilities |
|
|
45
52
|
| commit | [@helpers4/commit](https://www.npmjs.com/package/@helpers4/commit) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/commit) | Conventional Commits parsing and analysis |
|
|
46
53
|
| date | [@helpers4/date](https://www.npmjs.com/package/@helpers4/date) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/date) | Date and time utility functions |
|
|
47
54
|
| function | [@helpers4/function](https://www.npmjs.com/package/@helpers4/function) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/function) | Function utilities and type guards |
|
|
48
|
-
|
|
|
55
|
+
| id | [@helpers4/id](https://www.npmjs.com/package/@helpers4/id) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/id) | Unique identifier generation utilities |
|
|
56
|
+
| markdown | [@helpers4/markdown](https://www.npmjs.com/package/@helpers4/markdown) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/markdown) | Markdown formatting and escaping utilities |
|
|
49
57
|
| number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | Number utility functions and validation |
|
|
50
58
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
51
59
|
| observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
|
package/lib/index.d.ts
CHANGED
|
@@ -13,6 +13,29 @@
|
|
|
13
13
|
*/
|
|
14
14
|
declare function clamp(value: number, min: number, max: number): number;
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* This file is part of helpers4.
|
|
18
|
+
* Copyright (C) 2025 baxyz
|
|
19
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Formats a number using compact notation (e.g. `1_500_000 → "1.5M"`).
|
|
23
|
+
*
|
|
24
|
+
* Thin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion
|
|
25
|
+
* of `formatSize` in the same `format*` family.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The number to format.
|
|
28
|
+
* @param locale - BCP 47 locale tag. Defaults to the runtime locale.
|
|
29
|
+
* @returns A compact string representation of the number.
|
|
30
|
+
* @example
|
|
31
|
+
* formatCompact(1_500_000); // '1.5M' (en-US locale)
|
|
32
|
+
* formatCompact(1_000); // '1K'
|
|
33
|
+
* formatCompact(999); // '999'
|
|
34
|
+
* formatCompact(1_500_000, 'fr'); // '1,5 M'
|
|
35
|
+
* @since 2.0.0
|
|
36
|
+
*/
|
|
37
|
+
declare function formatCompact(value: number, locale?: string): string;
|
|
38
|
+
|
|
16
39
|
/**
|
|
17
40
|
* This file is part of helpers4.
|
|
18
41
|
* Copyright (C) 2025 baxyz
|
|
@@ -33,10 +56,91 @@ declare function clamp(value: number, min: number, max: number): number;
|
|
|
33
56
|
* formatSize(1536) // '1.5KB'
|
|
34
57
|
* formatSize(1_048_576) // '1.0MB'
|
|
35
58
|
* formatSize(1_073_741_824) // '1.0GB'
|
|
36
|
-
* @since
|
|
59
|
+
* @since 2.0.0
|
|
37
60
|
*/
|
|
38
61
|
declare function formatSize(bytes: number): string;
|
|
39
62
|
|
|
63
|
+
/**
|
|
64
|
+
* This file is part of helpers4.
|
|
65
|
+
* Copyright (C) 2025 baxyz
|
|
66
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Options for {@link inRange}.
|
|
70
|
+
*/
|
|
71
|
+
interface InRangeOptions {
|
|
72
|
+
/**
|
|
73
|
+
* Which boundary (or boundaries) are included in the range.
|
|
74
|
+
* - `'both'` (default): `min <= value <= max`
|
|
75
|
+
* - `'min'`: `min <= value < max`
|
|
76
|
+
* - `'max'`: `min < value <= max`
|
|
77
|
+
* - `'none'`: `min < value < max`
|
|
78
|
+
*/
|
|
79
|
+
inclusive?: 'both' | 'min' | 'max' | 'none';
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Checks whether a number falls within `[min, max]` (both inclusive by default).
|
|
83
|
+
*
|
|
84
|
+
* @param value - The number to test
|
|
85
|
+
* @param min - Lower bound
|
|
86
|
+
* @param max - Upper bound
|
|
87
|
+
* @param options - Boundary inclusion mode (default: `'both'`)
|
|
88
|
+
* @returns `true` if `value` is within the specified range
|
|
89
|
+
* @example
|
|
90
|
+
* inRange(5, 1, 10) // => true
|
|
91
|
+
* inRange(1, 1, 10) // => true (min inclusive)
|
|
92
|
+
* inRange(1, 1, 10, { inclusive: 'none' }) // => false
|
|
93
|
+
* inRange(10, 1, 10, { inclusive: 'max' }) // => true
|
|
94
|
+
* @since 2.0.0
|
|
95
|
+
*/
|
|
96
|
+
declare function inRange(value: number, min: number, max: number, options?: InRangeOptions): boolean;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* This file is part of helpers4.
|
|
100
|
+
* Copyright (C) 2025 baxyz
|
|
101
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
104
|
+
* Linearly interpolates between `start` and `end` by the factor `t`.
|
|
105
|
+
*
|
|
106
|
+
* - `t = 0` returns `start`.
|
|
107
|
+
* - `t = 1` returns `end`.
|
|
108
|
+
* - Values of `t` outside `[0, 1]` extrapolate beyond the range.
|
|
109
|
+
*
|
|
110
|
+
* @param start - The start value.
|
|
111
|
+
* @param end - The end value.
|
|
112
|
+
* @param t - The interpolation factor.
|
|
113
|
+
* @returns The interpolated value.
|
|
114
|
+
* @example
|
|
115
|
+
* lerp(0, 100, 0); // 0
|
|
116
|
+
* lerp(0, 100, 1); // 100
|
|
117
|
+
* lerp(0, 100, 0.5); // 50
|
|
118
|
+
* lerp(0, 100, 0.25); // 25
|
|
119
|
+
* @since 2.0.0
|
|
120
|
+
*/
|
|
121
|
+
declare function lerp(start: number, end: number, t: number): number;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* This file is part of helpers4.
|
|
125
|
+
* Copyright (C) 2025 baxyz
|
|
126
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
127
|
+
*/
|
|
128
|
+
/**
|
|
129
|
+
* Calculates the arithmetic mean (average) of an array of numbers.
|
|
130
|
+
* Returns `NaN` for an empty array.
|
|
131
|
+
*
|
|
132
|
+
* Pairs with {@link sum} for aggregate operations.
|
|
133
|
+
*
|
|
134
|
+
* @param array - The array of numbers to average
|
|
135
|
+
* @returns The arithmetic mean, or `NaN` if the array is empty
|
|
136
|
+
* @example
|
|
137
|
+
* mean([1, 2, 3, 4]) // => 2.5
|
|
138
|
+
* mean([10]) // => 10
|
|
139
|
+
* mean([]) // => NaN
|
|
140
|
+
* @since 2.0.0
|
|
141
|
+
*/
|
|
142
|
+
declare function mean(array: readonly number[]): number;
|
|
143
|
+
|
|
40
144
|
/**
|
|
41
145
|
* This file is part of helpers4.
|
|
42
146
|
* Copyright (C) 2025 baxyz
|
|
@@ -89,4 +193,5 @@ declare function roundTo(value: number, decimals: number): number;
|
|
|
89
193
|
*/
|
|
90
194
|
declare function sum(array: readonly number[]): number;
|
|
91
195
|
|
|
92
|
-
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
|
196
|
+
export { clamp, formatCompact, formatSize, inRange, lerp, mean, randomBetween, randomIntBetween, roundTo, sum };
|
|
197
|
+
export type { InRangeOptions };
|
package/lib/index.js
CHANGED
|
@@ -16,6 +16,32 @@ function clamp(value, min, max) {
|
|
|
16
16
|
return Math.min(Math.max(value, min), max);
|
|
17
17
|
}
|
|
18
18
|
//#endregion
|
|
19
|
+
//#region helpers/number/formatCompact.ts
|
|
20
|
+
/**
|
|
21
|
+
* This file is part of helpers4.
|
|
22
|
+
* Copyright (C) 2025 baxyz
|
|
23
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Formats a number using compact notation (e.g. `1_500_000 → "1.5M"`).
|
|
27
|
+
*
|
|
28
|
+
* Thin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion
|
|
29
|
+
* of `formatSize` in the same `format*` family.
|
|
30
|
+
*
|
|
31
|
+
* @param value - The number to format.
|
|
32
|
+
* @param locale - BCP 47 locale tag. Defaults to the runtime locale.
|
|
33
|
+
* @returns A compact string representation of the number.
|
|
34
|
+
* @example
|
|
35
|
+
* formatCompact(1_500_000); // '1.5M' (en-US locale)
|
|
36
|
+
* formatCompact(1_000); // '1K'
|
|
37
|
+
* formatCompact(999); // '999'
|
|
38
|
+
* formatCompact(1_500_000, 'fr'); // '1,5 M'
|
|
39
|
+
* @since 2.0.0
|
|
40
|
+
*/
|
|
41
|
+
function formatCompact(value, locale) {
|
|
42
|
+
return new Intl.NumberFormat(locale, { notation: "compact" }).format(value);
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
19
45
|
//#region helpers/number/formatSize.ts
|
|
20
46
|
/**
|
|
21
47
|
* This file is part of helpers4.
|
|
@@ -45,7 +71,7 @@ var UNITS = [
|
|
|
45
71
|
* formatSize(1536) // '1.5KB'
|
|
46
72
|
* formatSize(1_048_576) // '1.0MB'
|
|
47
73
|
* formatSize(1_073_741_824) // '1.0GB'
|
|
48
|
-
* @since
|
|
74
|
+
* @since 2.0.0
|
|
49
75
|
*/
|
|
50
76
|
function formatSize(bytes) {
|
|
51
77
|
let size = bytes;
|
|
@@ -57,6 +83,83 @@ function formatSize(bytes) {
|
|
|
57
83
|
return `${size.toFixed(1)}${UNITS[unitIndex]}`;
|
|
58
84
|
}
|
|
59
85
|
//#endregion
|
|
86
|
+
//#region helpers/number/inRange.ts
|
|
87
|
+
/**
|
|
88
|
+
* Checks whether a number falls within `[min, max]` (both inclusive by default).
|
|
89
|
+
*
|
|
90
|
+
* @param value - The number to test
|
|
91
|
+
* @param min - Lower bound
|
|
92
|
+
* @param max - Upper bound
|
|
93
|
+
* @param options - Boundary inclusion mode (default: `'both'`)
|
|
94
|
+
* @returns `true` if `value` is within the specified range
|
|
95
|
+
* @example
|
|
96
|
+
* inRange(5, 1, 10) // => true
|
|
97
|
+
* inRange(1, 1, 10) // => true (min inclusive)
|
|
98
|
+
* inRange(1, 1, 10, { inclusive: 'none' }) // => false
|
|
99
|
+
* inRange(10, 1, 10, { inclusive: 'max' }) // => true
|
|
100
|
+
* @since 2.0.0
|
|
101
|
+
*/
|
|
102
|
+
function inRange(value, min, max, options = {}) {
|
|
103
|
+
const { inclusive = "both" } = options;
|
|
104
|
+
return (inclusive === "both" || inclusive === "min" ? value >= min : value > min) && (inclusive === "both" || inclusive === "max" ? value <= max : value < max);
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region helpers/number/lerp.ts
|
|
108
|
+
/**
|
|
109
|
+
* This file is part of helpers4.
|
|
110
|
+
* Copyright (C) 2025 baxyz
|
|
111
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Linearly interpolates between `start` and `end` by the factor `t`.
|
|
115
|
+
*
|
|
116
|
+
* - `t = 0` returns `start`.
|
|
117
|
+
* - `t = 1` returns `end`.
|
|
118
|
+
* - Values of `t` outside `[0, 1]` extrapolate beyond the range.
|
|
119
|
+
*
|
|
120
|
+
* @param start - The start value.
|
|
121
|
+
* @param end - The end value.
|
|
122
|
+
* @param t - The interpolation factor.
|
|
123
|
+
* @returns The interpolated value.
|
|
124
|
+
* @example
|
|
125
|
+
* lerp(0, 100, 0); // 0
|
|
126
|
+
* lerp(0, 100, 1); // 100
|
|
127
|
+
* lerp(0, 100, 0.5); // 50
|
|
128
|
+
* lerp(0, 100, 0.25); // 25
|
|
129
|
+
* @since 2.0.0
|
|
130
|
+
*/
|
|
131
|
+
function lerp(start, end, t) {
|
|
132
|
+
if (t === 0) return start;
|
|
133
|
+
if (t === 1) return end;
|
|
134
|
+
if (Object.is(start, end)) return start;
|
|
135
|
+
return start + (end - start) * t;
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region helpers/number/mean.ts
|
|
139
|
+
/**
|
|
140
|
+
* This file is part of helpers4.
|
|
141
|
+
* Copyright (C) 2025 baxyz
|
|
142
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Calculates the arithmetic mean (average) of an array of numbers.
|
|
146
|
+
* Returns `NaN` for an empty array.
|
|
147
|
+
*
|
|
148
|
+
* Pairs with {@link sum} for aggregate operations.
|
|
149
|
+
*
|
|
150
|
+
* @param array - The array of numbers to average
|
|
151
|
+
* @returns The arithmetic mean, or `NaN` if the array is empty
|
|
152
|
+
* @example
|
|
153
|
+
* mean([1, 2, 3, 4]) // => 2.5
|
|
154
|
+
* mean([10]) // => 10
|
|
155
|
+
* mean([]) // => NaN
|
|
156
|
+
* @since 2.0.0
|
|
157
|
+
*/
|
|
158
|
+
function mean(array) {
|
|
159
|
+
if (array.length === 0) return NaN;
|
|
160
|
+
return array.reduce((acc, n) => acc + n, 0) / array.length;
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
60
163
|
//#region helpers/number/random.ts
|
|
61
164
|
/**
|
|
62
165
|
* This file is part of helpers4.
|
|
@@ -121,6 +224,6 @@ function sum(array) {
|
|
|
121
224
|
return array.reduce((acc, n) => acc + n, 0);
|
|
122
225
|
}
|
|
123
226
|
//#endregion
|
|
124
|
-
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
|
227
|
+
export { clamp, formatCompact, formatSize, inRange, lerp, mean, randomBetween, randomIntBetween, roundTo, sum };
|
|
125
228
|
|
|
126
229
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/number/clamp.ts","../../../helpers/number/formatSize.ts","../../../helpers/number/random.ts","../../../helpers/number/roundTo.ts","../../../helpers/number/sum.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Clamps a number between min and max values\n * @param value - The value to clamp\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Clamped value\n * @since 1.9.0\n */\nexport function clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/** Size units in ascending order. */\nconst UNITS = ['B', 'KB', 'MB', 'GB', 'TB'] as const;\n\n/**\n * Format a byte count into a human-readable string with the appropriate unit.\n *\n * Each unit is 1024 of the previous (binary prefix). The result is formatted\n * with one decimal place.\n *\n * @param bytes - A non-negative integer representing a byte count.\n * @returns A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`.\n * @example\n * formatSize(0) // '0.0B'\n * formatSize(512) // '512.0B'\n * formatSize(1024) // '1.0KB'\n * formatSize(1536) // '1.5KB'\n * formatSize(1_048_576) // '1.0MB'\n * formatSize(1_073_741_824) // '1.0GB'\n * @since
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../helpers/number/clamp.ts","../../../helpers/number/formatCompact.ts","../../../helpers/number/formatSize.ts","../../../helpers/number/inRange.ts","../../../helpers/number/lerp.ts","../../../helpers/number/mean.ts","../../../helpers/number/random.ts","../../../helpers/number/roundTo.ts","../../../helpers/number/sum.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Clamps a number between min and max values\n * @param value - The value to clamp\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Clamped value\n * @since 1.9.0\n */\nexport function clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Formats a number using compact notation (e.g. `1_500_000 → \"1.5M\"`).\n *\n * Thin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion\n * of `formatSize` in the same `format*` family.\n *\n * @param value - The number to format.\n * @param locale - BCP 47 locale tag. Defaults to the runtime locale.\n * @returns A compact string representation of the number.\n * @example\n * formatCompact(1_500_000); // '1.5M' (en-US locale)\n * formatCompact(1_000); // '1K'\n * formatCompact(999); // '999'\n * formatCompact(1_500_000, 'fr'); // '1,5 M'\n * @since 2.0.0\n */\nexport function formatCompact(value: number, locale?: string): string {\n return new Intl.NumberFormat(locale, { notation: 'compact' }).format(value);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/** Size units in ascending order. */\nconst UNITS = ['B', 'KB', 'MB', 'GB', 'TB'] as const;\n\n/**\n * Format a byte count into a human-readable string with the appropriate unit.\n *\n * Each unit is 1024 of the previous (binary prefix). The result is formatted\n * with one decimal place.\n *\n * @param bytes - A non-negative integer representing a byte count.\n * @returns A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`.\n * @example\n * formatSize(0) // '0.0B'\n * formatSize(512) // '512.0B'\n * formatSize(1024) // '1.0KB'\n * formatSize(1536) // '1.5KB'\n * formatSize(1_048_576) // '1.0MB'\n * formatSize(1_073_741_824) // '1.0GB'\n * @since 2.0.0\n */\nexport function formatSize(bytes: number): string {\n let size = bytes;\n let unitIndex = 0;\n\n while (size >= 1024 && unitIndex < UNITS.length - 1) {\n size /= 1024;\n unitIndex++;\n }\n\n return `${size.toFixed(1)}${UNITS[unitIndex]}`;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Options for {@link inRange}.\n */\nexport interface InRangeOptions {\n /**\n * Which boundary (or boundaries) are included in the range.\n * - `'both'` (default): `min <= value <= max`\n * - `'min'`: `min <= value < max`\n * - `'max'`: `min < value <= max`\n * - `'none'`: `min < value < max`\n */\n inclusive?: 'both' | 'min' | 'max' | 'none';\n}\n\n/**\n * Checks whether a number falls within `[min, max]` (both inclusive by default).\n *\n * @param value - The number to test\n * @param min - Lower bound\n * @param max - Upper bound\n * @param options - Boundary inclusion mode (default: `'both'`)\n * @returns `true` if `value` is within the specified range\n * @example\n * inRange(5, 1, 10) // => true\n * inRange(1, 1, 10) // => true (min inclusive)\n * inRange(1, 1, 10, { inclusive: 'none' }) // => false\n * inRange(10, 1, 10, { inclusive: 'max' }) // => true\n * @since 2.0.0\n */\nexport function inRange(value: number, min: number, max: number, options: InRangeOptions = {}): boolean {\n const { inclusive = 'both' } = options;\n const minOk = inclusive === 'both' || inclusive === 'min' ? value >= min : value > min;\n const maxOk = inclusive === 'both' || inclusive === 'max' ? value <= max : value < max;\n return minOk && maxOk;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Linearly interpolates between `start` and `end` by the factor `t`.\n *\n * - `t = 0` returns `start`.\n * - `t = 1` returns `end`.\n * - Values of `t` outside `[0, 1]` extrapolate beyond the range.\n *\n * @param start - The start value.\n * @param end - The end value.\n * @param t - The interpolation factor.\n * @returns The interpolated value.\n * @example\n * lerp(0, 100, 0); // 0\n * lerp(0, 100, 1); // 100\n * lerp(0, 100, 0.5); // 50\n * lerp(0, 100, 0.25); // 25\n * @since 2.0.0\n */\nexport function lerp(start: number, end: number, t: number): number {\n if (t === 0) return start;\n if (t === 1) return end;\n if (Object.is(start, end)) return start;\n return start + (end - start) * t;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Calculates the arithmetic mean (average) of an array of numbers.\n * Returns `NaN` for an empty array.\n *\n * Pairs with {@link sum} for aggregate operations.\n *\n * @param array - The array of numbers to average\n * @returns The arithmetic mean, or `NaN` if the array is empty\n * @example\n * mean([1, 2, 3, 4]) // => 2.5\n * mean([10]) // => 10\n * mean([]) // => NaN\n * @since 2.0.0\n */\nexport function mean(array: readonly number[]): number {\n if (array.length === 0) return NaN;\n return array.reduce((acc, n) => acc + n, 0) / array.length;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Generates a random number between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random number between min and max\n * @since 1.9.0\n */\nexport function randomBetween(min: number, max: number): number {\n return Math.random() * (max - min) + min;\n}\n\n/**\n * Generates a random integer between min and max (inclusive)\n * @param min - Minimum value\n * @param max - Maximum value\n * @returns Random integer between min and max\n * @since 1.9.0\n */\nexport function randomIntBetween(min: number, max: number): number {\n return Math.floor(Math.random() * (max - min + 1)) + min;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Rounds a number to specified decimal places\n * @param value - The number to round\n * @param decimals - Number of decimal places\n * @returns Rounded number\n * @since 1.9.0\n */\nexport function roundTo(value: number, decimals: number): number {\n const multiplier = Math.pow(10, decimals);\n return Math.round(value * multiplier) / multiplier;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Calculates the sum of an array of numbers.\n * @param array - The array of numbers to sum\n * @returns The sum of all values\n * @example\n * sum([1, 2, 3, 4])\n * // => 10\n * @since 2.0.0\n */\nexport function sum(array: readonly number[]): number {\n return array.reduce((acc, n) => acc + n, 0);\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,MAAM,OAAe,KAAa,KAAqB;CACrE,OAAO,KAAK,IAAI,KAAK,IAAI,OAAO,GAAG,GAAG,GAAG;AAC3C;;;;;;;;;;;;;;;;;;;;;;;;ACMA,SAAgB,cAAc,OAAe,QAAyB;CACpE,OAAO,IAAI,KAAK,aAAa,QAAQ,EAAE,UAAU,UAAU,CAAC,EAAE,OAAO,KAAK;AAC5E;;;;;;;;;ACjBA,IAAM,QAAQ;CAAC;CAAK;CAAM;CAAM;CAAM;AAAI;;;;;;;;;;;;;;;;;;AAmB1C,SAAgB,WAAW,OAAuB;CAChD,IAAI,OAAO;CACX,IAAI,YAAY;CAEhB,OAAO,QAAQ,QAAQ,YAAY,MAAM,SAAS,GAAG;EACnD,QAAQ;EACR;CACF;CAEA,OAAO,GAAG,KAAK,QAAQ,CAAC,IAAI,MAAM;AACpC;;;;;;;;;;;;;;;;;;ACDA,SAAgB,QAAQ,OAAe,KAAa,KAAa,UAA0B,CAAC,GAAY;CACtG,MAAM,EAAE,YAAY,WAAW;CAG/B,QAFc,cAAc,UAAU,cAAc,QAAQ,SAAS,MAAM,QAAQ,SACrE,cAAc,UAAU,cAAc,QAAQ,SAAS,MAAM,QAAQ;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;AChBA,SAAgB,KAAK,OAAe,KAAa,GAAmB;CAClE,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,OAAO,GAAG,OAAO,GAAG,GAAG,OAAO;CAClC,OAAO,SAAS,MAAM,SAAS;AACjC;;;;;;;;;;;;;;;;;;;;;;ACTA,SAAgB,KAAK,OAAkC;CACrD,IAAI,MAAM,WAAW,GAAG,OAAO;CAC/B,OAAO,MAAM,QAAQ,KAAK,MAAM,MAAM,GAAG,CAAC,IAAI,MAAM;AACtD;;;;;;;;;;;;;;;ACVA,SAAgB,cAAc,KAAa,KAAqB;CAC9D,OAAO,KAAK,OAAO,KAAK,MAAM,OAAO;AACvC;;;;;;;;AASA,SAAgB,iBAAiB,KAAa,KAAqB;CACjE,OAAO,KAAK,MAAM,KAAK,OAAO,KAAK,MAAM,MAAM,EAAE,IAAI;AACvD;;;;;;;;;;;;;;;ACbA,SAAgB,QAAQ,OAAe,UAA0B;CAC/D,MAAM,aAAa,KAAK,IAAI,IAAI,QAAQ;CACxC,OAAO,KAAK,MAAM,QAAQ,UAAU,IAAI;AAC1C;;;;;;;;;;;;;;;;;ACDA,SAAgB,IAAI,OAAkC;CACpD,OAAO,MAAM,QAAQ,KAAK,MAAM,MAAM,GAAG,CAAC;AAC5C"}
|
package/llms.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @helpers4/number
|
|
2
2
|
|
|
3
3
|
> Tree-shakable TypeScript utility functions for the `number` domain.
|
|
4
|
-
> Package: `@helpers4/number` — Version: 2.0.0
|
|
4
|
+
> Package: `@helpers4/number` — Version: 2.0.0
|
|
5
5
|
> License: LGPL-3.0-or-later
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
@@ -15,7 +15,7 @@ pnpm add @helpers4/number
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
import { clamp,
|
|
18
|
+
import { clamp, formatCompact, formatSize, ... } from '@helpers4/number';
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## Functions
|
|
@@ -23,7 +23,11 @@ import { clamp, formatSize, randomBetween, ... } from '@helpers4/number';
|
|
|
23
23
|
| Function | Description |
|
|
24
24
|
|---|---|
|
|
25
25
|
| `clamp` | Clamps a number between min and max values |
|
|
26
|
+
| `formatCompact` | Formats a number using compact notation (e.g. `1_500_000 → "1.5M"`). Thin wrapper over `Intl.Number |
|
|
26
27
|
| `formatSize` | Format a byte count into a human-readable string with the appropriate unit. Each unit is 1024 of th |
|
|
28
|
+
| `inRange` | Checks whether a number falls within `[min, max]` (both inclusive by default). |
|
|
29
|
+
| `lerp` | Linearly interpolates between `start` and `end` by the factor `t`. - `t = 0` returns `start`. - `t |
|
|
30
|
+
| `mean` | Calculates the arithmetic mean (average) of an array of numbers. Returns `NaN` for an empty array. |
|
|
27
31
|
| `randomBetween` | Generates a random number between min and max (inclusive) |
|
|
28
32
|
| `randomIntBetween` | Generates a random integer between min and max (inclusive) |
|
|
29
33
|
| `roundTo` | Rounds a number to specified decimal places |
|
|
@@ -65,6 +69,48 @@ clamp(5, 0, 10) // => 5
|
|
|
65
69
|
|
|
66
70
|
---
|
|
67
71
|
|
|
72
|
+
### `formatCompact`
|
|
73
|
+
|
|
74
|
+
Formats a number using compact notation (e.g. `1_500_000 → "1.5M"`).
|
|
75
|
+
|
|
76
|
+
Thin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion
|
|
77
|
+
of `formatSize` in the same `format*` family.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { formatCompact } from '@helpers4/number';
|
|
81
|
+
|
|
82
|
+
formatCompact(value: number, locale?: string): string
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Parameters:**
|
|
86
|
+
|
|
87
|
+
- `value: number` — The number to format.
|
|
88
|
+
- `locale?: string` — BCP 47 locale tag. Defaults to the runtime locale.
|
|
89
|
+
|
|
90
|
+
**Returns:** `string` — A compact string representation of the number.
|
|
91
|
+
|
|
92
|
+
**Examples:**
|
|
93
|
+
|
|
94
|
+
*Compact large numbers*
|
|
95
|
+
|
|
96
|
+
Formats a number using K / M suffixes for readability.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
formatCompact(1_500_000, 'en') // => '1.5M'
|
|
100
|
+
formatCompact(1_000, 'en') // => '1K'
|
|
101
|
+
formatCompact(999, 'en') // => '999'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
*Locale-aware formatting*
|
|
105
|
+
|
|
106
|
+
Uses the provided locale for the decimal separator and suffix.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
formatCompact(1_500_000, 'fr') // => '1,5 M'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
68
114
|
### `formatSize`
|
|
69
115
|
|
|
70
116
|
Format a byte count into a human-readable string with the appropriate unit.
|
|
@@ -100,6 +146,128 @@ formatSize(1_073_741_824) // '1.0GB'
|
|
|
100
146
|
|
|
101
147
|
---
|
|
102
148
|
|
|
149
|
+
### `inRange`
|
|
150
|
+
|
|
151
|
+
Checks whether a number falls within `[min, max]` (both inclusive by default).
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { inRange } from '@helpers4/number';
|
|
155
|
+
|
|
156
|
+
inRange(value: number, min: number, max: number, options: InRangeOptions): boolean
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Parameters:**
|
|
160
|
+
|
|
161
|
+
- `value: number` — The number to test
|
|
162
|
+
- `min: number` — Lower bound
|
|
163
|
+
- `max: number` — Upper bound
|
|
164
|
+
- `options: InRangeOptions` (default: `{}`) — Boundary inclusion mode (default: `'both'`)
|
|
165
|
+
|
|
166
|
+
**Returns:** `boolean` — `true` if `value` is within the specified range
|
|
167
|
+
|
|
168
|
+
**Examples:**
|
|
169
|
+
|
|
170
|
+
*Check if a value is within bounds (inclusive)*
|
|
171
|
+
|
|
172
|
+
Both min and max are included by default.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
inRange(5, 1, 10) // => true
|
|
176
|
+
inRange(0, 1, 10) // => false
|
|
177
|
+
inRange(1, 1, 10) // => true (min included)
|
|
178
|
+
inRange(10, 1, 10) // => true (max included)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
*Exclusive range*
|
|
182
|
+
|
|
183
|
+
Use { inclusive: "none" } for open interval (min, max).
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
inRange(5, 1, 10, { inclusive: 'none' }) // => true
|
|
187
|
+
inRange(1, 1, 10, { inclusive: 'none' }) // => false
|
|
188
|
+
inRange(10, 1, 10, { inclusive: 'none' }) // => false
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### `lerp`
|
|
194
|
+
|
|
195
|
+
Linearly interpolates between `start` and `end` by the factor `t`.
|
|
196
|
+
|
|
197
|
+
- `t = 0` returns `start`.
|
|
198
|
+
- `t = 1` returns `end`.
|
|
199
|
+
- Values of `t` outside `[0, 1]` extrapolate beyond the range.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { lerp } from '@helpers4/number';
|
|
203
|
+
|
|
204
|
+
lerp(start: number, end: number, t: number): number
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Parameters:**
|
|
208
|
+
|
|
209
|
+
- `start: number` — The start value.
|
|
210
|
+
- `end: number` — The end value.
|
|
211
|
+
- `t: number` — The interpolation factor.
|
|
212
|
+
|
|
213
|
+
**Returns:** `number` — The interpolated value.
|
|
214
|
+
|
|
215
|
+
**Examples:**
|
|
216
|
+
|
|
217
|
+
*Interpolate between two values*
|
|
218
|
+
|
|
219
|
+
Returns the value between start and end at position t (0 = start, 1 = end).
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
lerp(0, 100, 0) // => 0
|
|
223
|
+
lerp(0, 100, 0.5) // => 50
|
|
224
|
+
lerp(0, 100, 1) // => 100
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
*Animate a colour channel*
|
|
228
|
+
|
|
229
|
+
t outside [0, 1] extrapolates beyond the range.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
lerp(0, 255, 0.5) // => 127.5
|
|
233
|
+
lerp(0, 10, 2) // => 20 (extrapolation)
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
### `mean`
|
|
239
|
+
|
|
240
|
+
Calculates the arithmetic mean (average) of an array of numbers.
|
|
241
|
+
Returns `NaN` for an empty array.
|
|
242
|
+
|
|
243
|
+
Pairs with sum for aggregate operations.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { mean } from '@helpers4/number';
|
|
247
|
+
|
|
248
|
+
mean(array: readonly number[]): number
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
**Parameters:**
|
|
252
|
+
|
|
253
|
+
- `array: readonly number[]` — The array of numbers to average
|
|
254
|
+
|
|
255
|
+
**Returns:** `number` — The arithmetic mean, or `NaN` if the array is empty
|
|
256
|
+
|
|
257
|
+
**Examples:**
|
|
258
|
+
|
|
259
|
+
*Average a list of numbers*
|
|
260
|
+
|
|
261
|
+
Returns the arithmetic mean of the array; NaN for empty arrays.
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
mean([1, 2, 3, 4]) // => 2.5
|
|
265
|
+
mean([10, 20, 30]) // => 20
|
|
266
|
+
mean([]) // => NaN
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
103
271
|
### `randomBetween`
|
|
104
272
|
|
|
105
273
|
Generates a random number between min and max (inclusive)
|
package/meta/api.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"category": "number",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"functions": [
|
|
5
5
|
{
|
|
6
6
|
"name": "clamp",
|
|
@@ -43,11 +43,53 @@
|
|
|
43
43
|
],
|
|
44
44
|
"sourceFile": "clamp.ts"
|
|
45
45
|
},
|
|
46
|
+
{
|
|
47
|
+
"name": "formatCompact",
|
|
48
|
+
"kind": "function",
|
|
49
|
+
"description": "Formats a number using compact notation (e.g. `1_500_000 → \"1.5M\"`).\n\nThin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion\nof `formatSize` in the same `format*` family.",
|
|
50
|
+
"since": "2.0.0",
|
|
51
|
+
"signatures": [
|
|
52
|
+
{
|
|
53
|
+
"signature": "formatCompact(value: number, locale?: string): string",
|
|
54
|
+
"description": "Formats a number using compact notation (e.g. `1_500_000 → \"1.5M\"`).\n\nThin wrapper over `Intl.NumberFormat` with `notation: 'compact'`. Companion\nof `formatSize` in the same `format*` family.",
|
|
55
|
+
"params": [
|
|
56
|
+
{
|
|
57
|
+
"name": "value",
|
|
58
|
+
"type": "number",
|
|
59
|
+
"description": "The number to format."
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"name": "locale",
|
|
63
|
+
"type": "string",
|
|
64
|
+
"description": "BCP 47 locale tag. Defaults to the runtime locale.",
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
"returns": {
|
|
69
|
+
"type": "string",
|
|
70
|
+
"description": "A compact string representation of the number."
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
"examples": [
|
|
75
|
+
{
|
|
76
|
+
"title": "Compact large numbers",
|
|
77
|
+
"description": "Formats a number using K / M suffixes for readability.",
|
|
78
|
+
"code": "formatCompact(1_500_000, 'en') // => '1.5M'\nformatCompact(1_000, 'en') // => '1K'\nformatCompact(999, 'en') // => '999'"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"title": "Locale-aware formatting",
|
|
82
|
+
"description": "Uses the provided locale for the decimal separator and suffix.",
|
|
83
|
+
"code": "formatCompact(1_500_000, 'fr') // => '1,5 M'"
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
"sourceFile": "formatCompact.ts"
|
|
87
|
+
},
|
|
46
88
|
{
|
|
47
89
|
"name": "formatSize",
|
|
48
90
|
"kind": "function",
|
|
49
91
|
"description": "Format a byte count into a human-readable string with the appropriate unit.\n\nEach unit is 1024 of the previous (binary prefix). The result is formatted\nwith one decimal place.",
|
|
50
|
-
"since": "
|
|
92
|
+
"since": "2.0.0",
|
|
51
93
|
"signatures": [
|
|
52
94
|
{
|
|
53
95
|
"signature": "formatSize(bytes: number): string",
|
|
@@ -74,6 +116,142 @@
|
|
|
74
116
|
],
|
|
75
117
|
"sourceFile": "formatSize.ts"
|
|
76
118
|
},
|
|
119
|
+
{
|
|
120
|
+
"name": "inRange",
|
|
121
|
+
"kind": "function",
|
|
122
|
+
"description": "Checks whether a number falls within `[min, max]` (both inclusive by default).",
|
|
123
|
+
"since": "2.0.0",
|
|
124
|
+
"signatures": [
|
|
125
|
+
{
|
|
126
|
+
"signature": "inRange(value: number, min: number, max: number, options: InRangeOptions): boolean",
|
|
127
|
+
"description": "Checks whether a number falls within `[min, max]` (both inclusive by default).",
|
|
128
|
+
"params": [
|
|
129
|
+
{
|
|
130
|
+
"name": "value",
|
|
131
|
+
"type": "number",
|
|
132
|
+
"description": "The number to test"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"name": "min",
|
|
136
|
+
"type": "number",
|
|
137
|
+
"description": "Lower bound"
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
"name": "max",
|
|
141
|
+
"type": "number",
|
|
142
|
+
"description": "Upper bound"
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"name": "options",
|
|
146
|
+
"type": "InRangeOptions",
|
|
147
|
+
"description": "Boundary inclusion mode (default: `'both'`)",
|
|
148
|
+
"defaultValue": "{}"
|
|
149
|
+
}
|
|
150
|
+
],
|
|
151
|
+
"returns": {
|
|
152
|
+
"type": "boolean",
|
|
153
|
+
"description": "`true` if `value` is within the specified range"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
],
|
|
157
|
+
"examples": [
|
|
158
|
+
{
|
|
159
|
+
"title": "Check if a value is within bounds (inclusive)",
|
|
160
|
+
"description": "Both min and max are included by default.",
|
|
161
|
+
"code": "inRange(5, 1, 10) // => true\ninRange(0, 1, 10) // => false\ninRange(1, 1, 10) // => true (min included)\ninRange(10, 1, 10) // => true (max included)"
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
"title": "Exclusive range",
|
|
165
|
+
"description": "Use { inclusive: \"none\" } for open interval (min, max).",
|
|
166
|
+
"code": "inRange(5, 1, 10, { inclusive: 'none' }) // => true\ninRange(1, 1, 10, { inclusive: 'none' }) // => false\ninRange(10, 1, 10, { inclusive: 'none' }) // => false"
|
|
167
|
+
}
|
|
168
|
+
],
|
|
169
|
+
"sourceFile": "inRange.ts",
|
|
170
|
+
"relatedTypes": [
|
|
171
|
+
{
|
|
172
|
+
"name": "InRangeOptions",
|
|
173
|
+
"description": "Options for inRange.",
|
|
174
|
+
"typeDefinition": "interface InRangeOptions {\n inclusive?: \"min\" | \"max\" | \"both\" | \"none\";\n}"
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
"name": "lerp",
|
|
180
|
+
"kind": "function",
|
|
181
|
+
"description": "Linearly interpolates between `start` and `end` by the factor `t`.\n\n- `t = 0` returns `start`.\n- `t = 1` returns `end`.\n- Values of `t` outside `[0, 1]` extrapolate beyond the range.",
|
|
182
|
+
"since": "2.0.0",
|
|
183
|
+
"signatures": [
|
|
184
|
+
{
|
|
185
|
+
"signature": "lerp(start: number, end: number, t: number): number",
|
|
186
|
+
"description": "Linearly interpolates between `start` and `end` by the factor `t`.\n\n- `t = 0` returns `start`.\n- `t = 1` returns `end`.\n- Values of `t` outside `[0, 1]` extrapolate beyond the range.",
|
|
187
|
+
"params": [
|
|
188
|
+
{
|
|
189
|
+
"name": "start",
|
|
190
|
+
"type": "number",
|
|
191
|
+
"description": "The start value."
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
"name": "end",
|
|
195
|
+
"type": "number",
|
|
196
|
+
"description": "The end value."
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
"name": "t",
|
|
200
|
+
"type": "number",
|
|
201
|
+
"description": "The interpolation factor."
|
|
202
|
+
}
|
|
203
|
+
],
|
|
204
|
+
"returns": {
|
|
205
|
+
"type": "number",
|
|
206
|
+
"description": "The interpolated value."
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
],
|
|
210
|
+
"examples": [
|
|
211
|
+
{
|
|
212
|
+
"title": "Interpolate between two values",
|
|
213
|
+
"description": "Returns the value between start and end at position t (0 = start, 1 = end).",
|
|
214
|
+
"code": "lerp(0, 100, 0) // => 0\nlerp(0, 100, 0.5) // => 50\nlerp(0, 100, 1) // => 100"
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
"title": "Animate a colour channel",
|
|
218
|
+
"description": "t outside [0, 1] extrapolates beyond the range.",
|
|
219
|
+
"code": "lerp(0, 255, 0.5) // => 127.5\nlerp(0, 10, 2) // => 20 (extrapolation)"
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
"sourceFile": "lerp.ts"
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
"name": "mean",
|
|
226
|
+
"kind": "function",
|
|
227
|
+
"description": "Calculates the arithmetic mean (average) of an array of numbers.\nReturns `NaN` for an empty array.\n\nPairs with sum for aggregate operations.",
|
|
228
|
+
"since": "2.0.0",
|
|
229
|
+
"signatures": [
|
|
230
|
+
{
|
|
231
|
+
"signature": "mean(array: readonly number[]): number",
|
|
232
|
+
"description": "Calculates the arithmetic mean (average) of an array of numbers.\nReturns `NaN` for an empty array.\n\nPairs with sum for aggregate operations.",
|
|
233
|
+
"params": [
|
|
234
|
+
{
|
|
235
|
+
"name": "array",
|
|
236
|
+
"type": "readonly number[]",
|
|
237
|
+
"description": "The array of numbers to average"
|
|
238
|
+
}
|
|
239
|
+
],
|
|
240
|
+
"returns": {
|
|
241
|
+
"type": "number",
|
|
242
|
+
"description": "The arithmetic mean, or `NaN` if the array is empty"
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
],
|
|
246
|
+
"examples": [
|
|
247
|
+
{
|
|
248
|
+
"title": "Average a list of numbers",
|
|
249
|
+
"description": "Returns the arithmetic mean of the array; NaN for empty arrays.",
|
|
250
|
+
"code": "mean([1, 2, 3, 4]) // => 2.5\nmean([10, 20, 30]) // => 20\nmean([]) // => NaN"
|
|
251
|
+
}
|
|
252
|
+
],
|
|
253
|
+
"sourceFile": "mean.ts"
|
|
254
|
+
},
|
|
77
255
|
{
|
|
78
256
|
"name": "randomBetween",
|
|
79
257
|
"kind": "function",
|
package/meta/examples.json
CHANGED
|
@@ -11,6 +11,21 @@
|
|
|
11
11
|
}
|
|
12
12
|
]
|
|
13
13
|
},
|
|
14
|
+
{
|
|
15
|
+
"name": "formatCompact",
|
|
16
|
+
"examples": [
|
|
17
|
+
{
|
|
18
|
+
"title": "Compact large numbers",
|
|
19
|
+
"description": "Formats a number using K / M suffixes for readability.",
|
|
20
|
+
"code": "formatCompact(1_500_000, 'en') // => '1.5M'\nformatCompact(1_000, 'en') // => '1K'\nformatCompact(999, 'en') // => '999'"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"title": "Locale-aware formatting",
|
|
24
|
+
"description": "Uses the provided locale for the decimal separator and suffix.",
|
|
25
|
+
"code": "formatCompact(1_500_000, 'fr') // => '1,5 M'"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
14
29
|
{
|
|
15
30
|
"name": "formatSize",
|
|
16
31
|
"examples": [
|
|
@@ -21,6 +36,46 @@
|
|
|
21
36
|
}
|
|
22
37
|
]
|
|
23
38
|
},
|
|
39
|
+
{
|
|
40
|
+
"name": "inRange",
|
|
41
|
+
"examples": [
|
|
42
|
+
{
|
|
43
|
+
"title": "Check if a value is within bounds (inclusive)",
|
|
44
|
+
"description": "Both min and max are included by default.",
|
|
45
|
+
"code": "inRange(5, 1, 10) // => true\ninRange(0, 1, 10) // => false\ninRange(1, 1, 10) // => true (min included)\ninRange(10, 1, 10) // => true (max included)"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"title": "Exclusive range",
|
|
49
|
+
"description": "Use { inclusive: \"none\" } for open interval (min, max).",
|
|
50
|
+
"code": "inRange(5, 1, 10, { inclusive: 'none' }) // => true\ninRange(1, 1, 10, { inclusive: 'none' }) // => false\ninRange(10, 1, 10, { inclusive: 'none' }) // => false"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "lerp",
|
|
56
|
+
"examples": [
|
|
57
|
+
{
|
|
58
|
+
"title": "Interpolate between two values",
|
|
59
|
+
"description": "Returns the value between start and end at position t (0 = start, 1 = end).",
|
|
60
|
+
"code": "lerp(0, 100, 0) // => 0\nlerp(0, 100, 0.5) // => 50\nlerp(0, 100, 1) // => 100"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"title": "Animate a colour channel",
|
|
64
|
+
"description": "t outside [0, 1] extrapolates beyond the range.",
|
|
65
|
+
"code": "lerp(0, 255, 0.5) // => 127.5\nlerp(0, 10, 2) // => 20 (extrapolation)"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"name": "mean",
|
|
71
|
+
"examples": [
|
|
72
|
+
{
|
|
73
|
+
"title": "Average a list of numbers",
|
|
74
|
+
"description": "Returns the arithmetic mean of the array; NaN for empty arrays.",
|
|
75
|
+
"code": "mean([1, 2, 3, 4]) // => 2.5\nmean([10, 20, 30]) // => 20\nmean([]) // => NaN"
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
},
|
|
24
79
|
{
|
|
25
80
|
"name": "randomBetween",
|
|
26
81
|
"examples": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/number",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for number.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "LGPL-3.0",
|
|
@@ -28,7 +28,11 @@
|
|
|
28
28
|
"helpers",
|
|
29
29
|
"number",
|
|
30
30
|
"clamp",
|
|
31
|
+
"formatCompact",
|
|
31
32
|
"formatSize",
|
|
33
|
+
"inRange",
|
|
34
|
+
"lerp",
|
|
35
|
+
"mean",
|
|
32
36
|
"random",
|
|
33
37
|
"roundTo",
|
|
34
38
|
"sum"
|