@helpers4/number 2.0.0-alpha.9 → 2.0.0-beta.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 +8 -4
- package/lib/index.d.ts +41 -1
- package/lib/index.js +61 -1
- package/lib/index.js.map +1 -1
- package/llms.txt +236 -0
- package/meta/api.json +68 -1
- package/meta/category.json +6 -0
- package/meta/examples.json +25 -0
- package/meta/native-alternatives.json +37 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -13,14 +13,17 @@ A set of helpers for working with URLs.
|
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
15
|
- clamp
|
|
16
|
+
- formatSize
|
|
16
17
|
- random
|
|
17
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)
|
|
26
29
|
- [math](../math)
|
|
@@ -39,15 +42,16 @@ A set of helpers for working with URLs.
|
|
|
39
42
|
| Name | Package | Source Code | Description |
|
|
40
43
|
|------|---------|-------------|-------------|
|
|
41
44
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
42
|
-
|
|
|
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 |
|
|
43
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 |
|
|
44
|
-
| math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) |
|
|
45
|
-
| number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) |
|
|
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 |
|
|
46
50
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
47
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 |
|
|
48
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 |
|
|
49
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 |
|
|
50
|
-
| 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 |
|
|
51
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 |
|
|
52
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 |
|
|
53
57
|
<!-- /AUTOMATIC-CATEGORIES-TABLE -->
|
package/lib/index.d.ts
CHANGED
|
@@ -13,6 +13,30 @@
|
|
|
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
|
+
* 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
|
+
|
|
16
40
|
/**
|
|
17
41
|
* This file is part of helpers4.
|
|
18
42
|
* Copyright (C) 2025 baxyz
|
|
@@ -49,4 +73,20 @@ declare function randomIntBetween(min: number, max: number): number;
|
|
|
49
73
|
*/
|
|
50
74
|
declare function roundTo(value: number, decimals: number): number;
|
|
51
75
|
|
|
52
|
-
|
|
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
|
@@ -16,6 +16,47 @@ function clamp(value, min, max) {
|
|
|
16
16
|
return Math.min(Math.max(value, min), max);
|
|
17
17
|
}
|
|
18
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]}`;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
19
60
|
//#region helpers/number/random.ts
|
|
20
61
|
/**
|
|
21
62
|
* This file is part of helpers4.
|
|
@@ -61,6 +102,25 @@ function roundTo(value, decimals) {
|
|
|
61
102
|
return Math.round(value * multiplier) / multiplier;
|
|
62
103
|
}
|
|
63
104
|
//#endregion
|
|
64
|
-
|
|
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);
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
|
65
125
|
|
|
66
126
|
//# 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/random.ts","../../../helpers/number/roundTo.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 * 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"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,MAAM,OAAe,KAAa,KAAqB;AACrE,QAAO,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI;;;;;;;;;;;;;;;;
|
|
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-beta.0
|
|
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
|
+
---
|
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-beta.0",
|
|
4
4
|
"functions": [
|
|
5
5
|
{
|
|
6
6
|
"name": "clamp",
|
|
@@ -43,6 +43,37 @@
|
|
|
43
43
|
],
|
|
44
44
|
"sourceFile": "clamp.ts"
|
|
45
45
|
},
|
|
46
|
+
{
|
|
47
|
+
"name": "formatSize",
|
|
48
|
+
"kind": "function",
|
|
49
|
+
"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": "next",
|
|
51
|
+
"signatures": [
|
|
52
|
+
{
|
|
53
|
+
"signature": "formatSize(bytes: number): string",
|
|
54
|
+
"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.",
|
|
55
|
+
"params": [
|
|
56
|
+
{
|
|
57
|
+
"name": "bytes",
|
|
58
|
+
"type": "number",
|
|
59
|
+
"description": "A non-negative integer representing a byte count."
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
"returns": {
|
|
63
|
+
"type": "string",
|
|
64
|
+
"description": "A human-readable string such as `'0.0B'`, `'1.5KB'`, `'3.2MB'`."
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
"examples": [
|
|
69
|
+
{
|
|
70
|
+
"title": "Format bytes to human-readable size",
|
|
71
|
+
"description": "Converts a raw byte count to a human-readable string using binary prefixes.",
|
|
72
|
+
"code": "formatSize(0) // '0.0B'\nformatSize(512) // '512.0B'\nformatSize(1024) // '1.0KB'\nformatSize(1_048_576) // '1.0MB'\nformatSize(1_073_741_824) // '1.0GB'"
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
"sourceFile": "formatSize.ts"
|
|
76
|
+
},
|
|
46
77
|
{
|
|
47
78
|
"name": "randomBetween",
|
|
48
79
|
"kind": "function",
|
|
@@ -154,6 +185,42 @@
|
|
|
154
185
|
}
|
|
155
186
|
],
|
|
156
187
|
"sourceFile": "roundTo.ts"
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
"name": "sum",
|
|
191
|
+
"kind": "function",
|
|
192
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
193
|
+
"since": "2.0.0",
|
|
194
|
+
"signatures": [
|
|
195
|
+
{
|
|
196
|
+
"signature": "sum(array: readonly number[]): number",
|
|
197
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
198
|
+
"params": [
|
|
199
|
+
{
|
|
200
|
+
"name": "array",
|
|
201
|
+
"type": "readonly number[]",
|
|
202
|
+
"description": "The array of numbers to sum"
|
|
203
|
+
}
|
|
204
|
+
],
|
|
205
|
+
"returns": {
|
|
206
|
+
"type": "number",
|
|
207
|
+
"description": "The sum of all values"
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
],
|
|
211
|
+
"examples": [
|
|
212
|
+
{
|
|
213
|
+
"title": "Sum numbers",
|
|
214
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
215
|
+
"code": "sum([1, 2, 3, 4])\n// => 10"
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
"title": "Sum with negative numbers",
|
|
219
|
+
"description": "Handles negative numbers correctly.",
|
|
220
|
+
"code": "sum([10, -3, 5, -2])\n// => 10"
|
|
221
|
+
}
|
|
222
|
+
],
|
|
223
|
+
"sourceFile": "sum.ts"
|
|
157
224
|
}
|
|
158
225
|
]
|
|
159
226
|
}
|
package/meta/examples.json
CHANGED
|
@@ -11,6 +11,16 @@
|
|
|
11
11
|
}
|
|
12
12
|
]
|
|
13
13
|
},
|
|
14
|
+
{
|
|
15
|
+
"name": "formatSize",
|
|
16
|
+
"examples": [
|
|
17
|
+
{
|
|
18
|
+
"title": "Format bytes to human-readable size",
|
|
19
|
+
"description": "Converts a raw byte count to a human-readable string using binary prefixes.",
|
|
20
|
+
"code": "formatSize(0) // '0.0B'\nformatSize(512) // '512.0B'\nformatSize(1024) // '1.0KB'\nformatSize(1_048_576) // '1.0MB'\nformatSize(1_073_741_824) // '1.0GB'"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
},
|
|
14
24
|
{
|
|
15
25
|
"name": "randomBetween",
|
|
16
26
|
"examples": [
|
|
@@ -40,6 +50,21 @@
|
|
|
40
50
|
"code": "roundTo(3.7, 0)\n// => 4"
|
|
41
51
|
}
|
|
42
52
|
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "sum",
|
|
56
|
+
"examples": [
|
|
57
|
+
{
|
|
58
|
+
"title": "Sum numbers",
|
|
59
|
+
"description": "Calculates the sum of an array of numbers.",
|
|
60
|
+
"code": "sum([1, 2, 3, 4])\n// => 10"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"title": "Sum with negative numbers",
|
|
64
|
+
"description": "Handles negative numbers correctly.",
|
|
65
|
+
"code": "sum([10, -3, 5, -2])\n// => 10"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
43
68
|
}
|
|
44
69
|
]
|
|
45
70
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "number",
|
|
3
|
+
"functions": [
|
|
4
|
+
{
|
|
5
|
+
"name": "min / max",
|
|
6
|
+
"libraries": [
|
|
7
|
+
"lodash",
|
|
8
|
+
"radashi",
|
|
9
|
+
"ramda"
|
|
10
|
+
],
|
|
11
|
+
"native": "Math.min(...arr) / Math.max(...arr)",
|
|
12
|
+
"since": "ES1",
|
|
13
|
+
"example": "Math.min(...numbers)\nMath.max(...numbers)",
|
|
14
|
+
"notes": "For large arrays, prefer reduce to avoid stack overflow"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"name": "ceil / floor",
|
|
18
|
+
"libraries": [
|
|
19
|
+
"lodash"
|
|
20
|
+
],
|
|
21
|
+
"native": "Math.ceil() / Math.floor()",
|
|
22
|
+
"since": "ES1",
|
|
23
|
+
"example": "Math.ceil(4.2) // 5\nMath.floor(4.8) // 4",
|
|
24
|
+
"notes": "No precision param natively; use roundTo from helpers4 for decimal precision"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "toInt / toFloat",
|
|
28
|
+
"libraries": [
|
|
29
|
+
"radashi"
|
|
30
|
+
],
|
|
31
|
+
"native": "parseInt(str, 10) / parseFloat(str)",
|
|
32
|
+
"since": "ES1",
|
|
33
|
+
"example": "parseInt('42px', 10) // 42\nparseFloat('3.14') // 3.14",
|
|
34
|
+
"notes": "Radashi adds default-value fallback on NaN"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
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-beta.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",
|
|
@@ -21,20 +21,24 @@
|
|
|
21
21
|
"./meta/api.json": "./meta/api.json",
|
|
22
22
|
"./meta/examples.json": "./meta/examples.json",
|
|
23
23
|
"./meta/licenses.json": "./meta/licenses.json",
|
|
24
|
+
"./llms.txt": "./llms.txt",
|
|
24
25
|
"./package.json": "./package.json"
|
|
25
26
|
},
|
|
26
27
|
"keywords": [
|
|
27
28
|
"helpers",
|
|
28
29
|
"number",
|
|
29
30
|
"clamp",
|
|
31
|
+
"formatSize",
|
|
30
32
|
"random",
|
|
31
|
-
"roundTo"
|
|
33
|
+
"roundTo",
|
|
34
|
+
"sum"
|
|
32
35
|
],
|
|
33
36
|
"files": [
|
|
34
37
|
"lib/index.js",
|
|
35
38
|
"lib/index.d.ts",
|
|
36
39
|
"lib/index.js.map",
|
|
37
40
|
"meta/",
|
|
41
|
+
"llms.txt",
|
|
38
42
|
"LICENSE.md",
|
|
39
43
|
"package.json",
|
|
40
44
|
"README.md"
|