@helpers4/number 2.0.0-alpha.18 → 2.0.0-alpha.19
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 +1 -0
- package/lib/index.d.ts +25 -1
- package/lib/index.js +42 -1
- package/lib/index.js.map +1 -1
- package/llms.txt +38 -2
- package/meta/api.json +32 -1
- package/meta/examples.json +10 -0
- package/package.json +2 -1
package/README.md
CHANGED
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
|
|
@@ -65,4 +89,4 @@ declare function roundTo(value: number, decimals: number): number;
|
|
|
65
89
|
*/
|
|
66
90
|
declare function sum(array: readonly number[]): number;
|
|
67
91
|
|
|
68
|
-
export { clamp, randomBetween, randomIntBetween, roundTo, sum };
|
|
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.
|
|
@@ -80,6 +121,6 @@ function sum(array) {
|
|
|
80
121
|
return array.reduce((acc, n) => acc + n, 0);
|
|
81
122
|
}
|
|
82
123
|
//#endregion
|
|
83
|
-
export { clamp, randomBetween, randomIntBetween, roundTo, sum };
|
|
124
|
+
export { clamp, formatSize, randomBetween, randomIntBetween, roundTo, sum };
|
|
84
125
|
|
|
85
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","../../../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 * 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;;;;;;;;;;;;;;;;
|
|
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
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-alpha.
|
|
4
|
+
> Package: `@helpers4/number` — Version: 2.0.0-alpha.19
|
|
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, formatSize, randomBetween, ... } from '@helpers4/number';
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## Functions
|
|
@@ -23,6 +23,7 @@ import { clamp, randomBetween, randomIntBetween, ... } from '@helpers4/number';
|
|
|
23
23
|
| Function | Description |
|
|
24
24
|
|---|---|
|
|
25
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 |
|
|
26
27
|
| `randomBetween` | Generates a random number between min and max (inclusive) |
|
|
27
28
|
| `randomIntBetween` | Generates a random integer between min and max (inclusive) |
|
|
28
29
|
| `roundTo` | Rounds a number to specified decimal places |
|
|
@@ -64,6 +65,41 @@ clamp(5, 0, 10) // => 5
|
|
|
64
65
|
|
|
65
66
|
---
|
|
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
|
+
|
|
67
103
|
### `randomBetween`
|
|
68
104
|
|
|
69
105
|
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-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.19",
|
|
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",
|
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": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/number",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.19",
|
|
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,6 +28,7 @@
|
|
|
28
28
|
"helpers",
|
|
29
29
|
"number",
|
|
30
30
|
"clamp",
|
|
31
|
+
"formatSize",
|
|
31
32
|
"random",
|
|
32
33
|
"roundTo",
|
|
33
34
|
"sum"
|