@visulima/humanizer 2.0.5 → 3.0.0-alpha.10

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 (79) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +32 -14
  3. package/dist/index.js +3 -1
  4. package/dist/language/af.js +41 -1
  5. package/dist/language/am.js +33 -1
  6. package/dist/language/ar.js +53 -1
  7. package/dist/language/bg.js +42 -1
  8. package/dist/language/bn.js +33 -1
  9. package/dist/language/ca.js +41 -1
  10. package/dist/language/ckb.js +33 -1
  11. package/dist/language/cs.js +53 -1
  12. package/dist/language/cy.js +53 -1
  13. package/dist/language/da.js +45 -1
  14. package/dist/language/de.js +47 -1
  15. package/dist/language/el.js +53 -1
  16. package/dist/language/en.js +57 -1
  17. package/dist/language/eo.js +47 -1
  18. package/dist/language/es.js +46 -1
  19. package/dist/language/et.js +47 -1
  20. package/dist/language/eu.js +41 -1
  21. package/dist/language/fa.js +38 -1
  22. package/dist/language/fi.js +55 -1
  23. package/dist/language/fo.js +48 -1
  24. package/dist/language/fr.js +52 -1
  25. package/dist/language/he.js +52 -1
  26. package/dist/language/hi.js +55 -1
  27. package/dist/language/hr.js +110 -1
  28. package/dist/language/hu.js +51 -1
  29. package/dist/language/id.js +50 -1
  30. package/dist/language/is.js +52 -1
  31. package/dist/language/it.js +51 -1
  32. package/dist/language/ja.js +47 -1
  33. package/dist/language/km.js +57 -1
  34. package/dist/language/kn.js +50 -1
  35. package/dist/language/ko.js +42 -1
  36. package/dist/language/ku.js +54 -1
  37. package/dist/language/lo.js +58 -1
  38. package/dist/language/lt.js +69 -1
  39. package/dist/language/lv.js +55 -1
  40. package/dist/language/mk.js +55 -1
  41. package/dist/language/mn.js +46 -1
  42. package/dist/language/mr.js +43 -1
  43. package/dist/language/ms.js +49 -1
  44. package/dist/language/nl.js +50 -1
  45. package/dist/language/no.js +47 -1
  46. package/dist/language/pl.js +73 -1
  47. package/dist/language/pt.js +50 -1
  48. package/dist/language/ro.js +66 -1
  49. package/dist/language/ru.js +59 -1
  50. package/dist/language/sk.js +64 -1
  51. package/dist/language/sl.js +138 -1
  52. package/dist/language/sq.js +51 -1
  53. package/dist/language/sr.js +83 -1
  54. package/dist/language/sr_Latn.d.ts +1 -0
  55. package/dist/language/sr_Latn.js +21 -1
  56. package/dist/language/sv.js +49 -1
  57. package/dist/language/sw.js +51 -1
  58. package/dist/language/ta.js +54 -1
  59. package/dist/language/te.js +50 -1
  60. package/dist/language/th.js +44 -1
  61. package/dist/language/tr.js +42 -1
  62. package/dist/language/uk.js +61 -1
  63. package/dist/language/ur.js +57 -1
  64. package/dist/language/util/create-duration-language.js +33 -1
  65. package/dist/language/util/duration/get-czech-or-slovak-form.js +14 -1
  66. package/dist/language/util/duration/get-slavic-form.js +17 -1
  67. package/dist/language/util/validate-duration-language.js +37 -1
  68. package/dist/language/uz.js +43 -1
  69. package/dist/language/uz_CYR.js +43 -1
  70. package/dist/language/vi.js +34 -1
  71. package/dist/language/zh_CN.js +39 -1
  72. package/dist/language/zh_TW.js +39 -1
  73. package/dist/packem_shared/duration-Br8Mr-Lj.js +210 -0
  74. package/dist/packem_shared/parseBytes-BkjYV2ut.js +250 -0
  75. package/dist/packem_shared/parseDuration-qO1Y4eiu.js +111 -0
  76. package/package.json +3 -3
  77. package/dist/packem_shared/duration-B8tBhwXw.js +0 -1
  78. package/dist/packem_shared/parseBytes-Da87aeh8.js +0 -1
  79. package/dist/packem_shared/parseDuration-Bo02kq8G.js +0 -1
@@ -0,0 +1,210 @@
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 };
@@ -0,0 +1,250 @@
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 };
@@ -0,0 +1,111 @@
1
+ import { durationLanguage, englishUnitMap } from '../language/en.js';
2
+ import validateDurationLanguage from '../language/util/validate-duration-language.js';
3
+
4
+ const STANDARD_UNIT_MEASURES = {
5
+ d: 864e5,
6
+ h: 36e5,
7
+ m: 6e4,
8
+ mo: 2629746e3,
9
+ ms: 1,
10
+ s: 1e3,
11
+ w: 6048e5,
12
+ y: 31556952e3
13
+ };
14
+ const ESCAPE_REGEX = /[-/\\^$*+?.()|[\]{}]/g;
15
+ const ISO_FORMAT = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/i;
16
+ const COLON_FORMAT = /^(?:(\d+):)?(?:(\d+):)?(\d+)$/;
17
+ const NUMERIC_STRING_REGEX = /^[+-]?\d+(?:\.\d+)?$/;
18
+ const SIGN_PREFIX_REGEX = /^[-+]/;
19
+ const parseDuration = (value, options) => {
20
+ if (typeof value !== "string" || value.length === 0) {
21
+ return void 0;
22
+ }
23
+ const { defaultUnit = "ms", language = durationLanguage } = options ?? {};
24
+ validateDurationLanguage(language);
25
+ const decimalSeparator = language.decimal ?? ".";
26
+ const groupSeparator = language.groupSeparator ?? ",";
27
+ const placeholderSeparator = language.placeholderSeparator ?? "_";
28
+ const escapedDecimal = decimalSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
29
+ const escapedGroup = groupSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
30
+ const escapedPlaceholder = placeholderSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
31
+ const currentUnitMap = language.unitMap ?? englishUnitMap;
32
+ let processedValue = value.replaceAll(new RegExp(String.raw`(\d)[${escapedPlaceholder}${escapedGroup}](\d)`, "g"), "$1$2");
33
+ if (decimalSeparator !== ".") {
34
+ processedValue = processedValue.replace(escapedDecimal, ".");
35
+ }
36
+ if (NUMERIC_STRING_REGEX.test(value)) {
37
+ const numberOnly = Number.parseFloat(processedValue.trim());
38
+ if (!Number.isNaN(numberOnly)) {
39
+ const unitKey = currentUnitMap[defaultUnit];
40
+ if (unitKey !== void 0) {
41
+ return numberOnly * STANDARD_UNIT_MEASURES[unitKey];
42
+ }
43
+ }
44
+ return void 0;
45
+ }
46
+ const isoMatch = ISO_FORMAT.exec(value);
47
+ if (isoMatch) {
48
+ const hours = Number.parseInt(isoMatch[1] ?? "0", 10);
49
+ const minutes = Number.parseInt(isoMatch[2] ?? "0", 10);
50
+ const seconds = Number.parseInt(isoMatch[3] ?? "0", 10);
51
+ return hours * 36e5 + minutes * 6e4 + seconds * 1e3;
52
+ }
53
+ const colonMatch = COLON_FORMAT.exec(value);
54
+ if (colonMatch) {
55
+ let hours = 0;
56
+ let minutes = 0;
57
+ if (colonMatch[2] !== void 0) {
58
+ hours = Number.parseInt(colonMatch[1] ?? "0", 10);
59
+ minutes = Number.parseInt(colonMatch[2], 10);
60
+ } else if (colonMatch[1] !== void 0) {
61
+ minutes = Number.parseInt(colonMatch[1], 10);
62
+ }
63
+ const seconds = Number.parseInt(colonMatch[3] ?? "0", 10);
64
+ return hours * 36e5 + minutes * 6e4 + seconds * 1e3;
65
+ }
66
+ const currentUnitMapKeys = Object.keys(currentUnitMap);
67
+ const regexKeys = currentUnitMapKeys.toSorted((a, b) => b.length - a.length).map((k) => k.replaceAll(ESCAPE_REGEX, String.raw`\$&`)).join("|");
68
+ const durationRegex = new RegExp(String.raw`(-?\d*\.?\d+)\s*(${regexKeys})`, "gi");
69
+ let totalMs = 0;
70
+ let match;
71
+ let unitsFound = false;
72
+ let firstMatchIndex = -1;
73
+ let lastMatchEndIndex = 0;
74
+ durationRegex.lastIndex = 0;
75
+ while ((match = durationRegex.exec(processedValue)) !== null) {
76
+ if (!unitsFound) {
77
+ firstMatchIndex = match.index;
78
+ }
79
+ unitsFound = true;
80
+ const numberString = match[1];
81
+ const unitString = match[2];
82
+ if (!numberString || !unitString) {
83
+ continue;
84
+ }
85
+ const trimmedNumberString = numberString.trim();
86
+ const sign = trimmedNumberString.startsWith("-") ? -1 : 1;
87
+ const absNumberString = trimmedNumberString.replace(SIGN_PREFIX_REGEX, "");
88
+ const parsedNumber = Number.parseFloat(absNumberString);
89
+ const unitKey = currentUnitMap[unitString.toLowerCase()];
90
+ if (unitKey === void 0) {
91
+ continue;
92
+ }
93
+ const unitValue = STANDARD_UNIT_MEASURES[unitKey];
94
+ if (Number.isNaN(parsedNumber)) {
95
+ return void 0;
96
+ }
97
+ totalMs += sign * parsedNumber * unitValue;
98
+ lastMatchEndIndex = durationRegex.lastIndex;
99
+ }
100
+ const leadingText = processedValue.slice(0, firstMatchIndex).trim();
101
+ const trailingText = processedValue.slice(lastMatchEndIndex).trim();
102
+ if (unitsFound && (leadingText.length > 0 || trailingText.length > 0)) {
103
+ return void 0;
104
+ }
105
+ if (!unitsFound) {
106
+ return void 0;
107
+ }
108
+ return totalMs;
109
+ };
110
+
111
+ export { parseDuration as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/humanizer",
3
- "version": "2.0.5",
3
+ "version": "3.0.0-alpha.10",
4
4
  "description": "Humanizer is a library for humanizing data in a human-readable form.",
5
5
  "keywords": [
6
6
  "visulima",
@@ -36,7 +36,7 @@
36
36
  "repository": {
37
37
  "type": "git",
38
38
  "url": "git+https://github.com/visulima/visulima.git",
39
- "directory": "packages/humanizer"
39
+ "directory": "packages/data-manipulation/humanizer"
40
40
  },
41
41
  "funding": [
42
42
  {
@@ -70,7 +70,7 @@
70
70
  "dist"
71
71
  ],
72
72
  "engines": {
73
- "node": ">=20.19 <=25.x"
73
+ "node": ">=22.13 <=25.x"
74
74
  },
75
75
  "publishConfig": {
76
76
  "access": "public",
@@ -1 +0,0 @@
1
- var x=Object.defineProperty;var b=(o,n)=>x(o,"name",{value:n,configurable:!0});import{durationLanguage as M}from"../language/en.js";import P from"../language/util/validate-duration-language.js";var w=Object.defineProperty,g=b((o,n)=>w(o,"name",{value:n,configurable:!0}),"g");const N=g((o,n)=>{n=n||-1;const e=new RegExp(`^-?\\d+(?:.\\d{0,${String(n)}})?`).exec(o.toString());return e===null?o:Number.parseFloat(e[0])},"toFixed"),v=g(({unitCount:o,unitName:n},e,l)=>{let{spacer:f}=l;const{maxDecimalPoints:s}=l;let r=".";l.decimal!==void 0?r=l.decimal:e.decimal!==void 0&&(r=e.decimal);let t;"digitReplacements"in l?t=l.digitReplacements:"_digitReplacements"in e&&(t=e._digitReplacements);let i,m=o;s!==void 0&&(m=N(o,s));const c=m.toString();if(!e._hideCountIf2||o!==2)if(t){i="";for(const d of c)i+=d==="."?r:t[d]}else i=c.replace(".",r);else i="";const u=e[n];let a=u;return typeof u=="function"&&(a=u(o)),e._hideCountIf2&&o===2&&(f=""),e._numberFirst?a+f+i:i+f+a},"renderPiece"),C=g((o,n)=>{const{units:e}=n;if(e.length===0)return[];const{unitMeasures:l}=n,f=n.largest??Number.POSITIVE_INFINITY,s={};let r,t,i,m=o;for(t=0;t<e.length;t++){r=e[t];const u=l[r];i=t===e.length-1?m/u:Math.floor(m/u),s[r]=i,m-=i*u}if(n.round){let u=f;for(t=0;t<e.length;t++)if(r=e[t],i=s[r],i!==0&&(u--,u===0)){for(let a=t+1;a<e.length;a++){const d=e[a],h=s[d];s[r]+=h*l[d]/l[r],s[d]=0}break}for(t=e.length-1;t>=0;t--){if(r=e[t],i=s[r],i===0)continue;const a=Math.round(i);if(s[r]=a,t===0)break;const d=e[t-1],h=l[d],p=Math.floor(a*l[r]/h);if(p)s[d]+=p,s[r]=0;else break}}const c=[];for(t=0;t<e.length&&c.length<f;t++){if(r=e[t],i=s[r],i&&!n.round&&c.length===f-1){let u,a=0;for(u=t+1,e.length;u<e.length;u++){const d=e[u];a+=s[d]*(n.unitMeasures[d]/n.unitMeasures[r])}i+=a,n.maxDecimalPoints!==void 0&&(i=N(i,n.maxDecimalPoints))}i&&c.push({unitCount:i,unitName:r})}return c},"getPieces"),I=g((o,n,e)=>{const{language:l,units:f}=n;if(o.length===0){const u=f.at(-1);return v({unitCount:0,unitName:u},l,n)}const{conjunction:s,serialComma:r}=n;let t=", ";n.delimiter!==void 0?t=n.delimiter:l.delimiter!==void 0&&(t=l.delimiter);let i="";n.timeAdverb&&e!==0&&(i=l.future??"",e<0&&(i=l.past??""));const m=[];for(const u of o){const a=u;m.push(v(a,l,n))}let c;return!s||o.length===1?c=m.join(t):o.length===2?c=m.join(s):c=m.slice(0,-1).join(t)+(r?",":"")+s+m.at(-1),i&&(c=i.replace("%s",c)),c},"formatPieces"),R=g((o,n)=>{if(Number.isNaN(o))throw new TypeError("Expected a valid number");if(typeof o!="number")throw new TypeError("Expected a number for milliseconds input");const e={conjunction:"",language:M,round:!1,serialComma:!0,spacer:" ",timeAdverb:!1,unitMeasures:{d:864e5,h:36e5,m:6e4,mo:2629746e3,ms:1,s:1e3,w:6048e5,y:31556952e3},units:["w","d","h","m","s"],...n};P(e.language);const l=Math.abs(o),f=C(l,e);return I(f,e,o)},"duration");export{R as default};
@@ -1 +0,0 @@
1
- var M=Object.defineProperty;var B=(e,t)=>M(e,"name",{value:t,configurable:!0});var d=Object.defineProperty,n=B((e,t)=>d(e,"name",{value:t,configurable:!0}),"i");const E={iec:[{long:"Bytes",short:"B"},{long:"Kibibytes",short:"KiB"},{long:"Mebibytes",short:"MiB"},{long:"Gibibytes",short:"GiB"},{long:"Tebibytes",short:"TiB"},{long:"Pebibytes",short:"PiB"},{long:"Exbibytes",short:"EiB"},{long:"Zebibytes",short:"ZiB"},{long:"Yobibytes",short:"YiB"}],iec_octet:[{long:"Octets",short:"o"},{long:"Kibioctets",short:"Kio"},{long:"Mebioctets",short:"Mio"},{long:"Gibioctets",short:"Gio"},{long:"Tebioctets",short:"Tio"},{long:"Pebioctets",short:"Pio"},{long:"Exbioctets",short:"Eio"},{long:"Zebioctets",short:"Zio"},{long:"Yobioctets",short:"Yio"}],metric:[{long:"Bytes",short:"Bytes"},{long:"Kilobytes",short:"KB"},{long:"Megabytes",short:"MB"},{long:"Gigabytes",short:"GB"},{long:"Terabytes",short:"TB"},{long:"Petabytes",short:"PB"},{long:"Exabytes",short:"EB"},{long:"Zettabytes",short:"ZB"},{long:"Yottabytes",short:"YB"}],metric_octet:[{long:"Octets",short:"o"},{long:"Kilo-octets",short:"ko"},{long:"Mega-octets",short:"Mo"},{long:"Giga-octets",short:"Go"},{long:"Tera-octets",short:"To"},{long:"Peta-octets",short:"Po"},{long:"Exa-octets",short:"Eo"},{long:"Zetta-octets",short:"Zo"},{long:"Yotta-octets",short:"Yo"}]},w=n((e,t)=>{const r=new Intl.NumberFormat(t).format(11111).replaceAll(new RegExp("\\p{Number}","gu"),""),o=new Intl.NumberFormat(t).format(1.1).replaceAll(new RegExp("\\p{Number}","gu"),"");return Number.parseFloat(e.replaceAll(new RegExp(`\\${r}`,"g"),"").replace(new RegExp(`\\${o}`),"."))},"parseLocalizedNumber"),f=n(e=>{if(e===2)return 1024;if(e===10)return 1e3;throw new TypeError("Unsupported base.")},"fromBase"),N=n((e,t)=>{const r={base:2,locale:"en-US",units:"metric",...t};if(typeof e!="string"||e.length===0)throw new TypeError("Value is not a string or is empty.");if(e.length>100)throw new TypeError("Value exceeds the maximum length of 100 characters.");const o=/^(?<value>-?(?:\d+(([.,])\d+)*)?[.,]?\d+) *(?<type>bytes?|b|kb|kib|mb|mib|gb|gib|tb|tib|pb|pib|eb|eib|zb|zib|yb|yib|(kilo|kibi|mega|mebi|giga|gibi|tera|tebi|peta|pebi|exa|exbi|zetta|zebi|yotta|yobi)?bytes)?$/i.exec(e)?.groups;if(!o)return Number.NaN;const a=w(o.value,r.locale),i=(o.type??"Bytes").toUpperCase().replace(/^KIBI/,"KILO").replace(/^MIBI/,"MEGA").replace(/^GIBI/,"GIGA").replace(/^TEBI/,"TERA").replace(/^PEBI/,"PETA").replace(/^EXBI/,"EXA").replace(/^ZEBI/,"ZETTA").replace(/^YIBI/,"YOTTA").replace(/^(.)IB$/,"$1B"),l=E[r.units].findIndex(g=>g.short[0].toUpperCase()===i[0]),b=f(r.base);return a*b**l},"parseBytes"),P=n((e,t)=>{if(typeof e!="number"||!Number.isFinite(e))throw new TypeError("Bytesize is not a number.");const{base:r,decimals:o,locale:a,long:i,unit:l,units:b,...g}={base:2,decimals:0,locale:"en-US",long:!1,units:"metric",...t},p=f(r),I=Math.abs(e),m=t?.space??!0?" ":"",s=E[b],c=s.findIndex(h=>h.short===l);if(e===0){const h=Math.min(0,Math.max(c,s.length-1));return"0"+m+s[h][i?"long":"short"]}const y=c===-1?Math.min(Math.floor(Math.log(I)/Math.log(p)),s.length-1):c,T=s[y][i?"long":"short"],x=e/p**y,u=o<0?void 0:o;return new Intl.NumberFormat(a,{maximumFractionDigits:u,minimumFractionDigits:u,...g}).format(x)+m+T},"formatBytes");export{P as formatBytes,N as parseBytes};
@@ -1 +0,0 @@
1
- var U=Object.defineProperty;var x=(t,s)=>U(t,"name",{value:s,configurable:!0});import{durationLanguage as k,englishUnitMap as D}from"../language/en.js";import P from"../language/util/validate-duration-language.js";var T=Object.defineProperty,_=x((t,s)=>T(t,"name",{value:s,configurable:!0}),"h");const I={d:864e5,h:36e5,m:6e4,mo:2629746e3,ms:1,s:1e3,w:6048e5,y:31556952e3},p=/[-/\\^$*+?.()|[\]{}]/g,C=/^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/i,G=/^(?:(\d+):)?(?:(\d+):)?(\d+)$/,H=/^[+-]?\d+(?:\.\d+)?$/,B=_((t,s)=>{if(typeof t!="string"||t.length===0)return;const{defaultUnit:w="ms",language:l=k}=s??{};P(l);const g=l.decimal??".",S=l.groupSeparator??",",v=l.placeholderSeparator??"_",A=g.replaceAll(p,String.raw`\$&`),y=S.replaceAll(p,String.raw`\$&`),j=v.replaceAll(p,String.raw`\$&`),d=l.unitMap??D;let i=t.replaceAll(new RegExp(`(\\d)[${j}${y}](\\d)`,"g"),"$1$2");if(g!=="."&&(i=i.replace(A,".")),H.test(t)){const e=Number.parseFloat(i.trim());if(!Number.isNaN(e)){const r=d[w];if(r!==void 0)return e*I[r]}return}const o=C.exec(t);if(o){const e=Number.parseInt(o[1]??"0",10),r=Number.parseInt(o[2]??"0",10),n=Number.parseInt(o[3]??"0",10);return e*36e5+r*6e4+n*1e3}const a=G.exec(t);if(a){let e=0,r=0,n=0;return a[2]!==void 0?(e=Number.parseInt(a[1]??"0",10),r=Number.parseInt(a[2],10)):a[1]!==void 0&&(r=Number.parseInt(a[1],10)),n=Number.parseInt(a[3]??"0",10),e*36e5+r*6e4+n*1e3}const M=Object.keys(d).toSorted((e,r)=>r.length-e.length).map(e=>e.replaceAll(p,String.raw`\$&`)).join("|"),m=new RegExp(`(-?\\d*\\.?\\d+)\\s*(${M})`,"gi");let f=0,u,c=!1,N=-1,b=0;for(m.lastIndex=0;(u=m.exec(i))!==null;){c||(N=u.index),c=!0;const e=u[1],r=u[2];if(!e||!r)continue;const n=e.trim(),L=n.startsWith("-")?-1:1,O=n.replace(/^[-+]/,""),$=Number.parseFloat(O),h=d[r.toLowerCase()];if(h===void 0)continue;const R=I[h];if(Number.isNaN($))return;f+=L*$*R,b=m.lastIndex}const E=i.slice(0,N).trim(),F=i.slice(b).trim();if(!(c&&(E.length>0||F.length>0))&&c)return f},"parseDuration");export{B as default};