utilium 3.8.0 → 3.9.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/dist/numbers.d.ts +4 -0
- package/dist/numbers.js +20 -0
- package/package.json +1 -1
package/dist/numbers.d.ts
CHANGED
|
@@ -9,3 +9,7 @@ export declare const formatCompact: {
|
|
|
9
9
|
};
|
|
10
10
|
export type ToNumber<T extends CoercibleToString> = `${T}` extends `${infer U extends number}` ? U : never;
|
|
11
11
|
export type ToBigInt<T extends CoercibleToString> = `${T}` extends `${infer U extends bigint}` ? U : never;
|
|
12
|
+
/**
|
|
13
|
+
* Parse a string containing some number of bytes, e.g. `1 GiB` or `99.4kb`
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseBytes(input: string): bigint | null;
|
package/dist/numbers.js
CHANGED
|
@@ -15,3 +15,23 @@ export function toRadians(degrees) {
|
|
|
15
15
|
}
|
|
16
16
|
const __formatter = Intl.NumberFormat('en', { notation: 'compact' });
|
|
17
17
|
export const formatCompact = __formatter.format.bind(__formatter);
|
|
18
|
+
const byteSizePattern = /^(?<size>\d+(?:\.\d+)?)\s*(?<unit>\w*)$/i;
|
|
19
|
+
const units = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
|
|
20
|
+
/**
|
|
21
|
+
* Parse a string containing some number of bytes, e.g. `1 GiB` or `99.4kb`
|
|
22
|
+
*/
|
|
23
|
+
export function parseBytes(input) {
|
|
24
|
+
const match = byteSizePattern.exec(input.trim());
|
|
25
|
+
if (!match)
|
|
26
|
+
return null;
|
|
27
|
+
const size = BigInt(match.groups.size);
|
|
28
|
+
let unit = match.groups.unit.toUpperCase();
|
|
29
|
+
if (unit.at(-1) == 'B')
|
|
30
|
+
unit = unit.slice(0, -1);
|
|
31
|
+
if (unit.at(-1) == 'I')
|
|
32
|
+
unit = unit.slice(0, -1);
|
|
33
|
+
const unitIndex = BigInt(units.indexOf(unit));
|
|
34
|
+
if (unitIndex === -1n)
|
|
35
|
+
return null;
|
|
36
|
+
return size * 1024n ** unitIndex;
|
|
37
|
+
}
|