@helpers4/number 2.0.0-alpha.2 → 2.0.0-alpha.21
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/LICENSE.md +150 -650
- package/README.md +11 -5
- package/lib/index.d.ts +92 -4
- package/lib/index.js +119 -15
- package/lib/index.js.map +1 -0
- package/llms.txt +236 -0
- package/meta/api.json +226 -0
- package/meta/category.json +6 -0
- package/meta/examples.json +70 -0
- package/meta/licenses.json +4 -0
- package/meta/native-alternatives.json +37 -0
- package/package.json +20 -6
package/README.md
CHANGED
|
@@ -9,20 +9,24 @@ A set of helpers for working with URLs.
|
|
|
9
9
|
|
|
10
10
|
## Documentation
|
|
11
11
|
|
|
12
|
-
[https://helpers4.
|
|
12
|
+
[https://helpers4.dev/typescript/categories/number/](https://helpers4.dev/typescript/categories/number/)
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
15
|
- clamp
|
|
16
|
-
-
|
|
16
|
+
- formatSize
|
|
17
17
|
- random
|
|
18
|
+
- roundTo
|
|
19
|
+
- sum
|
|
18
20
|
<!-- /AUTOMATIC-METHODS -->
|
|
19
21
|
|
|
20
22
|
## See
|
|
21
23
|
|
|
22
24
|
<!-- AUTOMATIC-SIBLINGS -->
|
|
23
25
|
- [array](../array)
|
|
26
|
+
- [commit](../commit)
|
|
24
27
|
- [date](../date)
|
|
25
28
|
- [function](../function)
|
|
29
|
+
- [math](../math)
|
|
26
30
|
- [object](../object)
|
|
27
31
|
- [observable](../observable)
|
|
28
32
|
- [promise](../promise)
|
|
@@ -38,14 +42,16 @@ A set of helpers for working with URLs.
|
|
|
38
42
|
| Name | Package | Source Code | Description |
|
|
39
43
|
|------|---------|-------------|-------------|
|
|
40
44
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
41
|
-
|
|
|
45
|
+
| 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
|
+
| 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 |
|
|
42
47
|
| 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 |
|
|
43
|
-
|
|
|
48
|
+
| math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) | Mathematical utility functions and calculations |
|
|
49
|
+
| 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 |
|
|
44
50
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
45
51
|
| observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
|
|
46
52
|
| promise | [@helpers4/promise](https://www.npmjs.com/package/@helpers4/promise) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/promise) | Promise utilities and error handling |
|
|
47
53
|
| string | [@helpers4/string](https://www.npmjs.com/package/@helpers4/string) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/string) | String manipulation and formatting utilities |
|
|
48
|
-
| type | [@helpers4/type](https://www.npmjs.com/package/@helpers4/type) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/type) |
|
|
54
|
+
| type | [@helpers4/type](https://www.npmjs.com/package/@helpers4/type) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/type) | Type checking and validation utilities |
|
|
49
55
|
| url | [@helpers4/url](https://www.npmjs.com/package/@helpers4/url) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/url) | URL parsing and manipulation utilities |
|
|
50
56
|
| version | [@helpers4/version](https://www.npmjs.com/package/@helpers4/version) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/version) | Version string manipulation utilities |
|
|
51
57
|
<!-- /AUTOMATIC-CATEGORIES-TABLE -->
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of helpers4.
|
|
3
|
+
* Copyright (C) 2025 baxyz
|
|
4
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Clamps a number between min and max values
|
|
8
|
+
* @param value - The value to clamp
|
|
9
|
+
* @param min - Minimum value
|
|
10
|
+
* @param max - Maximum value
|
|
11
|
+
* @returns Clamped value
|
|
12
|
+
* @since 1.9.0
|
|
13
|
+
*/
|
|
14
|
+
declare function clamp(value: number, min: number, max: number): number;
|
|
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
|
+
* Format a byte count into a human-readable string with the appropriate unit.
|
|
23
|
+
*
|
|
24
|
+
* Each unit is 1024 of the previous (binary prefix). The result is formatted
|
|
25
|
+
* with one decimal place.
|
|
26
|
+
*
|
|
27
|
+
* @param bytes - A non-negative integer representing a byte count.
|
|
28
|
+
* @returns A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`.
|
|
29
|
+
* @example
|
|
30
|
+
* formatSize(0) // '0.0B'
|
|
31
|
+
* formatSize(512) // '512.0B'
|
|
32
|
+
* formatSize(1024) // '1.0KB'
|
|
33
|
+
* formatSize(1536) // '1.5KB'
|
|
34
|
+
* formatSize(1_048_576) // '1.0MB'
|
|
35
|
+
* formatSize(1_073_741_824) // '1.0GB'
|
|
36
|
+
* @since next
|
|
37
|
+
*/
|
|
38
|
+
declare function formatSize(bytes: number): string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* This file is part of helpers4.
|
|
42
|
+
* Copyright (C) 2025 baxyz
|
|
43
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* Generates a random number between min and max (inclusive)
|
|
47
|
+
* @param min - Minimum value
|
|
48
|
+
* @param max - Maximum value
|
|
49
|
+
* @returns Random number between min and max
|
|
50
|
+
* @since 1.9.0
|
|
51
|
+
*/
|
|
52
|
+
declare function randomBetween(min: number, max: number): number;
|
|
53
|
+
/**
|
|
54
|
+
* Generates a random integer between min and max (inclusive)
|
|
55
|
+
* @param min - Minimum value
|
|
56
|
+
* @param max - Maximum value
|
|
57
|
+
* @returns Random integer between min and max
|
|
58
|
+
* @since 1.9.0
|
|
59
|
+
*/
|
|
60
|
+
declare function randomIntBetween(min: number, max: number): number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* This file is part of helpers4.
|
|
64
|
+
* Copyright (C) 2025 baxyz
|
|
65
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* Rounds a number to specified decimal places
|
|
69
|
+
* @param value - The number to round
|
|
70
|
+
* @param decimals - Number of decimal places
|
|
71
|
+
* @returns Rounded number
|
|
72
|
+
* @since 1.9.0
|
|
73
|
+
*/
|
|
74
|
+
declare function roundTo(value: number, decimals: number): number;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* This file is part of helpers4.
|
|
78
|
+
* Copyright (C) 2025 baxyz
|
|
79
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Calculates the sum of an array of numbers.
|
|
83
|
+
* @param array - The array of numbers to sum
|
|
84
|
+
* @returns The sum of all values
|
|
85
|
+
* @example
|
|
86
|
+
* sum([1, 2, 3, 4])
|
|
87
|
+
* // => 10
|
|
88
|
+
* @since 2.0.0
|
|
89
|
+
*/
|
|
90
|
+
declare function sum(array: readonly number[]): number;
|
|
91
|
+
|
|
92
|
+
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
package/lib/index.js
CHANGED
|
@@ -1,22 +1,126 @@
|
|
|
1
|
-
|
|
1
|
+
//#region helpers/number/clamp.ts
|
|
2
|
+
/**
|
|
3
|
+
* This file is part of helpers4.
|
|
4
|
+
* Copyright (C) 2025 baxyz
|
|
5
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Clamps a number between min and max values
|
|
9
|
+
* @param value - The value to clamp
|
|
10
|
+
* @param min - Minimum value
|
|
11
|
+
* @param max - Maximum value
|
|
12
|
+
* @returns Clamped value
|
|
13
|
+
* @since 1.9.0
|
|
14
|
+
*/
|
|
2
15
|
function clamp(value, min, max) {
|
|
3
|
-
|
|
16
|
+
return Math.min(Math.max(value, min), max);
|
|
4
17
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region helpers/number/formatSize.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
|
+
/** Size units in ascending order. */
|
|
26
|
+
var UNITS = [
|
|
27
|
+
"B",
|
|
28
|
+
"KB",
|
|
29
|
+
"MB",
|
|
30
|
+
"GB",
|
|
31
|
+
"TB"
|
|
32
|
+
];
|
|
33
|
+
/**
|
|
34
|
+
* Format a byte count into a human-readable string with the appropriate unit.
|
|
35
|
+
*
|
|
36
|
+
* Each unit is 1024 of the previous (binary prefix). The result is formatted
|
|
37
|
+
* with one decimal place.
|
|
38
|
+
*
|
|
39
|
+
* @param bytes - A non-negative integer representing a byte count.
|
|
40
|
+
* @returns A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`.
|
|
41
|
+
* @example
|
|
42
|
+
* formatSize(0) // '0.0B'
|
|
43
|
+
* formatSize(512) // '512.0B'
|
|
44
|
+
* formatSize(1024) // '1.0KB'
|
|
45
|
+
* formatSize(1536) // '1.5KB'
|
|
46
|
+
* formatSize(1_048_576) // '1.0MB'
|
|
47
|
+
* formatSize(1_073_741_824) // '1.0GB'
|
|
48
|
+
* @since next
|
|
49
|
+
*/
|
|
50
|
+
function formatSize(bytes) {
|
|
51
|
+
let size = bytes;
|
|
52
|
+
let unitIndex = 0;
|
|
53
|
+
while (size >= 1024 && unitIndex < UNITS.length - 1) {
|
|
54
|
+
size /= 1024;
|
|
55
|
+
unitIndex++;
|
|
56
|
+
}
|
|
57
|
+
return `${size.toFixed(1)}${UNITS[unitIndex]}`;
|
|
9
58
|
}
|
|
10
|
-
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region helpers/number/random.ts
|
|
61
|
+
/**
|
|
62
|
+
* This file is part of helpers4.
|
|
63
|
+
* Copyright (C) 2025 baxyz
|
|
64
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
65
|
+
*/
|
|
66
|
+
/**
|
|
67
|
+
* Generates a random number between min and max (inclusive)
|
|
68
|
+
* @param min - Minimum value
|
|
69
|
+
* @param max - Maximum value
|
|
70
|
+
* @returns Random number between min and max
|
|
71
|
+
* @since 1.9.0
|
|
72
|
+
*/
|
|
11
73
|
function randomBetween(min, max) {
|
|
12
|
-
|
|
74
|
+
return Math.random() * (max - min) + min;
|
|
13
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Generates a random integer between min and max (inclusive)
|
|
78
|
+
* @param min - Minimum value
|
|
79
|
+
* @param max - Maximum value
|
|
80
|
+
* @returns Random integer between min and max
|
|
81
|
+
* @since 1.9.0
|
|
82
|
+
*/
|
|
14
83
|
function randomIntBetween(min, max) {
|
|
15
|
-
|
|
84
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region helpers/number/roundTo.ts
|
|
88
|
+
/**
|
|
89
|
+
* This file is part of helpers4.
|
|
90
|
+
* Copyright (C) 2025 baxyz
|
|
91
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* Rounds a number to specified decimal places
|
|
95
|
+
* @param value - The number to round
|
|
96
|
+
* @param decimals - Number of decimal places
|
|
97
|
+
* @returns Rounded number
|
|
98
|
+
* @since 1.9.0
|
|
99
|
+
*/
|
|
100
|
+
function roundTo(value, decimals) {
|
|
101
|
+
const multiplier = Math.pow(10, decimals);
|
|
102
|
+
return Math.round(value * multiplier) / multiplier;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region helpers/number/sum.ts
|
|
106
|
+
/**
|
|
107
|
+
* This file is part of helpers4.
|
|
108
|
+
* Copyright (C) 2025 baxyz
|
|
109
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
110
|
+
*/
|
|
111
|
+
/**
|
|
112
|
+
* Calculates the sum of an array of numbers.
|
|
113
|
+
* @param array - The array of numbers to sum
|
|
114
|
+
* @returns The sum of all values
|
|
115
|
+
* @example
|
|
116
|
+
* sum([1, 2, 3, 4])
|
|
117
|
+
* // => 10
|
|
118
|
+
* @since 2.0.0
|
|
119
|
+
*/
|
|
120
|
+
function sum(array) {
|
|
121
|
+
return array.reduce((acc, n) => acc + n, 0);
|
|
16
122
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
clamp
|
|
22
|
-
};
|
|
123
|
+
//#endregion
|
|
124
|
+
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +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 next\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 * 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;AACrE,QAAO,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI;;;;;;;;;;ACR5C,IAAM,QAAQ;CAAC;CAAK;CAAM;CAAM;CAAM;CAAK;;;;;;;;;;;;;;;;;;AAmB3C,SAAgB,WAAW,OAAuB;CAChD,IAAI,OAAO;CACX,IAAI,YAAY;AAEhB,QAAO,QAAQ,QAAQ,YAAY,MAAM,SAAS,GAAG;AACnD,UAAQ;AACR;;AAGF,QAAO,GAAG,KAAK,QAAQ,EAAE,GAAG,MAAM;;;;;;;;;;;;;;;;ACtBpC,SAAgB,cAAc,KAAa,KAAqB;AAC9D,QAAO,KAAK,QAAQ,IAAI,MAAM,OAAO;;;;;;;;;AAUvC,SAAgB,iBAAiB,KAAa,KAAqB;AACjE,QAAO,KAAK,MAAM,KAAK,QAAQ,IAAI,MAAM,MAAM,GAAG,GAAG;;;;;;;;;;;;;;;;ACZvD,SAAgB,QAAQ,OAAe,UAA0B;CAC/D,MAAM,aAAa,KAAK,IAAI,IAAI,SAAS;AACzC,QAAO,KAAK,MAAM,QAAQ,WAAW,GAAG;;;;;;;;;;;;;;;;;;ACA1C,SAAgB,IAAI,OAAkC;AACpD,QAAO,MAAM,QAAQ,KAAK,MAAM,MAAM,GAAG,EAAE"}
|
package/llms.txt
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# @helpers4/number
|
|
2
|
+
|
|
3
|
+
> Tree-shakable TypeScript utility functions for the `number` domain.
|
|
4
|
+
> Package: `@helpers4/number` — Version: 2.0.0-alpha.21
|
|
5
|
+
> License: LGPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @helpers4/number
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @helpers4/number
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { clamp, formatSize, randomBetween, ... } from '@helpers4/number';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Functions
|
|
22
|
+
|
|
23
|
+
| Function | Description |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `clamp` | Clamps a number between min and max values |
|
|
26
|
+
| `formatSize` | Format a byte count into a human-readable string with the appropriate unit. Each unit is 1024 of th |
|
|
27
|
+
| `randomBetween` | Generates a random number between min and max (inclusive) |
|
|
28
|
+
| `randomIntBetween` | Generates a random integer between min and max (inclusive) |
|
|
29
|
+
| `roundTo` | Rounds a number to specified decimal places |
|
|
30
|
+
| `sum` | Calculates the sum of an array of numbers. |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## API Reference
|
|
35
|
+
|
|
36
|
+
### `clamp`
|
|
37
|
+
|
|
38
|
+
Clamps a number between min and max values
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { clamp } from '@helpers4/number';
|
|
42
|
+
|
|
43
|
+
clamp(value: number, min: number, max: number): number
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**Parameters:**
|
|
47
|
+
|
|
48
|
+
- `value: number` — The value to clamp
|
|
49
|
+
- `min: number` — Minimum value
|
|
50
|
+
- `max: number` — Maximum value
|
|
51
|
+
|
|
52
|
+
**Returns:** `number` — Clamped value
|
|
53
|
+
|
|
54
|
+
**Examples:**
|
|
55
|
+
|
|
56
|
+
*Clamp a value within range*
|
|
57
|
+
|
|
58
|
+
Restricts a number to be within a min/max range.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
clamp(15, 0, 10) // => 10
|
|
62
|
+
clamp(-5, 0, 10) // => 0
|
|
63
|
+
clamp(5, 0, 10) // => 5
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### `formatSize`
|
|
69
|
+
|
|
70
|
+
Format a byte count into a human-readable string with the appropriate unit.
|
|
71
|
+
|
|
72
|
+
Each unit is 1024 of the previous (binary prefix). The result is formatted
|
|
73
|
+
with one decimal place.
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { formatSize } from '@helpers4/number';
|
|
77
|
+
|
|
78
|
+
formatSize(bytes: number): string
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Parameters:**
|
|
82
|
+
|
|
83
|
+
- `bytes: number` — A non-negative integer representing a byte count.
|
|
84
|
+
|
|
85
|
+
**Returns:** `string` — A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`.
|
|
86
|
+
|
|
87
|
+
**Examples:**
|
|
88
|
+
|
|
89
|
+
*Format bytes to human-readable size*
|
|
90
|
+
|
|
91
|
+
Converts a raw byte count to a human-readable string using binary prefixes.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
formatSize(0) // '0.0B'
|
|
95
|
+
formatSize(512) // '512.0B'
|
|
96
|
+
formatSize(1024) // '1.0KB'
|
|
97
|
+
formatSize(1_048_576) // '1.0MB'
|
|
98
|
+
formatSize(1_073_741_824) // '1.0GB'
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### `randomBetween`
|
|
104
|
+
|
|
105
|
+
Generates a random number between min and max (inclusive)
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { randomBetween } from '@helpers4/number';
|
|
109
|
+
|
|
110
|
+
randomBetween(min: number, max: number): number
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Parameters:**
|
|
114
|
+
|
|
115
|
+
- `min: number` — Minimum value
|
|
116
|
+
- `max: number` — Maximum value
|
|
117
|
+
|
|
118
|
+
**Returns:** `number` — Random number between min and max
|
|
119
|
+
|
|
120
|
+
**Examples:**
|
|
121
|
+
|
|
122
|
+
*Generate a random float in range*
|
|
123
|
+
|
|
124
|
+
Returns a random number between min and max (inclusive).
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
randomBetween(1, 10)
|
|
128
|
+
// => e.g. 5.327...
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
*Generate a random integer in range*
|
|
132
|
+
|
|
133
|
+
Returns a random integer between min and max (inclusive).
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
randomIntBetween(1, 6)
|
|
137
|
+
// => e.g. 4
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
### `randomIntBetween`
|
|
143
|
+
|
|
144
|
+
Generates a random integer between min and max (inclusive)
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { randomIntBetween } from '@helpers4/number';
|
|
148
|
+
|
|
149
|
+
randomIntBetween(min: number, max: number): number
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Parameters:**
|
|
153
|
+
|
|
154
|
+
- `min: number` — Minimum value
|
|
155
|
+
- `max: number` — Maximum value
|
|
156
|
+
|
|
157
|
+
**Returns:** `number` — Random integer between min and max
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
### `roundTo`
|
|
162
|
+
|
|
163
|
+
Rounds a number to specified decimal places
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { roundTo } from '@helpers4/number';
|
|
167
|
+
|
|
168
|
+
roundTo(value: number, decimals: number): number
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Parameters:**
|
|
172
|
+
|
|
173
|
+
- `value: number` — The number to round
|
|
174
|
+
- `decimals: number` — Number of decimal places
|
|
175
|
+
|
|
176
|
+
**Returns:** `number` — Rounded number
|
|
177
|
+
|
|
178
|
+
**Examples:**
|
|
179
|
+
|
|
180
|
+
*Round to 2 decimal places*
|
|
181
|
+
|
|
182
|
+
Rounds a floating-point number to the specified number of decimals.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
roundTo(3.14159, 2)
|
|
186
|
+
// => 3.14
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
*Round to 0 decimal places*
|
|
190
|
+
|
|
191
|
+
Effectively rounds to the nearest integer.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
roundTo(3.7, 0)
|
|
195
|
+
// => 4
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
### `sum`
|
|
201
|
+
|
|
202
|
+
Calculates the sum of an array of numbers.
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { sum } from '@helpers4/number';
|
|
206
|
+
|
|
207
|
+
sum(array: readonly number[]): number
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Parameters:**
|
|
211
|
+
|
|
212
|
+
- `array: readonly number[]` — The array of numbers to sum
|
|
213
|
+
|
|
214
|
+
**Returns:** `number` — The sum of all values
|
|
215
|
+
|
|
216
|
+
**Examples:**
|
|
217
|
+
|
|
218
|
+
*Sum numbers*
|
|
219
|
+
|
|
220
|
+
Calculates the sum of an array of numbers.
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
sum([1, 2, 3, 4])
|
|
224
|
+
// => 10
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
*Sum with negative numbers*
|
|
228
|
+
|
|
229
|
+
Handles negative numbers correctly.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
sum([10, -3, 5, -2])
|
|
233
|
+
// => 10
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|