@visulima/humanizer 1.0.18 → 1.0.20
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/index.cjs +12 -1
- package/dist/index.mjs +2 -1
- package/dist/language/af.cjs +21 -1
- package/dist/language/af.mjs +17 -1
- package/dist/language/am.cjs +9 -1
- package/dist/language/am.mjs +5 -1
- package/dist/language/ar.cjs +37 -1
- package/dist/language/ar.d.cts +4 -4
- package/dist/language/ar.d.mts +4 -4
- package/dist/language/ar.d.ts +4 -4
- package/dist/language/ar.mjs +33 -1
- package/dist/language/bg.cjs +22 -1
- package/dist/language/bg.mjs +18 -1
- package/dist/language/bn.cjs +9 -1
- package/dist/language/bn.mjs +5 -1
- package/dist/language/ca.cjs +21 -1
- package/dist/language/ca.mjs +17 -1
- package/dist/language/ckb.cjs +9 -1
- package/dist/language/ckb.mjs +5 -1
- package/dist/language/cs.cjs +22 -1
- package/dist/language/cs.mjs +18 -1
- package/dist/language/cy.cjs +9 -1
- package/dist/language/cy.mjs +5 -1
- package/dist/language/da.cjs +21 -1
- package/dist/language/da.mjs +17 -1
- package/dist/language/de.cjs +21 -1
- package/dist/language/de.mjs +17 -1
- package/dist/language/el.cjs +21 -1
- package/dist/language/el.mjs +17 -1
- package/dist/language/en.cjs +20 -1
- package/dist/language/en.mjs +16 -1
- package/dist/language/eo.cjs +21 -1
- package/dist/language/eo.mjs +17 -1
- package/dist/language/es.cjs +21 -1
- package/dist/language/es.mjs +17 -1
- package/dist/language/et.cjs +21 -1
- package/dist/language/et.mjs +17 -1
- package/dist/language/eu.cjs +21 -1
- package/dist/language/eu.mjs +17 -1
- package/dist/language/fa.cjs +9 -1
- package/dist/language/fa.mjs +5 -1
- package/dist/language/fi.cjs +21 -1
- package/dist/language/fi.mjs +17 -1
- package/dist/language/fo.cjs +21 -1
- package/dist/language/fo.mjs +17 -1
- package/dist/language/fr.cjs +21 -1
- package/dist/language/fr.mjs +17 -1
- package/dist/language/he.cjs +20 -1
- package/dist/language/he.mjs +16 -1
- package/dist/language/hi.cjs +20 -1
- package/dist/language/hi.mjs +16 -1
- package/dist/language/hr.cjs +73 -1
- package/dist/language/hr.mjs +69 -1
- package/dist/language/hu.cjs +9 -1
- package/dist/language/hu.mjs +5 -1
- package/dist/language/id.cjs +9 -1
- package/dist/language/id.mjs +5 -1
- package/dist/language/is.cjs +20 -1
- package/dist/language/is.mjs +16 -1
- package/dist/language/it.cjs +21 -1
- package/dist/language/it.mjs +17 -1
- package/dist/language/ja.cjs +9 -1
- package/dist/language/ja.mjs +5 -1
- package/dist/language/km.cjs +9 -1
- package/dist/language/km.mjs +5 -1
- package/dist/language/kn.cjs +20 -1
- package/dist/language/kn.mjs +16 -1
- package/dist/language/ko.cjs +9 -1
- package/dist/language/ko.mjs +5 -1
- package/dist/language/ku.cjs +9 -1
- package/dist/language/ku.mjs +5 -1
- package/dist/language/lo.cjs +9 -1
- package/dist/language/lo.mjs +5 -1
- package/dist/language/lt.cjs +32 -1
- package/dist/language/lt.mjs +28 -1
- package/dist/language/lv.cjs +24 -1
- package/dist/language/lv.mjs +20 -1
- package/dist/language/mk.cjs +21 -1
- package/dist/language/mk.mjs +17 -1
- package/dist/language/mn.cjs +9 -1
- package/dist/language/mn.mjs +5 -1
- package/dist/language/mr.cjs +20 -1
- package/dist/language/mr.mjs +16 -1
- package/dist/language/ms.cjs +9 -1
- package/dist/language/ms.mjs +5 -1
- package/dist/language/nl.cjs +21 -1
- package/dist/language/nl.mjs +17 -1
- package/dist/language/no.cjs +21 -1
- package/dist/language/no.mjs +17 -1
- package/dist/language/pl.cjs +35 -1
- package/dist/language/pl.mjs +31 -1
- package/dist/language/pt.cjs +21 -1
- package/dist/language/pt.mjs +17 -1
- package/dist/language/ro.cjs +21 -1
- package/dist/language/ro.mjs +17 -1
- package/dist/language/ru.cjs +22 -1
- package/dist/language/ru.mjs +18 -1
- package/dist/language/sk.cjs +22 -1
- package/dist/language/sk.mjs +18 -1
- package/dist/language/sl.cjs +98 -1
- package/dist/language/sl.mjs +94 -1
- package/dist/language/sq.cjs +21 -1
- package/dist/language/sq.mjs +17 -1
- package/dist/language/sr.cjs +22 -1
- package/dist/language/sr.mjs +18 -1
- package/dist/language/sv.cjs +21 -1
- package/dist/language/sv.mjs +17 -1
- package/dist/language/sw.cjs +23 -1
- package/dist/language/sw.d.cts +6 -6
- package/dist/language/sw.d.mts +6 -6
- package/dist/language/sw.d.ts +6 -6
- package/dist/language/sw.mjs +19 -1
- package/dist/language/ta.cjs +20 -1
- package/dist/language/ta.mjs +16 -1
- package/dist/language/te.cjs +20 -1
- package/dist/language/te.mjs +16 -1
- package/dist/language/th.cjs +9 -1
- package/dist/language/th.mjs +5 -1
- package/dist/language/tr.cjs +9 -1
- package/dist/language/tr.mjs +5 -1
- package/dist/language/uk.cjs +22 -1
- package/dist/language/uk.mjs +18 -1
- package/dist/language/ur.cjs +20 -1
- package/dist/language/ur.mjs +16 -1
- package/dist/language/util/create-duration-language.cjs +28 -1
- package/dist/language/util/create-duration-language.mjs +26 -1
- package/dist/language/util/duration/get-czech-or-slovak-form.cjs +18 -1
- package/dist/language/util/duration/get-czech-or-slovak-form.mjs +16 -1
- package/dist/language/util/duration/get-slavic-form.cjs +21 -1
- package/dist/language/util/duration/get-slavic-form.mjs +19 -1
- package/dist/language/util/validate-duration-language.cjs +34 -1
- package/dist/language/util/validate-duration-language.mjs +32 -1
- package/dist/language/uz.cjs +9 -1
- package/dist/language/uz.mjs +5 -1
- package/dist/language/uz_CYR.cjs +9 -1
- package/dist/language/uz_CYR.mjs +5 -1
- package/dist/language/vi.cjs +9 -1
- package/dist/language/vi.mjs +5 -1
- package/dist/language/zh_CN.cjs +9 -1
- package/dist/language/zh_CN.mjs +5 -1
- package/dist/language/zh_TW.cjs +9 -1
- package/dist/language/zh_TW.mjs +5 -1
- package/dist/packem_shared/duration-1XAT3kv0.cjs +218 -0
- package/dist/packem_shared/duration-CfhZBuZE.mjs +216 -0
- package/dist/packem_shared/parseBytes-CUjzg1ll.mjs +121 -0
- package/dist/packem_shared/parseBytes-QKt9uy0m.cjs +126 -0
- package/package.json +8 -8
- package/dist/packem_shared/duration-0OLTwN46.mjs +0 -1
- package/dist/packem_shared/duration-CU5vEjtr.cjs +0 -1
- package/dist/packem_shared/parseBytes-BMtRQhVC.mjs +0 -1
- package/dist/packem_shared/parseBytes-BWae2sJX.cjs +0 -1
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const language_en = require('../language/en.cjs');
|
|
4
|
+
const language_util_validateDurationLanguage = require('../language/util/validate-duration-language.cjs');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
const toFixed = /* @__PURE__ */ __name((number_, fixed) => {
|
|
9
|
+
fixed = fixed || -1;
|
|
10
|
+
const matches = new RegExp(`^-?\\d+(?:.\\d{0,${fixed}})?`).exec(number_.toString());
|
|
11
|
+
if (matches === null) {
|
|
12
|
+
return number_;
|
|
13
|
+
}
|
|
14
|
+
return Number.parseFloat(matches[0]);
|
|
15
|
+
}, "toFixed");
|
|
16
|
+
const renderPiece = /* @__PURE__ */ __name(({ unitCount, unitName }, language, options) => {
|
|
17
|
+
let { spacer } = options;
|
|
18
|
+
const { maxDecimalPoints } = options;
|
|
19
|
+
let decimal = ".";
|
|
20
|
+
if (options.decimal !== undefined) {
|
|
21
|
+
decimal = options.decimal;
|
|
22
|
+
} else if (language.decimal !== undefined) {
|
|
23
|
+
decimal = language.decimal;
|
|
24
|
+
}
|
|
25
|
+
let digitReplacements;
|
|
26
|
+
if ("digitReplacements" in options) {
|
|
27
|
+
digitReplacements = options.digitReplacements;
|
|
28
|
+
} else if ("_digitReplacements" in language) {
|
|
29
|
+
digitReplacements = language._digitReplacements;
|
|
30
|
+
}
|
|
31
|
+
let formattedCount;
|
|
32
|
+
let normalizedUnitCount = unitCount;
|
|
33
|
+
if (maxDecimalPoints !== undefined) {
|
|
34
|
+
normalizedUnitCount = toFixed(unitCount, maxDecimalPoints);
|
|
35
|
+
}
|
|
36
|
+
const countString = normalizedUnitCount.toString();
|
|
37
|
+
if (!language._hideCountIf2 || unitCount !== 2) {
|
|
38
|
+
if (digitReplacements) {
|
|
39
|
+
formattedCount = "";
|
|
40
|
+
for (const char of countString) {
|
|
41
|
+
formattedCount += char === "." ? decimal : digitReplacements[char];
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
formattedCount = countString.replace(".", decimal);
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
formattedCount = "";
|
|
48
|
+
}
|
|
49
|
+
const languageWord = language[unitName];
|
|
50
|
+
let word = languageWord;
|
|
51
|
+
if (typeof languageWord === "function") {
|
|
52
|
+
word = languageWord(unitCount);
|
|
53
|
+
}
|
|
54
|
+
if (language._hideCountIf2 && unitCount === 2) {
|
|
55
|
+
spacer = "";
|
|
56
|
+
}
|
|
57
|
+
if (language._numberFirst) {
|
|
58
|
+
return word + spacer + formattedCount;
|
|
59
|
+
}
|
|
60
|
+
return formattedCount + spacer + word;
|
|
61
|
+
}, "renderPiece");
|
|
62
|
+
const getPieces = /* @__PURE__ */ __name((ms, options) => {
|
|
63
|
+
const { units } = options;
|
|
64
|
+
if (units.length === 0) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const { unitMeasures } = options;
|
|
68
|
+
const largest = options.largest ?? Number.POSITIVE_INFINITY;
|
|
69
|
+
const unitCounts = {};
|
|
70
|
+
let unitName;
|
|
71
|
+
let index;
|
|
72
|
+
let unitCount;
|
|
73
|
+
let msRemaining = ms;
|
|
74
|
+
for (index = 0; index < units.length; index++) {
|
|
75
|
+
unitName = units[index];
|
|
76
|
+
const unitMs = unitMeasures[unitName];
|
|
77
|
+
const isLast = index === units.length - 1;
|
|
78
|
+
unitCount = isLast ? msRemaining / unitMs : Math.floor(msRemaining / unitMs);
|
|
79
|
+
unitCounts[unitName] = unitCount;
|
|
80
|
+
msRemaining -= unitCount * unitMs;
|
|
81
|
+
}
|
|
82
|
+
if (options.round) {
|
|
83
|
+
let unitsRemainingBeforeRound = largest;
|
|
84
|
+
for (index = 0; index < units.length; index++) {
|
|
85
|
+
unitName = units[index];
|
|
86
|
+
unitCount = unitCounts[unitName];
|
|
87
|
+
if (unitCount === 0) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
unitsRemainingBeforeRound--;
|
|
91
|
+
if (unitsRemainingBeforeRound === 0) {
|
|
92
|
+
for (let index_ = index + 1; index_ < units.length; index_++) {
|
|
93
|
+
const smallerUnitName = units[index_];
|
|
94
|
+
const smallerUnitCount = unitCounts[smallerUnitName];
|
|
95
|
+
unitCounts[unitName] += smallerUnitCount * unitMeasures[smallerUnitName] / unitMeasures[unitName];
|
|
96
|
+
unitCounts[smallerUnitName] = 0;
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (index = units.length - 1; index >= 0; index--) {
|
|
102
|
+
unitName = units[index];
|
|
103
|
+
unitCount = unitCounts[unitName];
|
|
104
|
+
if (unitCount === 0) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const rounded = Math.round(unitCount);
|
|
108
|
+
unitCounts[unitName] = rounded;
|
|
109
|
+
if (index === 0) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
const previousUnitName = units[index - 1];
|
|
113
|
+
const previousUnitMs = unitMeasures[previousUnitName];
|
|
114
|
+
const amountOfPreviousUnit = Math.floor(rounded * unitMeasures[unitName] / previousUnitMs);
|
|
115
|
+
if (amountOfPreviousUnit) {
|
|
116
|
+
unitCounts[previousUnitName] += amountOfPreviousUnit;
|
|
117
|
+
unitCounts[unitName] = 0;
|
|
118
|
+
} else {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const result = [];
|
|
124
|
+
for (index = 0; index < units.length && result.length < largest; index++) {
|
|
125
|
+
unitName = units[index];
|
|
126
|
+
unitCount = unitCounts[unitName];
|
|
127
|
+
if (unitCount && !options.round && result.length === largest - 1) {
|
|
128
|
+
let index_;
|
|
129
|
+
let remainder = 0;
|
|
130
|
+
for (index_ = index + 1, units.length; index_ < units.length; index_++) {
|
|
131
|
+
const remainderUnitName = units[index_];
|
|
132
|
+
remainder += unitCounts[remainderUnitName] * (options.unitMeasures[remainderUnitName] / options.unitMeasures[unitName]);
|
|
133
|
+
}
|
|
134
|
+
unitCount += remainder;
|
|
135
|
+
if (options.maxDecimalPoints !== undefined) {
|
|
136
|
+
unitCount = toFixed(unitCount, options.maxDecimalPoints);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (unitCount) {
|
|
140
|
+
result.push({ unitCount, unitName });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return result;
|
|
144
|
+
}, "getPieces");
|
|
145
|
+
const formatPieces = /* @__PURE__ */ __name((pieces, options, ms) => {
|
|
146
|
+
const { language, units } = options;
|
|
147
|
+
if (pieces.length === 0) {
|
|
148
|
+
const smallestUnitName = units.at(-1);
|
|
149
|
+
return renderPiece({ unitCount: 0, unitName: smallestUnitName }, language, options);
|
|
150
|
+
}
|
|
151
|
+
const { conjunction, serialComma } = options;
|
|
152
|
+
let delimiter = ", ";
|
|
153
|
+
if (options.delimiter !== undefined) {
|
|
154
|
+
delimiter = options.delimiter;
|
|
155
|
+
} else if (language.delimiter !== undefined) {
|
|
156
|
+
delimiter = language.delimiter;
|
|
157
|
+
}
|
|
158
|
+
let adverb = "";
|
|
159
|
+
if (options.timeAdverb && ms !== 0) {
|
|
160
|
+
adverb = language.future ?? "";
|
|
161
|
+
if (ms < 0) {
|
|
162
|
+
adverb = language.past ?? "";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const renderedPieces = [];
|
|
166
|
+
for (const piece_ of pieces) {
|
|
167
|
+
const piece = piece_;
|
|
168
|
+
renderedPieces.push(renderPiece(piece, language, options));
|
|
169
|
+
}
|
|
170
|
+
let result;
|
|
171
|
+
if (!conjunction || pieces.length === 1) {
|
|
172
|
+
result = renderedPieces.join(delimiter);
|
|
173
|
+
} else if (pieces.length === 2) {
|
|
174
|
+
result = renderedPieces.join(conjunction);
|
|
175
|
+
} else {
|
|
176
|
+
result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + renderedPieces.at(-1);
|
|
177
|
+
}
|
|
178
|
+
if (adverb) {
|
|
179
|
+
result = adverb.replace("%s", result);
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}, "formatPieces");
|
|
183
|
+
const duration = /* @__PURE__ */ __name((milliseconds, options) => {
|
|
184
|
+
if (Number.isNaN(milliseconds)) {
|
|
185
|
+
throw new TypeError("Expected a valid number");
|
|
186
|
+
}
|
|
187
|
+
if (typeof milliseconds !== "number") {
|
|
188
|
+
throw new TypeError("Expected a number");
|
|
189
|
+
}
|
|
190
|
+
const config = {
|
|
191
|
+
conjunction: "",
|
|
192
|
+
language: language_en.durationLanguage,
|
|
193
|
+
round: false,
|
|
194
|
+
serialComma: true,
|
|
195
|
+
spacer: " ",
|
|
196
|
+
timeAdverb: false,
|
|
197
|
+
unitMeasures: {
|
|
198
|
+
d: 864e5,
|
|
199
|
+
h: 36e5,
|
|
200
|
+
m: 6e4,
|
|
201
|
+
mo: 2629746e3,
|
|
202
|
+
// 365.2425 / 12 = 30.436875 days
|
|
203
|
+
ms: 1,
|
|
204
|
+
s: 1e3,
|
|
205
|
+
w: 6048e5,
|
|
206
|
+
y: 31556952e3
|
|
207
|
+
// 365 + 1/4 - 1/100 + 1/400 (actual leap day rules) = 365.2425 days
|
|
208
|
+
},
|
|
209
|
+
units: ["w", "d", "h", "m", "s"],
|
|
210
|
+
...options
|
|
211
|
+
};
|
|
212
|
+
language_util_validateDurationLanguage(config.language);
|
|
213
|
+
const absTime = Math.abs(milliseconds);
|
|
214
|
+
const pieces = getPieces(absTime, config);
|
|
215
|
+
return formatPieces(pieces, config, milliseconds);
|
|
216
|
+
}, "duration");
|
|
217
|
+
|
|
218
|
+
module.exports = duration;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { durationLanguage } from '../language/en.mjs';
|
|
2
|
+
import validateDurationLanguage from '../language/util/validate-duration-language.mjs';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
const toFixed = /* @__PURE__ */ __name((number_, fixed) => {
|
|
7
|
+
fixed = fixed || -1;
|
|
8
|
+
const matches = new RegExp(`^-?\\d+(?:.\\d{0,${fixed}})?`).exec(number_.toString());
|
|
9
|
+
if (matches === null) {
|
|
10
|
+
return number_;
|
|
11
|
+
}
|
|
12
|
+
return Number.parseFloat(matches[0]);
|
|
13
|
+
}, "toFixed");
|
|
14
|
+
const renderPiece = /* @__PURE__ */ __name(({ unitCount, unitName }, language, options) => {
|
|
15
|
+
let { spacer } = options;
|
|
16
|
+
const { maxDecimalPoints } = options;
|
|
17
|
+
let decimal = ".";
|
|
18
|
+
if (options.decimal !== undefined) {
|
|
19
|
+
decimal = options.decimal;
|
|
20
|
+
} else if (language.decimal !== undefined) {
|
|
21
|
+
decimal = language.decimal;
|
|
22
|
+
}
|
|
23
|
+
let digitReplacements;
|
|
24
|
+
if ("digitReplacements" in options) {
|
|
25
|
+
digitReplacements = options.digitReplacements;
|
|
26
|
+
} else if ("_digitReplacements" in language) {
|
|
27
|
+
digitReplacements = language._digitReplacements;
|
|
28
|
+
}
|
|
29
|
+
let formattedCount;
|
|
30
|
+
let normalizedUnitCount = unitCount;
|
|
31
|
+
if (maxDecimalPoints !== undefined) {
|
|
32
|
+
normalizedUnitCount = toFixed(unitCount, maxDecimalPoints);
|
|
33
|
+
}
|
|
34
|
+
const countString = normalizedUnitCount.toString();
|
|
35
|
+
if (!language._hideCountIf2 || unitCount !== 2) {
|
|
36
|
+
if (digitReplacements) {
|
|
37
|
+
formattedCount = "";
|
|
38
|
+
for (const char of countString) {
|
|
39
|
+
formattedCount += char === "." ? decimal : digitReplacements[char];
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
formattedCount = countString.replace(".", decimal);
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
formattedCount = "";
|
|
46
|
+
}
|
|
47
|
+
const languageWord = language[unitName];
|
|
48
|
+
let word = languageWord;
|
|
49
|
+
if (typeof languageWord === "function") {
|
|
50
|
+
word = languageWord(unitCount);
|
|
51
|
+
}
|
|
52
|
+
if (language._hideCountIf2 && unitCount === 2) {
|
|
53
|
+
spacer = "";
|
|
54
|
+
}
|
|
55
|
+
if (language._numberFirst) {
|
|
56
|
+
return word + spacer + formattedCount;
|
|
57
|
+
}
|
|
58
|
+
return formattedCount + spacer + word;
|
|
59
|
+
}, "renderPiece");
|
|
60
|
+
const getPieces = /* @__PURE__ */ __name((ms, options) => {
|
|
61
|
+
const { units } = options;
|
|
62
|
+
if (units.length === 0) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
const { unitMeasures } = options;
|
|
66
|
+
const largest = options.largest ?? Number.POSITIVE_INFINITY;
|
|
67
|
+
const unitCounts = {};
|
|
68
|
+
let unitName;
|
|
69
|
+
let index;
|
|
70
|
+
let unitCount;
|
|
71
|
+
let msRemaining = ms;
|
|
72
|
+
for (index = 0; index < units.length; index++) {
|
|
73
|
+
unitName = units[index];
|
|
74
|
+
const unitMs = unitMeasures[unitName];
|
|
75
|
+
const isLast = index === units.length - 1;
|
|
76
|
+
unitCount = isLast ? msRemaining / unitMs : Math.floor(msRemaining / unitMs);
|
|
77
|
+
unitCounts[unitName] = unitCount;
|
|
78
|
+
msRemaining -= unitCount * unitMs;
|
|
79
|
+
}
|
|
80
|
+
if (options.round) {
|
|
81
|
+
let unitsRemainingBeforeRound = largest;
|
|
82
|
+
for (index = 0; index < units.length; index++) {
|
|
83
|
+
unitName = units[index];
|
|
84
|
+
unitCount = unitCounts[unitName];
|
|
85
|
+
if (unitCount === 0) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
unitsRemainingBeforeRound--;
|
|
89
|
+
if (unitsRemainingBeforeRound === 0) {
|
|
90
|
+
for (let index_ = index + 1; index_ < units.length; index_++) {
|
|
91
|
+
const smallerUnitName = units[index_];
|
|
92
|
+
const smallerUnitCount = unitCounts[smallerUnitName];
|
|
93
|
+
unitCounts[unitName] += smallerUnitCount * unitMeasures[smallerUnitName] / unitMeasures[unitName];
|
|
94
|
+
unitCounts[smallerUnitName] = 0;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (index = units.length - 1; index >= 0; index--) {
|
|
100
|
+
unitName = units[index];
|
|
101
|
+
unitCount = unitCounts[unitName];
|
|
102
|
+
if (unitCount === 0) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const rounded = Math.round(unitCount);
|
|
106
|
+
unitCounts[unitName] = rounded;
|
|
107
|
+
if (index === 0) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
const previousUnitName = units[index - 1];
|
|
111
|
+
const previousUnitMs = unitMeasures[previousUnitName];
|
|
112
|
+
const amountOfPreviousUnit = Math.floor(rounded * unitMeasures[unitName] / previousUnitMs);
|
|
113
|
+
if (amountOfPreviousUnit) {
|
|
114
|
+
unitCounts[previousUnitName] += amountOfPreviousUnit;
|
|
115
|
+
unitCounts[unitName] = 0;
|
|
116
|
+
} else {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const result = [];
|
|
122
|
+
for (index = 0; index < units.length && result.length < largest; index++) {
|
|
123
|
+
unitName = units[index];
|
|
124
|
+
unitCount = unitCounts[unitName];
|
|
125
|
+
if (unitCount && !options.round && result.length === largest - 1) {
|
|
126
|
+
let index_;
|
|
127
|
+
let remainder = 0;
|
|
128
|
+
for (index_ = index + 1, units.length; index_ < units.length; index_++) {
|
|
129
|
+
const remainderUnitName = units[index_];
|
|
130
|
+
remainder += unitCounts[remainderUnitName] * (options.unitMeasures[remainderUnitName] / options.unitMeasures[unitName]);
|
|
131
|
+
}
|
|
132
|
+
unitCount += remainder;
|
|
133
|
+
if (options.maxDecimalPoints !== undefined) {
|
|
134
|
+
unitCount = toFixed(unitCount, options.maxDecimalPoints);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (unitCount) {
|
|
138
|
+
result.push({ unitCount, unitName });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}, "getPieces");
|
|
143
|
+
const formatPieces = /* @__PURE__ */ __name((pieces, options, ms) => {
|
|
144
|
+
const { language, units } = options;
|
|
145
|
+
if (pieces.length === 0) {
|
|
146
|
+
const smallestUnitName = units.at(-1);
|
|
147
|
+
return renderPiece({ unitCount: 0, unitName: smallestUnitName }, language, options);
|
|
148
|
+
}
|
|
149
|
+
const { conjunction, serialComma } = options;
|
|
150
|
+
let delimiter = ", ";
|
|
151
|
+
if (options.delimiter !== undefined) {
|
|
152
|
+
delimiter = options.delimiter;
|
|
153
|
+
} else if (language.delimiter !== undefined) {
|
|
154
|
+
delimiter = language.delimiter;
|
|
155
|
+
}
|
|
156
|
+
let adverb = "";
|
|
157
|
+
if (options.timeAdverb && ms !== 0) {
|
|
158
|
+
adverb = language.future ?? "";
|
|
159
|
+
if (ms < 0) {
|
|
160
|
+
adverb = language.past ?? "";
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const renderedPieces = [];
|
|
164
|
+
for (const piece_ of pieces) {
|
|
165
|
+
const piece = piece_;
|
|
166
|
+
renderedPieces.push(renderPiece(piece, language, options));
|
|
167
|
+
}
|
|
168
|
+
let result;
|
|
169
|
+
if (!conjunction || pieces.length === 1) {
|
|
170
|
+
result = renderedPieces.join(delimiter);
|
|
171
|
+
} else if (pieces.length === 2) {
|
|
172
|
+
result = renderedPieces.join(conjunction);
|
|
173
|
+
} else {
|
|
174
|
+
result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + renderedPieces.at(-1);
|
|
175
|
+
}
|
|
176
|
+
if (adverb) {
|
|
177
|
+
result = adverb.replace("%s", result);
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}, "formatPieces");
|
|
181
|
+
const duration = /* @__PURE__ */ __name((milliseconds, options) => {
|
|
182
|
+
if (Number.isNaN(milliseconds)) {
|
|
183
|
+
throw new TypeError("Expected a valid number");
|
|
184
|
+
}
|
|
185
|
+
if (typeof milliseconds !== "number") {
|
|
186
|
+
throw new TypeError("Expected a number");
|
|
187
|
+
}
|
|
188
|
+
const config = {
|
|
189
|
+
conjunction: "",
|
|
190
|
+
language: durationLanguage,
|
|
191
|
+
round: false,
|
|
192
|
+
serialComma: true,
|
|
193
|
+
spacer: " ",
|
|
194
|
+
timeAdverb: false,
|
|
195
|
+
unitMeasures: {
|
|
196
|
+
d: 864e5,
|
|
197
|
+
h: 36e5,
|
|
198
|
+
m: 6e4,
|
|
199
|
+
mo: 2629746e3,
|
|
200
|
+
// 365.2425 / 12 = 30.436875 days
|
|
201
|
+
ms: 1,
|
|
202
|
+
s: 1e3,
|
|
203
|
+
w: 6048e5,
|
|
204
|
+
y: 31556952e3
|
|
205
|
+
// 365 + 1/4 - 1/100 + 1/400 (actual leap day rules) = 365.2425 days
|
|
206
|
+
},
|
|
207
|
+
units: ["w", "d", "h", "m", "s"],
|
|
208
|
+
...options
|
|
209
|
+
};
|
|
210
|
+
validateDurationLanguage(config.language);
|
|
211
|
+
const absTime = Math.abs(milliseconds);
|
|
212
|
+
const pieces = getPieces(absTime, config);
|
|
213
|
+
return formatPieces(pieces, config, milliseconds);
|
|
214
|
+
}, "duration");
|
|
215
|
+
|
|
216
|
+
export { duration as default };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
const BYTE_SIZES = [
|
|
4
|
+
{
|
|
5
|
+
long: "Bytes",
|
|
6
|
+
short: "Bytes"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
long: "Kilobytes",
|
|
10
|
+
short: "KB"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
long: "Megabytes",
|
|
14
|
+
short: "MB"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
long: "Gigabytes",
|
|
18
|
+
short: "GB"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
long: "Terabytes",
|
|
22
|
+
short: "TB"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
long: "Petabytes",
|
|
26
|
+
short: "PB"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
long: "Exabytes",
|
|
30
|
+
short: "EB"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
long: "Zettabytes",
|
|
34
|
+
short: "ZB"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
long: "Yottabytes",
|
|
38
|
+
short: "YB"
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
const parseLocalizedNumber = /* @__PURE__ */ __name((stringNumber, locale) => {
|
|
42
|
+
const thousandSeparator = new Intl.NumberFormat(locale).format(11111).replaceAll(new RegExp("\\p{Number}", "gu"), "");
|
|
43
|
+
const decimalSeparator = new Intl.NumberFormat(locale).format(1.1).replaceAll(new RegExp("\\p{Number}", "gu"), "");
|
|
44
|
+
return Number.parseFloat(stringNumber.replaceAll(new RegExp(`\\${thousandSeparator}`, "g"), "").replace(new RegExp(`\\${decimalSeparator}`), "."));
|
|
45
|
+
}, "parseLocalizedNumber");
|
|
46
|
+
const fromBase = /* @__PURE__ */ __name((base) => {
|
|
47
|
+
if (base === 2) {
|
|
48
|
+
return 1024;
|
|
49
|
+
}
|
|
50
|
+
if (base === 10) {
|
|
51
|
+
return 1e3;
|
|
52
|
+
}
|
|
53
|
+
throw new TypeError(`Unsupported base.`);
|
|
54
|
+
}, "fromBase");
|
|
55
|
+
const parseBytes = /* @__PURE__ */ __name((value, options) => {
|
|
56
|
+
const config = {
|
|
57
|
+
base: 2,
|
|
58
|
+
locale: "en-US",
|
|
59
|
+
...options
|
|
60
|
+
};
|
|
61
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
62
|
+
throw new TypeError("Value is not a string or is empty.");
|
|
63
|
+
}
|
|
64
|
+
if (value.length > 100) {
|
|
65
|
+
throw new TypeError("Value exceeds the maximum length of 100 characters.");
|
|
66
|
+
}
|
|
67
|
+
const match = (
|
|
68
|
+
// eslint-disable-next-line regexp/no-super-linear-backtracking,regexp/no-unused-capturing-group,regexp/no-misleading-capturing-group,security/detect-unsafe-regex
|
|
69
|
+
/^(?<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(
|
|
70
|
+
value
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
const groups = match?.groups;
|
|
74
|
+
if (!groups) {
|
|
75
|
+
return Number.NaN;
|
|
76
|
+
}
|
|
77
|
+
const localizedNumber = parseLocalizedNumber(groups.value, config.locale);
|
|
78
|
+
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");
|
|
79
|
+
const level = BYTE_SIZES.findIndex((unit) => unit.short[0].toUpperCase() === type[0]);
|
|
80
|
+
const base = fromBase(config.base);
|
|
81
|
+
return localizedNumber * base ** level;
|
|
82
|
+
}, "parseBytes");
|
|
83
|
+
const formatBytes = /* @__PURE__ */ __name((bytes, options) => {
|
|
84
|
+
if (typeof bytes !== "number" || !Number.isFinite(bytes)) {
|
|
85
|
+
throw new TypeError("Bytesize is not a number.");
|
|
86
|
+
}
|
|
87
|
+
const {
|
|
88
|
+
base: givenBase,
|
|
89
|
+
decimals,
|
|
90
|
+
locale,
|
|
91
|
+
long,
|
|
92
|
+
unit: requestedUnit,
|
|
93
|
+
...l10nOptions
|
|
94
|
+
} = {
|
|
95
|
+
base: 2,
|
|
96
|
+
decimals: 0,
|
|
97
|
+
locale: "en-US",
|
|
98
|
+
long: false,
|
|
99
|
+
...options
|
|
100
|
+
};
|
|
101
|
+
const base = fromBase(givenBase);
|
|
102
|
+
const absoluteBytes = Math.abs(bytes);
|
|
103
|
+
const space = options?.space ?? true ? " " : "";
|
|
104
|
+
const requestedUnitIndex = BYTE_SIZES.findIndex((unit2) => unit2.short === requestedUnit);
|
|
105
|
+
if (bytes === 0) {
|
|
106
|
+
const level2 = Math.min(0, Math.max(requestedUnitIndex, BYTE_SIZES.length - 1));
|
|
107
|
+
return "0" + space + BYTE_SIZES[level2][long ? "long" : "short"];
|
|
108
|
+
}
|
|
109
|
+
const level = requestedUnitIndex >= 0 ? requestedUnitIndex : Math.min(Math.floor(Math.log(absoluteBytes) / Math.log(base)), BYTE_SIZES.length - 1);
|
|
110
|
+
const unit = BYTE_SIZES[level][long ? "long" : "short"];
|
|
111
|
+
const value = bytes / base ** level;
|
|
112
|
+
const fractionDigits = decimals < 0 ? undefined : decimals;
|
|
113
|
+
const formattedValue = new Intl.NumberFormat(locale, {
|
|
114
|
+
maximumFractionDigits: fractionDigits,
|
|
115
|
+
minimumFractionDigits: fractionDigits,
|
|
116
|
+
...l10nOptions
|
|
117
|
+
}).format(value);
|
|
118
|
+
return formattedValue + space + unit;
|
|
119
|
+
}, "formatBytes");
|
|
120
|
+
|
|
121
|
+
export { formatBytes, parseBytes };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
const BYTE_SIZES = [
|
|
8
|
+
{
|
|
9
|
+
long: "Bytes",
|
|
10
|
+
short: "Bytes"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
long: "Kilobytes",
|
|
14
|
+
short: "KB"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
long: "Megabytes",
|
|
18
|
+
short: "MB"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
long: "Gigabytes",
|
|
22
|
+
short: "GB"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
long: "Terabytes",
|
|
26
|
+
short: "TB"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
long: "Petabytes",
|
|
30
|
+
short: "PB"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
long: "Exabytes",
|
|
34
|
+
short: "EB"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
long: "Zettabytes",
|
|
38
|
+
short: "ZB"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
long: "Yottabytes",
|
|
42
|
+
short: "YB"
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
const parseLocalizedNumber = /* @__PURE__ */ __name((stringNumber, locale) => {
|
|
46
|
+
const thousandSeparator = new Intl.NumberFormat(locale).format(11111).replaceAll(new RegExp("\\p{Number}", "gu"), "");
|
|
47
|
+
const decimalSeparator = new Intl.NumberFormat(locale).format(1.1).replaceAll(new RegExp("\\p{Number}", "gu"), "");
|
|
48
|
+
return Number.parseFloat(stringNumber.replaceAll(new RegExp(`\\${thousandSeparator}`, "g"), "").replace(new RegExp(`\\${decimalSeparator}`), "."));
|
|
49
|
+
}, "parseLocalizedNumber");
|
|
50
|
+
const fromBase = /* @__PURE__ */ __name((base) => {
|
|
51
|
+
if (base === 2) {
|
|
52
|
+
return 1024;
|
|
53
|
+
}
|
|
54
|
+
if (base === 10) {
|
|
55
|
+
return 1e3;
|
|
56
|
+
}
|
|
57
|
+
throw new TypeError(`Unsupported base.`);
|
|
58
|
+
}, "fromBase");
|
|
59
|
+
const parseBytes = /* @__PURE__ */ __name((value, options) => {
|
|
60
|
+
const config = {
|
|
61
|
+
base: 2,
|
|
62
|
+
locale: "en-US",
|
|
63
|
+
...options
|
|
64
|
+
};
|
|
65
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
66
|
+
throw new TypeError("Value is not a string or is empty.");
|
|
67
|
+
}
|
|
68
|
+
if (value.length > 100) {
|
|
69
|
+
throw new TypeError("Value exceeds the maximum length of 100 characters.");
|
|
70
|
+
}
|
|
71
|
+
const match = (
|
|
72
|
+
// eslint-disable-next-line regexp/no-super-linear-backtracking,regexp/no-unused-capturing-group,regexp/no-misleading-capturing-group,security/detect-unsafe-regex
|
|
73
|
+
/^(?<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(
|
|
74
|
+
value
|
|
75
|
+
)
|
|
76
|
+
);
|
|
77
|
+
const groups = match?.groups;
|
|
78
|
+
if (!groups) {
|
|
79
|
+
return Number.NaN;
|
|
80
|
+
}
|
|
81
|
+
const localizedNumber = parseLocalizedNumber(groups.value, config.locale);
|
|
82
|
+
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");
|
|
83
|
+
const level = BYTE_SIZES.findIndex((unit) => unit.short[0].toUpperCase() === type[0]);
|
|
84
|
+
const base = fromBase(config.base);
|
|
85
|
+
return localizedNumber * base ** level;
|
|
86
|
+
}, "parseBytes");
|
|
87
|
+
const formatBytes = /* @__PURE__ */ __name((bytes, options) => {
|
|
88
|
+
if (typeof bytes !== "number" || !Number.isFinite(bytes)) {
|
|
89
|
+
throw new TypeError("Bytesize is not a number.");
|
|
90
|
+
}
|
|
91
|
+
const {
|
|
92
|
+
base: givenBase,
|
|
93
|
+
decimals,
|
|
94
|
+
locale,
|
|
95
|
+
long,
|
|
96
|
+
unit: requestedUnit,
|
|
97
|
+
...l10nOptions
|
|
98
|
+
} = {
|
|
99
|
+
base: 2,
|
|
100
|
+
decimals: 0,
|
|
101
|
+
locale: "en-US",
|
|
102
|
+
long: false,
|
|
103
|
+
...options
|
|
104
|
+
};
|
|
105
|
+
const base = fromBase(givenBase);
|
|
106
|
+
const absoluteBytes = Math.abs(bytes);
|
|
107
|
+
const space = options?.space ?? true ? " " : "";
|
|
108
|
+
const requestedUnitIndex = BYTE_SIZES.findIndex((unit2) => unit2.short === requestedUnit);
|
|
109
|
+
if (bytes === 0) {
|
|
110
|
+
const level2 = Math.min(0, Math.max(requestedUnitIndex, BYTE_SIZES.length - 1));
|
|
111
|
+
return "0" + space + BYTE_SIZES[level2][long ? "long" : "short"];
|
|
112
|
+
}
|
|
113
|
+
const level = requestedUnitIndex >= 0 ? requestedUnitIndex : Math.min(Math.floor(Math.log(absoluteBytes) / Math.log(base)), BYTE_SIZES.length - 1);
|
|
114
|
+
const unit = BYTE_SIZES[level][long ? "long" : "short"];
|
|
115
|
+
const value = bytes / base ** level;
|
|
116
|
+
const fractionDigits = decimals < 0 ? undefined : decimals;
|
|
117
|
+
const formattedValue = new Intl.NumberFormat(locale, {
|
|
118
|
+
maximumFractionDigits: fractionDigits,
|
|
119
|
+
minimumFractionDigits: fractionDigits,
|
|
120
|
+
...l10nOptions
|
|
121
|
+
}).format(value);
|
|
122
|
+
return formattedValue + space + unit;
|
|
123
|
+
}, "formatBytes");
|
|
124
|
+
|
|
125
|
+
exports.formatBytes = formatBytes;
|
|
126
|
+
exports.parseBytes = parseBytes;
|