@visulima/humanizer 2.0.5 → 3.0.0-alpha.2

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 (77) hide show
  1. package/README.md +32 -14
  2. package/dist/index.js +3 -1
  3. package/dist/language/af.js +41 -1
  4. package/dist/language/am.js +33 -1
  5. package/dist/language/ar.js +53 -1
  6. package/dist/language/bg.js +42 -1
  7. package/dist/language/bn.js +33 -1
  8. package/dist/language/ca.js +41 -1
  9. package/dist/language/ckb.js +33 -1
  10. package/dist/language/cs.js +53 -1
  11. package/dist/language/cy.js +53 -1
  12. package/dist/language/da.js +45 -1
  13. package/dist/language/de.js +47 -1
  14. package/dist/language/el.js +53 -1
  15. package/dist/language/en.js +57 -1
  16. package/dist/language/eo.js +47 -1
  17. package/dist/language/es.js +46 -1
  18. package/dist/language/et.js +47 -1
  19. package/dist/language/eu.js +41 -1
  20. package/dist/language/fa.js +38 -1
  21. package/dist/language/fi.js +55 -1
  22. package/dist/language/fo.js +48 -1
  23. package/dist/language/fr.js +52 -1
  24. package/dist/language/he.js +52 -1
  25. package/dist/language/hi.js +55 -1
  26. package/dist/language/hr.js +110 -1
  27. package/dist/language/hu.js +51 -1
  28. package/dist/language/id.js +50 -1
  29. package/dist/language/is.js +52 -1
  30. package/dist/language/it.js +51 -1
  31. package/dist/language/ja.js +47 -1
  32. package/dist/language/km.js +57 -1
  33. package/dist/language/kn.js +50 -1
  34. package/dist/language/ko.js +42 -1
  35. package/dist/language/ku.js +54 -1
  36. package/dist/language/lo.js +58 -1
  37. package/dist/language/lt.js +69 -1
  38. package/dist/language/lv.js +55 -1
  39. package/dist/language/mk.js +55 -1
  40. package/dist/language/mn.js +46 -1
  41. package/dist/language/mr.js +43 -1
  42. package/dist/language/ms.js +49 -1
  43. package/dist/language/nl.js +50 -1
  44. package/dist/language/no.js +47 -1
  45. package/dist/language/pl.js +73 -1
  46. package/dist/language/pt.js +50 -1
  47. package/dist/language/ro.js +53 -1
  48. package/dist/language/ru.js +59 -1
  49. package/dist/language/sk.js +64 -1
  50. package/dist/language/sl.js +138 -1
  51. package/dist/language/sq.js +51 -1
  52. package/dist/language/sr.js +83 -1
  53. package/dist/language/sr_Latn.js +21 -1
  54. package/dist/language/sv.js +49 -1
  55. package/dist/language/sw.js +51 -1
  56. package/dist/language/ta.js +54 -1
  57. package/dist/language/te.js +50 -1
  58. package/dist/language/th.js +44 -1
  59. package/dist/language/tr.js +42 -1
  60. package/dist/language/uk.js +61 -1
  61. package/dist/language/ur.js +57 -1
  62. package/dist/language/util/create-duration-language.js +33 -1
  63. package/dist/language/util/duration/get-czech-or-slovak-form.js +14 -1
  64. package/dist/language/util/duration/get-slavic-form.js +17 -1
  65. package/dist/language/util/validate-duration-language.js +36 -1
  66. package/dist/language/uz.js +43 -1
  67. package/dist/language/uz_CYR.js +43 -1
  68. package/dist/language/vi.js +34 -1
  69. package/dist/language/zh_CN.js +39 -1
  70. package/dist/language/zh_TW.js +39 -1
  71. package/dist/packem_shared/duration-C62ipnQn.js +214 -0
  72. package/dist/packem_shared/parseBytes-JWspeMzP.js +238 -0
  73. package/dist/packem_shared/parseDuration-CeFaBgx9.js +111 -0
  74. package/package.json +3 -3
  75. package/dist/packem_shared/duration-B8tBhwXw.js +0 -1
  76. package/dist/packem_shared/parseBytes-Da87aeh8.js +0 -1
  77. package/dist/packem_shared/parseDuration-Bo02kq8G.js +0 -1
@@ -0,0 +1,214 @@
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] += 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] += 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 = [];
162
+ for (const piece_ of pieces) {
163
+ const piece = piece_;
164
+ renderedPieces.push(renderPiece(piece, language, options));
165
+ }
166
+ let result;
167
+ if (!conjunction || pieces.length === 1) {
168
+ result = renderedPieces.join(delimiter);
169
+ } else if (pieces.length === 2) {
170
+ result = renderedPieces.join(conjunction);
171
+ } else {
172
+ result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + renderedPieces.at(-1);
173
+ }
174
+ if (adverb) {
175
+ result = adverb.replace("%s", result);
176
+ }
177
+ return result;
178
+ };
179
+ const duration = (milliseconds, options) => {
180
+ if (Number.isNaN(milliseconds)) {
181
+ throw new TypeError("Expected a valid number");
182
+ }
183
+ if (typeof milliseconds !== "number") {
184
+ throw new TypeError("Expected a number for milliseconds input");
185
+ }
186
+ const config = {
187
+ conjunction: "",
188
+ language: durationLanguage,
189
+ round: false,
190
+ serialComma: true,
191
+ spacer: " ",
192
+ timeAdverb: false,
193
+ unitMeasures: {
194
+ d: 864e5,
195
+ h: 36e5,
196
+ m: 6e4,
197
+ mo: 2629746e3,
198
+ // 365.2425 / 12 = 30.436875 days
199
+ ms: 1,
200
+ s: 1e3,
201
+ w: 6048e5,
202
+ y: 31556952e3
203
+ // 365 + 1/4 - 1/100 + 1/400 (actual leap day rules) = 365.2425 days
204
+ },
205
+ units: ["w", "d", "h", "m", "s"],
206
+ ...options
207
+ };
208
+ validateDurationLanguage(config.language);
209
+ const absTime = Math.abs(milliseconds);
210
+ const pieces = getPieces(absTime, config);
211
+ return formatPieces(pieces, config, milliseconds);
212
+ };
213
+
214
+ export { duration as default };
@@ -0,0 +1,238 @@
1
+ const BYTE_SIZES = {
2
+ iec: [
3
+ {
4
+ long: "Bytes",
5
+ short: "B"
6
+ },
7
+ {
8
+ long: "Kibibytes",
9
+ short: "KiB"
10
+ },
11
+ {
12
+ long: "Mebibytes",
13
+ short: "MiB"
14
+ },
15
+ {
16
+ long: "Gibibytes",
17
+ short: "GiB"
18
+ },
19
+ {
20
+ long: "Tebibytes",
21
+ short: "TiB"
22
+ },
23
+ {
24
+ long: "Pebibytes",
25
+ short: "PiB"
26
+ },
27
+ {
28
+ long: "Exbibytes",
29
+ short: "EiB"
30
+ },
31
+ {
32
+ long: "Zebibytes",
33
+ short: "ZiB"
34
+ },
35
+ {
36
+ long: "Yobibytes",
37
+ short: "YiB"
38
+ }
39
+ ],
40
+ iec_octet: [
41
+ {
42
+ long: "Octets",
43
+ short: "o"
44
+ },
45
+ {
46
+ long: "Kibioctets",
47
+ short: "Kio"
48
+ },
49
+ {
50
+ long: "Mebioctets",
51
+ short: "Mio"
52
+ },
53
+ {
54
+ long: "Gibioctets",
55
+ short: "Gio"
56
+ },
57
+ {
58
+ long: "Tebioctets",
59
+ short: "Tio"
60
+ },
61
+ {
62
+ long: "Pebioctets",
63
+ short: "Pio"
64
+ },
65
+ {
66
+ long: "Exbioctets",
67
+ short: "Eio"
68
+ },
69
+ {
70
+ long: "Zebioctets",
71
+ short: "Zio"
72
+ },
73
+ {
74
+ long: "Yobioctets",
75
+ short: "Yio"
76
+ }
77
+ ],
78
+ metric: [
79
+ {
80
+ long: "Bytes",
81
+ short: "Bytes"
82
+ },
83
+ {
84
+ long: "Kilobytes",
85
+ short: "KB"
86
+ },
87
+ {
88
+ long: "Megabytes",
89
+ short: "MB"
90
+ },
91
+ {
92
+ long: "Gigabytes",
93
+ short: "GB"
94
+ },
95
+ {
96
+ long: "Terabytes",
97
+ short: "TB"
98
+ },
99
+ {
100
+ long: "Petabytes",
101
+ short: "PB"
102
+ },
103
+ {
104
+ long: "Exabytes",
105
+ short: "EB"
106
+ },
107
+ {
108
+ long: "Zettabytes",
109
+ short: "ZB"
110
+ },
111
+ {
112
+ long: "Yottabytes",
113
+ short: "YB"
114
+ }
115
+ ],
116
+ metric_octet: [
117
+ {
118
+ long: "Octets",
119
+ short: "o"
120
+ },
121
+ {
122
+ long: "Kilo-octets",
123
+ short: "ko"
124
+ },
125
+ {
126
+ long: "Mega-octets",
127
+ short: "Mo"
128
+ },
129
+ {
130
+ long: "Giga-octets",
131
+ short: "Go"
132
+ },
133
+ {
134
+ long: "Tera-octets",
135
+ short: "To"
136
+ },
137
+ {
138
+ long: "Peta-octets",
139
+ short: "Po"
140
+ },
141
+ {
142
+ long: "Exa-octets",
143
+ short: "Eo"
144
+ },
145
+ {
146
+ long: "Zetta-octets",
147
+ short: "Zo"
148
+ },
149
+ {
150
+ long: "Yotta-octets",
151
+ short: "Yo"
152
+ }
153
+ ]
154
+ };
155
+ const parseLocalizedNumber = (stringNumber, locale) => {
156
+ const thousandSeparator = new Intl.NumberFormat(locale).format(11111).replaceAll(new RegExp("\\p{Number}", "gu"), "");
157
+ const decimalSeparator = new Intl.NumberFormat(locale).format(1.1).replaceAll(new RegExp("\\p{Number}", "gu"), "");
158
+ return Number.parseFloat(stringNumber.replaceAll(new RegExp(`\\${thousandSeparator}`, "g"), "").replace(new RegExp(`\\${decimalSeparator}`), "."));
159
+ };
160
+ const fromBase = (base) => {
161
+ if (base === 2) {
162
+ return 1024;
163
+ }
164
+ if (base === 10) {
165
+ return 1e3;
166
+ }
167
+ throw new TypeError(`Unsupported base.`);
168
+ };
169
+ const parseBytes = (value, options) => {
170
+ const config = {
171
+ base: 2,
172
+ locale: "en-US",
173
+ units: "metric",
174
+ ...options
175
+ };
176
+ if (typeof value !== "string" || value.length === 0) {
177
+ throw new TypeError("Value is not a string or is empty.");
178
+ }
179
+ if (value.length > 100) {
180
+ throw new TypeError("Value exceeds the maximum length of 100 characters.");
181
+ }
182
+ const match = /^(?<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(
183
+ value
184
+ );
185
+ const groups = match?.groups;
186
+ if (!groups) {
187
+ return Number.NaN;
188
+ }
189
+ const localizedNumber = parseLocalizedNumber(groups.value, config.locale);
190
+ const type = (groups.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");
191
+ const level = BYTE_SIZES[config.units].findIndex((unit) => unit.short[0].toUpperCase() === type[0]);
192
+ const base = fromBase(config.base);
193
+ return localizedNumber * base ** level;
194
+ };
195
+ const formatBytes = (bytes, options) => {
196
+ if (typeof bytes !== "number" || !Number.isFinite(bytes)) {
197
+ throw new TypeError("Bytesize is not a number.");
198
+ }
199
+ const {
200
+ base: givenBase,
201
+ decimals,
202
+ locale,
203
+ long,
204
+ unit: requestedUnit,
205
+ units,
206
+ ...l10nOptions
207
+ } = {
208
+ base: 2,
209
+ decimals: 0,
210
+ locale: "en-US",
211
+ long: false,
212
+ units: "metric",
213
+ ...options
214
+ };
215
+ const base = fromBase(givenBase);
216
+ const absoluteBytes = Math.abs(bytes);
217
+ const space = options?.space ?? true ? " " : "";
218
+ const referenceTable = BYTE_SIZES[units];
219
+ const requestedUnitIndex = referenceTable.findIndex((unit2) => unit2.short === requestedUnit);
220
+ if (bytes === 0) {
221
+ const level2 = Math.min(0, Math.max(requestedUnitIndex, referenceTable.length - 1));
222
+ return "0" + space + referenceTable[level2][long ? "long" : "short"];
223
+ }
224
+ const level = requestedUnitIndex === -1 ? Math.min(Math.floor(Math.log(absoluteBytes) / Math.log(base)), referenceTable.length - 1) : requestedUnitIndex;
225
+ const unit = referenceTable[level][long ? "long" : "short"];
226
+ const value = bytes / base ** level;
227
+ const fractionDigits = decimals < 0 ? void 0 : decimals;
228
+ const formattedValue = new Intl.NumberFormat(locale, {
229
+ // @ts-expect-error - should be overridden by the options
230
+ maximumFractionDigits: fractionDigits,
231
+ // @ts-expect-error - should be overridden by the options
232
+ minimumFractionDigits: fractionDigits,
233
+ ...l10nOptions
234
+ }).format(value);
235
+ return formattedValue + space + unit;
236
+ };
237
+
238
+ 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 parseDuration = (value, options) => {
19
+ if (typeof value !== "string" || value.length === 0) {
20
+ return void 0;
21
+ }
22
+ const { defaultUnit = "ms", language = durationLanguage } = options ?? {};
23
+ validateDurationLanguage(language);
24
+ const decimalSeparator = language.decimal ?? ".";
25
+ const groupSeparator = language.groupSeparator ?? ",";
26
+ const placeholderSeparator = language.placeholderSeparator ?? "_";
27
+ const escapedDecimal = decimalSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
28
+ const escapedGroup = groupSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
29
+ const escapedPlaceholder = placeholderSeparator.replaceAll(ESCAPE_REGEX, String.raw`\$&`);
30
+ const currentUnitMap = language.unitMap ?? englishUnitMap;
31
+ let processedValue = value.replaceAll(new RegExp(String.raw`(\d)[${escapedPlaceholder}${escapedGroup}](\d)`, "g"), "$1$2");
32
+ if (decimalSeparator !== ".") {
33
+ processedValue = processedValue.replace(escapedDecimal, ".");
34
+ }
35
+ if (NUMERIC_STRING_REGEX.test(value)) {
36
+ const numberOnly = Number.parseFloat(processedValue.trim());
37
+ if (!Number.isNaN(numberOnly)) {
38
+ const unitKey = currentUnitMap[defaultUnit];
39
+ if (unitKey !== void 0) {
40
+ return numberOnly * STANDARD_UNIT_MEASURES[unitKey];
41
+ }
42
+ }
43
+ return void 0;
44
+ }
45
+ const isoMatch = ISO_FORMAT.exec(value);
46
+ if (isoMatch) {
47
+ const hours = Number.parseInt(isoMatch[1] ?? "0", 10);
48
+ const minutes = Number.parseInt(isoMatch[2] ?? "0", 10);
49
+ const seconds = Number.parseInt(isoMatch[3] ?? "0", 10);
50
+ return hours * 36e5 + minutes * 6e4 + seconds * 1e3;
51
+ }
52
+ const colonMatch = COLON_FORMAT.exec(value);
53
+ if (colonMatch) {
54
+ let hours = 0;
55
+ let minutes = 0;
56
+ let seconds = 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
+ 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(/^[-+]/, "");
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.2",
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};