@visulima/humanizer 3.0.0-alpha.10 → 3.0.0-alpha.12

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.
Files changed (150) hide show
  1. package/dist/index.d.ts +140 -4
  2. package/dist/index.js +1 -3
  3. package/dist/language/af.d.ts +3 -2
  4. package/dist/language/af.js +1 -41
  5. package/dist/language/am.d.ts +3 -2
  6. package/dist/language/am.js +1 -33
  7. package/dist/language/ar.d.ts +3 -2
  8. package/dist/language/ar.js +1 -53
  9. package/dist/language/bg.d.ts +3 -2
  10. package/dist/language/bg.js +1 -42
  11. package/dist/language/bn.d.ts +3 -2
  12. package/dist/language/bn.js +1 -33
  13. package/dist/language/ca.d.ts +3 -2
  14. package/dist/language/ca.js +1 -41
  15. package/dist/language/ckb.d.ts +3 -2
  16. package/dist/language/ckb.js +1 -33
  17. package/dist/language/cs.d.ts +3 -2
  18. package/dist/language/cs.js +1 -53
  19. package/dist/language/cy.d.ts +3 -2
  20. package/dist/language/cy.js +1 -53
  21. package/dist/language/da.d.ts +3 -2
  22. package/dist/language/da.js +1 -45
  23. package/dist/language/de.d.ts +3 -2
  24. package/dist/language/de.js +1 -47
  25. package/dist/language/el.d.ts +3 -2
  26. package/dist/language/el.js +1 -53
  27. package/dist/language/en.d.ts +4 -3
  28. package/dist/language/en.js +1 -57
  29. package/dist/language/eo.d.ts +3 -2
  30. package/dist/language/eo.js +1 -47
  31. package/dist/language/es.d.ts +3 -2
  32. package/dist/language/es.js +1 -46
  33. package/dist/language/et.d.ts +3 -2
  34. package/dist/language/et.js +1 -47
  35. package/dist/language/eu.d.ts +3 -2
  36. package/dist/language/eu.js +1 -41
  37. package/dist/language/fa.d.ts +3 -2
  38. package/dist/language/fa.js +1 -38
  39. package/dist/language/fi.d.ts +3 -2
  40. package/dist/language/fi.js +1 -55
  41. package/dist/language/fo.d.ts +3 -2
  42. package/dist/language/fo.js +1 -48
  43. package/dist/language/fr.d.ts +3 -2
  44. package/dist/language/fr.js +1 -52
  45. package/dist/language/he.d.ts +3 -2
  46. package/dist/language/he.js +1 -52
  47. package/dist/language/hi.d.ts +3 -2
  48. package/dist/language/hi.js +1 -55
  49. package/dist/language/hr.d.ts +3 -2
  50. package/dist/language/hr.js +1 -110
  51. package/dist/language/hu.d.ts +3 -2
  52. package/dist/language/hu.js +1 -51
  53. package/dist/language/id.d.ts +3 -2
  54. package/dist/language/id.js +1 -50
  55. package/dist/language/is.d.ts +3 -2
  56. package/dist/language/is.js +1 -52
  57. package/dist/language/it.d.ts +3 -2
  58. package/dist/language/it.js +1 -51
  59. package/dist/language/ja.d.ts +3 -2
  60. package/dist/language/ja.js +1 -47
  61. package/dist/language/km.d.ts +3 -2
  62. package/dist/language/km.js +1 -57
  63. package/dist/language/kn.d.ts +3 -2
  64. package/dist/language/kn.js +1 -50
  65. package/dist/language/ko.d.ts +3 -2
  66. package/dist/language/ko.js +1 -42
  67. package/dist/language/ku.d.ts +3 -2
  68. package/dist/language/ku.js +1 -54
  69. package/dist/language/lo.d.ts +3 -2
  70. package/dist/language/lo.js +1 -58
  71. package/dist/language/lt.d.ts +3 -2
  72. package/dist/language/lt.js +1 -69
  73. package/dist/language/lv.d.ts +3 -2
  74. package/dist/language/lv.js +1 -55
  75. package/dist/language/mk.d.ts +3 -2
  76. package/dist/language/mk.js +1 -55
  77. package/dist/language/mn.d.ts +3 -2
  78. package/dist/language/mn.js +1 -46
  79. package/dist/language/mr.d.ts +3 -2
  80. package/dist/language/mr.js +1 -43
  81. package/dist/language/ms.d.ts +3 -2
  82. package/dist/language/ms.js +1 -49
  83. package/dist/language/nl.d.ts +3 -2
  84. package/dist/language/nl.js +1 -50
  85. package/dist/language/no.d.ts +3 -2
  86. package/dist/language/no.js +1 -47
  87. package/dist/language/pl.d.ts +3 -2
  88. package/dist/language/pl.js +1 -73
  89. package/dist/language/pt.d.ts +3 -2
  90. package/dist/language/pt.js +1 -50
  91. package/dist/language/ro.d.ts +3 -2
  92. package/dist/language/ro.js +1 -66
  93. package/dist/language/ru.d.ts +3 -2
  94. package/dist/language/ru.js +1 -59
  95. package/dist/language/sk.d.ts +3 -2
  96. package/dist/language/sk.js +1 -64
  97. package/dist/language/sl.d.ts +3 -2
  98. package/dist/language/sl.js +1 -138
  99. package/dist/language/sq.d.ts +3 -2
  100. package/dist/language/sq.js +1 -51
  101. package/dist/language/sr.d.ts +3 -2
  102. package/dist/language/sr.js +1 -83
  103. package/dist/language/sr_Latn.d.ts +3 -2
  104. package/dist/language/sr_Latn.js +1 -21
  105. package/dist/language/sv.d.ts +3 -2
  106. package/dist/language/sv.js +1 -49
  107. package/dist/language/sw.d.ts +3 -2
  108. package/dist/language/sw.js +1 -51
  109. package/dist/language/ta.d.ts +3 -2
  110. package/dist/language/ta.js +1 -54
  111. package/dist/language/te.d.ts +3 -2
  112. package/dist/language/te.js +1 -50
  113. package/dist/language/th.d.ts +3 -2
  114. package/dist/language/th.js +1 -44
  115. package/dist/language/tr.d.ts +3 -2
  116. package/dist/language/tr.js +1 -42
  117. package/dist/language/uk.d.ts +3 -2
  118. package/dist/language/uk.js +1 -61
  119. package/dist/language/ur.d.ts +3 -2
  120. package/dist/language/ur.js +1 -57
  121. package/dist/language/util/create-duration-language.d.ts +2 -2
  122. package/dist/language/util/create-duration-language.js +1 -33
  123. package/dist/language/util/duration/get-czech-or-slovak-form.d.ts +1 -1
  124. package/dist/language/util/duration/get-czech-or-slovak-form.js +1 -14
  125. package/dist/language/util/duration/get-slavic-form.d.ts +1 -1
  126. package/dist/language/util/duration/get-slavic-form.js +1 -17
  127. package/dist/language/util/validate-duration-language.d.ts +2 -2
  128. package/dist/language/util/validate-duration-language.js +1 -37
  129. package/dist/language/uz.d.ts +3 -2
  130. package/dist/language/uz.js +1 -43
  131. package/dist/language/uz_CYR.d.ts +3 -2
  132. package/dist/language/uz_CYR.js +1 -43
  133. package/dist/language/vi.d.ts +3 -2
  134. package/dist/language/vi.js +1 -34
  135. package/dist/language/zh_CN.d.ts +3 -2
  136. package/dist/language/zh_CN.js +1 -39
  137. package/dist/language/zh_TW.d.ts +3 -2
  138. package/dist/language/zh_TW.js +1 -39
  139. package/dist/packem_shared/duration-8-I9U1BK.js +1 -0
  140. package/dist/packem_shared/parseBytes-DQ0Kw2M8.js +1 -0
  141. package/dist/packem_shared/parseDuration-teGYCgRI.js +1 -0
  142. package/dist/packem_shared/types.d-DEgTgpjz.d.ts +130 -0
  143. package/package.json +20 -22
  144. package/dist/bytes.d.ts +0 -131
  145. package/dist/duration.d.ts +0 -3
  146. package/dist/packem_shared/duration-Br8Mr-Lj.js +0 -210
  147. package/dist/packem_shared/parseBytes-BkjYV2ut.js +0 -250
  148. package/dist/packem_shared/parseDuration-qO1Y4eiu.js +0 -111
  149. package/dist/parse-duration.d.ts +0 -9
  150. package/dist/types.d.ts +0 -130
package/package.json CHANGED
@@ -1,38 +1,36 @@
1
1
  {
2
2
  "name": "@visulima/humanizer",
3
- "version": "3.0.0-alpha.10",
3
+ "version": "3.0.0-alpha.12",
4
4
  "description": "Humanizer is a library for humanizing data in a human-readable form.",
5
5
  "keywords": [
6
- "visulima",
7
- "humanizer",
8
- "humanize",
9
- "human-readable",
10
- "human",
11
- "time",
12
- "ms",
13
6
  "bytes",
14
- "filesize",
15
- "filesize-humanizer",
16
- "time-humanizer",
17
- "ms-humanizer",
18
7
  "bytes-humanizer",
19
- "millisecond",
20
- "second",
8
+ "days",
21
9
  "duration",
10
+ "filesize",
11
+ "filesize-humanizer",
22
12
  "hours",
13
+ "human",
14
+ "human-readable",
15
+ "humanize",
16
+ "humanizer",
17
+ "millisecond",
23
18
  "minutes",
24
- "seconds",
25
- "days",
26
- "years",
27
19
  "months",
20
+ "ms",
21
+ "ms-humanizer",
28
22
  "range",
23
+ "second",
24
+ "seconds",
25
+ "string",
29
26
  "text",
30
- "string"
27
+ "time",
28
+ "time-humanizer",
29
+ "visulima",
30
+ "years"
31
31
  ],
32
32
  "homepage": "https://visulima.io",
33
- "bugs": {
34
- "url": "https://github.com/visulima/visulima/issues"
35
- },
33
+ "bugs": "https://github.com/visulima/visulima/issues",
36
34
  "repository": {
37
35
  "type": "git",
38
36
  "url": "git+https://github.com/visulima/visulima.git",
@@ -70,7 +68,7 @@
70
68
  "dist"
71
69
  ],
72
70
  "engines": {
73
- "node": ">=22.13 <=25.x"
71
+ "node": "^22.14.0 || >=24.10.0"
74
72
  },
75
73
  "publishConfig": {
76
74
  "access": "public",
package/dist/bytes.d.ts DELETED
@@ -1,131 +0,0 @@
1
- import type { FormateByteOptions, ParseByteOptions } from "./types.d.ts";
2
- declare const BYTE_SIZES: {
3
- readonly iec: readonly [{
4
- readonly long: "Bytes";
5
- readonly short: "B";
6
- }, {
7
- readonly long: "Kibibytes";
8
- readonly short: "KiB";
9
- }, {
10
- readonly long: "Mebibytes";
11
- readonly short: "MiB";
12
- }, {
13
- readonly long: "Gibibytes";
14
- readonly short: "GiB";
15
- }, {
16
- readonly long: "Tebibytes";
17
- readonly short: "TiB";
18
- }, {
19
- readonly long: "Pebibytes";
20
- readonly short: "PiB";
21
- }, {
22
- readonly long: "Exbibytes";
23
- readonly short: "EiB";
24
- }, {
25
- readonly long: "Zebibytes";
26
- readonly short: "ZiB";
27
- }, {
28
- readonly long: "Yobibytes";
29
- readonly short: "YiB";
30
- }];
31
- readonly iec_octet: readonly [{
32
- readonly long: "Octets";
33
- readonly short: "o";
34
- }, {
35
- readonly long: "Kibioctets";
36
- readonly short: "Kio";
37
- }, {
38
- readonly long: "Mebioctets";
39
- readonly short: "Mio";
40
- }, {
41
- readonly long: "Gibioctets";
42
- readonly short: "Gio";
43
- }, {
44
- readonly long: "Tebioctets";
45
- readonly short: "Tio";
46
- }, {
47
- readonly long: "Pebioctets";
48
- readonly short: "Pio";
49
- }, {
50
- readonly long: "Exbioctets";
51
- readonly short: "Eio";
52
- }, {
53
- readonly long: "Zebioctets";
54
- readonly short: "Zio";
55
- }, {
56
- readonly long: "Yobioctets";
57
- readonly short: "Yio";
58
- }];
59
- readonly metric: readonly [{
60
- readonly long: "Bytes";
61
- readonly short: "Bytes";
62
- }, {
63
- readonly long: "Kilobytes";
64
- readonly short: "KB";
65
- }, {
66
- readonly long: "Megabytes";
67
- readonly short: "MB";
68
- }, {
69
- readonly long: "Gigabytes";
70
- readonly short: "GB";
71
- }, {
72
- readonly long: "Terabytes";
73
- readonly short: "TB";
74
- }, {
75
- readonly long: "Petabytes";
76
- readonly short: "PB";
77
- }, {
78
- readonly long: "Exabytes";
79
- readonly short: "EB";
80
- }, {
81
- readonly long: "Zettabytes";
82
- readonly short: "ZB";
83
- }, {
84
- readonly long: "Yottabytes";
85
- readonly short: "YB";
86
- }];
87
- readonly metric_octet: readonly [{
88
- readonly long: "Octets";
89
- readonly short: "o";
90
- }, {
91
- readonly long: "Kilo-octets";
92
- readonly short: "ko";
93
- }, {
94
- readonly long: "Mega-octets";
95
- readonly short: "Mo";
96
- }, {
97
- readonly long: "Giga-octets";
98
- readonly short: "Go";
99
- }, {
100
- readonly long: "Tera-octets";
101
- readonly short: "To";
102
- }, {
103
- readonly long: "Peta-octets";
104
- readonly short: "Po";
105
- }, {
106
- readonly long: "Exa-octets";
107
- readonly short: "Eo";
108
- }, {
109
- readonly long: "Zetta-octets";
110
- readonly short: "Zo";
111
- }, {
112
- readonly long: "Yotta-octets";
113
- readonly short: "Yo";
114
- }];
115
- };
116
- type ByteSize = (typeof BYTE_SIZES)["iec_octet"][number]["short"] | (typeof BYTE_SIZES)["iec"][number]["short"] | (typeof BYTE_SIZES)["metric_octet"][number]["short"] | (typeof BYTE_SIZES)["metric"][number]["short"];
117
- /**
118
- * Parse the given bytesize string and return bytes.
119
- * @param value The string to parse
120
- * @param options Options for the conversion from string to bytes
121
- * @throws Error if `value` is not a non-empty string or a number
122
- */
123
- export declare const parseBytes: (value: string, options?: ParseByteOptions) => number;
124
- /**
125
- * Formats the given bytes into a human-readable string.
126
- * Per default, it will use the closest unit to the given value.
127
- * @param bytes The bytes to format
128
- * @param options Options for the conversion from bytes to string
129
- */
130
- export declare const formatBytes: (bytes: number, options?: FormateByteOptions<ByteSize>) => string;
131
- export {};
@@ -1,3 +0,0 @@
1
- import type { DurationOptions } from "./types.d.ts";
2
- declare const duration: (milliseconds: number, options?: DurationOptions) => string;
3
- export default duration;
@@ -1,210 +0,0 @@
1
- import { durationLanguage } from '../language/en.js';
2
- import validateDurationLanguage from '../language/util/validate-duration-language.js';
3
-
4
- const toFixed = (number_, fixed) => {
5
- fixed = fixed || -1;
6
- const matches = new RegExp(String.raw`^-?\d+(?:.\d{0,${String(fixed)}})?`).exec(number_.toString());
7
- if (matches === null) {
8
- return number_;
9
- }
10
- return Number.parseFloat(matches[0]);
11
- };
12
- const renderPiece = ({ unitCount, unitName }, language, options) => {
13
- let { spacer } = options;
14
- const { maxDecimalPoints } = options;
15
- let decimal = ".";
16
- if (options.decimal !== void 0) {
17
- decimal = options.decimal;
18
- } else if (language.decimal !== void 0) {
19
- decimal = language.decimal;
20
- }
21
- let digitReplacements;
22
- if ("digitReplacements" in options) {
23
- digitReplacements = options.digitReplacements;
24
- } else if ("_digitReplacements" in language) {
25
- digitReplacements = language._digitReplacements;
26
- }
27
- let formattedCount;
28
- let normalizedUnitCount = unitCount;
29
- if (maxDecimalPoints !== void 0) {
30
- normalizedUnitCount = toFixed(unitCount, maxDecimalPoints);
31
- }
32
- const countString = normalizedUnitCount.toString();
33
- if (!language._hideCountIf2 || unitCount !== 2) {
34
- if (digitReplacements) {
35
- formattedCount = "";
36
- for (const char of countString) {
37
- formattedCount += char === "." ? decimal : digitReplacements[char];
38
- }
39
- } else {
40
- formattedCount = countString.replace(".", decimal);
41
- }
42
- } else {
43
- formattedCount = "";
44
- }
45
- const languageWord = language[unitName];
46
- let word = languageWord;
47
- if (typeof languageWord === "function") {
48
- word = languageWord(unitCount);
49
- }
50
- if (language._hideCountIf2 && unitCount === 2) {
51
- spacer = "";
52
- }
53
- if (language._numberFirst) {
54
- return word + spacer + formattedCount;
55
- }
56
- return formattedCount + spacer + word;
57
- };
58
- const getPieces = (ms, options) => {
59
- const { units } = options;
60
- if (units.length === 0) {
61
- return [];
62
- }
63
- const { unitMeasures } = options;
64
- const largest = options.largest ?? Number.POSITIVE_INFINITY;
65
- const unitCounts = {};
66
- let unitName;
67
- let index;
68
- let unitCount;
69
- let msRemaining = ms;
70
- for (index = 0; index < units.length; index++) {
71
- unitName = units[index];
72
- const unitMs = unitMeasures[unitName];
73
- const isLast = index === units.length - 1;
74
- unitCount = isLast ? msRemaining / unitMs : Math.floor(msRemaining / unitMs);
75
- unitCounts[unitName] = unitCount;
76
- msRemaining -= unitCount * unitMs;
77
- }
78
- if (options.round) {
79
- let unitsRemainingBeforeRound = largest;
80
- for (index = 0; index < units.length; index++) {
81
- unitName = units[index];
82
- unitCount = unitCounts[unitName];
83
- if (unitCount === 0) {
84
- continue;
85
- }
86
- unitsRemainingBeforeRound--;
87
- if (unitsRemainingBeforeRound === 0) {
88
- for (let index_ = index + 1; index_ < units.length; index_++) {
89
- const smallerUnitName = units[index_];
90
- const smallerUnitCount = unitCounts[smallerUnitName];
91
- unitCounts[unitName] = (unitCounts[unitName] ?? 0) + smallerUnitCount * unitMeasures[smallerUnitName] / unitMeasures[unitName];
92
- unitCounts[smallerUnitName] = 0;
93
- }
94
- break;
95
- }
96
- }
97
- for (index = units.length - 1; index >= 0; index--) {
98
- unitName = units[index];
99
- unitCount = unitCounts[unitName];
100
- if (unitCount === 0) {
101
- continue;
102
- }
103
- const rounded = Math.round(unitCount);
104
- unitCounts[unitName] = rounded;
105
- if (index === 0) {
106
- break;
107
- }
108
- const previousUnitName = units[index - 1];
109
- const previousUnitMs = unitMeasures[previousUnitName];
110
- const amountOfPreviousUnit = Math.floor(rounded * unitMeasures[unitName] / previousUnitMs);
111
- if (amountOfPreviousUnit) {
112
- unitCounts[previousUnitName] = (unitCounts[previousUnitName] ?? 0) + amountOfPreviousUnit;
113
- unitCounts[unitName] = 0;
114
- } else {
115
- break;
116
- }
117
- }
118
- }
119
- const result = [];
120
- for (index = 0; index < units.length && result.length < largest; index++) {
121
- unitName = units[index];
122
- unitCount = unitCounts[unitName];
123
- if (unitCount && !options.round && result.length === largest - 1) {
124
- let index_;
125
- let remainder = 0;
126
- for (index_ = index + 1, units.length; index_ < units.length; index_++) {
127
- const remainderUnitName = units[index_];
128
- remainder += unitCounts[remainderUnitName] * (options.unitMeasures[remainderUnitName] / options.unitMeasures[unitName]);
129
- }
130
- unitCount += remainder;
131
- if (options.maxDecimalPoints !== void 0) {
132
- unitCount = toFixed(unitCount, options.maxDecimalPoints);
133
- }
134
- }
135
- if (unitCount) {
136
- result.push({ unitCount, unitName });
137
- }
138
- }
139
- return result;
140
- };
141
- const formatPieces = (pieces, options, ms) => {
142
- const { language, units } = options;
143
- if (pieces.length === 0) {
144
- const smallestUnitName = units.at(-1);
145
- return renderPiece({ unitCount: 0, unitName: smallestUnitName }, language, options);
146
- }
147
- const { conjunction, serialComma } = options;
148
- let delimiter = ", ";
149
- if (options.delimiter !== void 0) {
150
- delimiter = options.delimiter;
151
- } else if (language.delimiter !== void 0) {
152
- delimiter = language.delimiter;
153
- }
154
- let adverb = "";
155
- if (options.timeAdverb && ms !== 0) {
156
- adverb = language.future ?? "";
157
- if (ms < 0) {
158
- adverb = language.past ?? "";
159
- }
160
- }
161
- const renderedPieces = pieces.map((piece) => renderPiece(piece, language, options));
162
- let result;
163
- if (!conjunction || pieces.length === 1) {
164
- result = renderedPieces.join(delimiter);
165
- } else if (pieces.length === 2) {
166
- result = renderedPieces.join(conjunction);
167
- } else {
168
- result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + (renderedPieces.at(-1) ?? "");
169
- }
170
- if (adverb) {
171
- result = adverb.replace("%s", result);
172
- }
173
- return result;
174
- };
175
- const duration = (milliseconds, options) => {
176
- if (Number.isNaN(milliseconds)) {
177
- throw new TypeError("Expected a valid number");
178
- }
179
- if (typeof milliseconds !== "number") {
180
- throw new TypeError("Expected a number for milliseconds input");
181
- }
182
- const config = {
183
- conjunction: "",
184
- language: durationLanguage,
185
- round: false,
186
- serialComma: true,
187
- spacer: " ",
188
- timeAdverb: false,
189
- unitMeasures: {
190
- d: 864e5,
191
- h: 36e5,
192
- m: 6e4,
193
- mo: 2629746e3,
194
- // 365.2425 / 12 = 30.436875 days
195
- ms: 1,
196
- s: 1e3,
197
- w: 6048e5,
198
- y: 31556952e3
199
- // 365 + 1/4 - 1/100 + 1/400 (actual leap day rules) = 365.2425 days
200
- },
201
- units: ["w", "d", "h", "m", "s"],
202
- ...options
203
- };
204
- validateDurationLanguage(config.language);
205
- const absTime = Math.abs(milliseconds);
206
- const pieces = getPieces(absTime, config);
207
- return formatPieces(pieces, config, milliseconds);
208
- };
209
-
210
- export { duration as default };
@@ -1,250 +0,0 @@
1
- const PARSE_BYTES_REGEX = /^(?<value>-?\d+(?:[.,]\d+)*) *(?<type>[a-z]+)?$/i;
2
- const KIBI_REGEX = /^KIBI/;
3
- const MIBI_REGEX = /^MIBI/;
4
- const GIBI_REGEX = /^GIBI/;
5
- const TEBI_REGEX = /^TEBI/;
6
- const PEBI_REGEX = /^PEBI/;
7
- const EXBI_REGEX = /^EXBI/;
8
- const ZEBI_REGEX = /^ZEBI/;
9
- const YIBI_REGEX = /^YIBI/;
10
- const IB_SUFFIX_REGEX = /^(.)IB$/;
11
- const BYTE_SIZES = {
12
- iec: [
13
- {
14
- long: "Bytes",
15
- short: "B"
16
- },
17
- {
18
- long: "Kibibytes",
19
- short: "KiB"
20
- },
21
- {
22
- long: "Mebibytes",
23
- short: "MiB"
24
- },
25
- {
26
- long: "Gibibytes",
27
- short: "GiB"
28
- },
29
- {
30
- long: "Tebibytes",
31
- short: "TiB"
32
- },
33
- {
34
- long: "Pebibytes",
35
- short: "PiB"
36
- },
37
- {
38
- long: "Exbibytes",
39
- short: "EiB"
40
- },
41
- {
42
- long: "Zebibytes",
43
- short: "ZiB"
44
- },
45
- {
46
- long: "Yobibytes",
47
- short: "YiB"
48
- }
49
- ],
50
- iec_octet: [
51
- {
52
- long: "Octets",
53
- short: "o"
54
- },
55
- {
56
- long: "Kibioctets",
57
- short: "Kio"
58
- },
59
- {
60
- long: "Mebioctets",
61
- short: "Mio"
62
- },
63
- {
64
- long: "Gibioctets",
65
- short: "Gio"
66
- },
67
- {
68
- long: "Tebioctets",
69
- short: "Tio"
70
- },
71
- {
72
- long: "Pebioctets",
73
- short: "Pio"
74
- },
75
- {
76
- long: "Exbioctets",
77
- short: "Eio"
78
- },
79
- {
80
- long: "Zebioctets",
81
- short: "Zio"
82
- },
83
- {
84
- long: "Yobioctets",
85
- short: "Yio"
86
- }
87
- ],
88
- metric: [
89
- {
90
- long: "Bytes",
91
- short: "Bytes"
92
- },
93
- {
94
- long: "Kilobytes",
95
- short: "KB"
96
- },
97
- {
98
- long: "Megabytes",
99
- short: "MB"
100
- },
101
- {
102
- long: "Gigabytes",
103
- short: "GB"
104
- },
105
- {
106
- long: "Terabytes",
107
- short: "TB"
108
- },
109
- {
110
- long: "Petabytes",
111
- short: "PB"
112
- },
113
- {
114
- long: "Exabytes",
115
- short: "EB"
116
- },
117
- {
118
- long: "Zettabytes",
119
- short: "ZB"
120
- },
121
- {
122
- long: "Yottabytes",
123
- short: "YB"
124
- }
125
- ],
126
- metric_octet: [
127
- {
128
- long: "Octets",
129
- short: "o"
130
- },
131
- {
132
- long: "Kilo-octets",
133
- short: "ko"
134
- },
135
- {
136
- long: "Mega-octets",
137
- short: "Mo"
138
- },
139
- {
140
- long: "Giga-octets",
141
- short: "Go"
142
- },
143
- {
144
- long: "Tera-octets",
145
- short: "To"
146
- },
147
- {
148
- long: "Peta-octets",
149
- short: "Po"
150
- },
151
- {
152
- long: "Exa-octets",
153
- short: "Eo"
154
- },
155
- {
156
- long: "Zetta-octets",
157
- short: "Zo"
158
- },
159
- {
160
- long: "Yotta-octets",
161
- short: "Yo"
162
- }
163
- ]
164
- };
165
- const parseLocalizedNumber = (stringNumber, locale) => {
166
- const thousandSeparator = new Intl.NumberFormat(locale).format(11111).replaceAll(new RegExp("\\p{Number}", "gu"), "");
167
- const decimalSeparator = new Intl.NumberFormat(locale).format(1.1).replaceAll(new RegExp("\\p{Number}", "gu"), "");
168
- return Number.parseFloat(stringNumber.replaceAll(new RegExp(`\\${thousandSeparator}`, "g"), "").replace(new RegExp(`\\${decimalSeparator}`), "."));
169
- };
170
- const fromBase = (base) => {
171
- switch (base) {
172
- case 2: {
173
- return 1024;
174
- }
175
- case 10: {
176
- return 1e3;
177
- }
178
- default: {
179
- throw new Error("Unsupported base.");
180
- }
181
- }
182
- };
183
- const parseBytes = (value, options) => {
184
- const config = {
185
- base: 2,
186
- locale: "en-US",
187
- units: "metric",
188
- ...options
189
- };
190
- if (typeof value !== "string" || value.length === 0) {
191
- throw new TypeError("Value is not a string or is empty.");
192
- }
193
- if (value.length > 100) {
194
- throw new TypeError("Value exceeds the maximum length of 100 characters.");
195
- }
196
- const match = PARSE_BYTES_REGEX.exec(value);
197
- const groups = match?.groups;
198
- if (!groups) {
199
- return Number.NaN;
200
- }
201
- const localizedNumber = parseLocalizedNumber(groups.value, config.locale);
202
- const type = (groups.type ?? "Bytes").toUpperCase().replace(KIBI_REGEX, "KILO").replace(MIBI_REGEX, "MEGA").replace(GIBI_REGEX, "GIGA").replace(TEBI_REGEX, "TERA").replace(PEBI_REGEX, "PETA").replace(EXBI_REGEX, "EXA").replace(ZEBI_REGEX, "ZETTA").replace(YIBI_REGEX, "YOTTA").replace(IB_SUFFIX_REGEX, "$1B");
203
- const level = BYTE_SIZES[config.units].findIndex((unit) => unit.short[0].toUpperCase() === type[0]);
204
- const base = fromBase(config.base);
205
- return localizedNumber * base ** level;
206
- };
207
- const formatBytes = (bytes, options) => {
208
- if (typeof bytes !== "number" || !Number.isFinite(bytes)) {
209
- throw new TypeError("Bytesize is not a number.");
210
- }
211
- const {
212
- base: givenBase,
213
- decimals,
214
- locale,
215
- long,
216
- unit: requestedUnit,
217
- units,
218
- ...l10nOptions
219
- } = {
220
- base: 2,
221
- decimals: 0,
222
- locale: "en-US",
223
- long: false,
224
- units: "metric",
225
- ...options
226
- };
227
- const base = fromBase(givenBase);
228
- const absoluteBytes = Math.abs(bytes);
229
- const space = options?.space ?? true ? " " : "";
230
- const referenceTable = BYTE_SIZES[units];
231
- const requestedUnitIndex = referenceTable.findIndex((unit2) => unit2.short === requestedUnit);
232
- if (bytes === 0) {
233
- const level2 = Math.min(0, Math.max(requestedUnitIndex, referenceTable.length - 1));
234
- return "0" + space + referenceTable[level2][long ? "long" : "short"];
235
- }
236
- const level = requestedUnitIndex === -1 ? Math.min(Math.floor(Math.log(absoluteBytes) / Math.log(base)), referenceTable.length - 1) : requestedUnitIndex;
237
- const unit = referenceTable[level][long ? "long" : "short"];
238
- const value = bytes / base ** level;
239
- const fractionDigits = decimals < 0 ? void 0 : decimals;
240
- const formattedValue = new Intl.NumberFormat(locale, {
241
- // @ts-expect-error - should be overridden by the options
242
- maximumFractionDigits: fractionDigits,
243
- // @ts-expect-error - should be overridden by the options
244
- minimumFractionDigits: fractionDigits,
245
- ...l10nOptions
246
- }).format(value);
247
- return formattedValue + space + unit;
248
- };
249
-
250
- export { formatBytes, parseBytes };