@visulima/humanizer 3.0.0-alpha.8 → 3.0.0-alpha.9

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.js CHANGED
@@ -1,3 +1,3 @@
1
- export { formatBytes, parseBytes } from './packem_shared/parseBytes-JWspeMzP.js';
2
- export { default as duration } from './packem_shared/duration-C62ipnQn.js';
3
- export { default as parseDuration } from './packem_shared/parseDuration-CeFaBgx9.js';
1
+ export { formatBytes, parseBytes } from './packem_shared/parseBytes-BWhfsrbP.js';
2
+ export { default as duration } from './packem_shared/duration-Br8Mr-Lj.js';
3
+ export { default as parseDuration } from './packem_shared/parseDuration-qO1Y4eiu.js';
@@ -28,20 +28,18 @@ const roUnitMap = {
28
28
  zi: "d",
29
29
  zile: "d"
30
30
  };
31
- const romanianUnit = (unit) => {
32
- return (counter) => {
33
- if (counter === 1) {
34
- return unit[0];
35
- }
36
- if (Math.floor(counter) !== counter || counter === 0) {
37
- return unit[1];
38
- }
39
- const remainder = counter % 100;
40
- if (remainder >= 1 && remainder <= 19) {
41
- return unit[1];
42
- }
43
- return unit[2];
44
- };
31
+ const romanianUnit = (unit) => (counter) => {
32
+ if (counter === 1) {
33
+ return unit[0];
34
+ }
35
+ if (Math.floor(counter) !== counter || counter === 0) {
36
+ return unit[1];
37
+ }
38
+ const remainder = counter % 100;
39
+ if (remainder >= 1 && remainder <= 19) {
40
+ return unit[1];
41
+ }
42
+ return unit[2];
45
43
  };
46
44
  const durationLanguage = createDurationLanguage(
47
45
  romanianUnit(["an", "ani", "de ani"]),
@@ -1 +1,2 @@
1
+ import type { DurationLanguage } from "../types.d.ts";
1
2
  export declare const durationLanguage: DurationLanguage;
@@ -1,14 +1,15 @@
1
1
  const validateDurationLanguage = (language) => {
2
2
  const requiredProperties = ["y", "mo", "w", "d", "h", "m", "s", "ms", "future", "past"];
3
3
  for (const property of requiredProperties) {
4
- if (!Object.prototype.hasOwnProperty.call(language, property)) {
4
+ if (!Object.hasOwn(language, property)) {
5
5
  throw new TypeError(`Missing required property: ${property}`);
6
6
  }
7
7
  }
8
8
  if (typeof language.future !== "string" || typeof language.past !== "string") {
9
9
  throw new TypeError("Properties future and past must be of type string");
10
10
  }
11
- for (const property of ["y", "mo", "w", "d", "h", "m", "s", "ms"]) {
11
+ const unitProperties = ["y", "mo", "w", "d", "h", "m", "s", "ms"];
12
+ for (const property of unitProperties) {
12
13
  if (typeof language[property] !== "string" && typeof language[property] !== "function") {
13
14
  throw new TypeError(`Property ${property} must be of type string or function`);
14
15
  }
@@ -88,7 +88,7 @@ const getPieces = (ms, options) => {
88
88
  for (let index_ = index + 1; index_ < units.length; index_++) {
89
89
  const smallerUnitName = units[index_];
90
90
  const smallerUnitCount = unitCounts[smallerUnitName];
91
- unitCounts[unitName] += smallerUnitCount * unitMeasures[smallerUnitName] / unitMeasures[unitName];
91
+ unitCounts[unitName] = (unitCounts[unitName] ?? 0) + smallerUnitCount * unitMeasures[smallerUnitName] / unitMeasures[unitName];
92
92
  unitCounts[smallerUnitName] = 0;
93
93
  }
94
94
  break;
@@ -109,7 +109,7 @@ const getPieces = (ms, options) => {
109
109
  const previousUnitMs = unitMeasures[previousUnitName];
110
110
  const amountOfPreviousUnit = Math.floor(rounded * unitMeasures[unitName] / previousUnitMs);
111
111
  if (amountOfPreviousUnit) {
112
- unitCounts[previousUnitName] += amountOfPreviousUnit;
112
+ unitCounts[previousUnitName] = (unitCounts[previousUnitName] ?? 0) + amountOfPreviousUnit;
113
113
  unitCounts[unitName] = 0;
114
114
  } else {
115
115
  break;
@@ -158,18 +158,14 @@ const formatPieces = (pieces, options, ms) => {
158
158
  adverb = language.past ?? "";
159
159
  }
160
160
  }
161
- const renderedPieces = [];
162
- for (const piece_ of pieces) {
163
- const piece = piece_;
164
- renderedPieces.push(renderPiece(piece, language, options));
165
- }
161
+ const renderedPieces = pieces.map((piece) => renderPiece(piece, language, options));
166
162
  let result;
167
163
  if (!conjunction || pieces.length === 1) {
168
164
  result = renderedPieces.join(delimiter);
169
165
  } else if (pieces.length === 2) {
170
166
  result = renderedPieces.join(conjunction);
171
167
  } else {
172
- result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + renderedPieces.at(-1);
168
+ result = renderedPieces.slice(0, -1).join(delimiter) + (serialComma ? "," : "") + conjunction + (renderedPieces.at(-1) ?? "");
173
169
  }
174
170
  if (adverb) {
175
171
  result = adverb.replace("%s", result);
@@ -1,3 +1,13 @@
1
+ const PARSE_BYTES_REGEX = /^(?<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;
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$/;
1
11
  const BYTE_SIZES = {
2
12
  iec: [
3
13
  {
@@ -164,7 +174,7 @@ const fromBase = (base) => {
164
174
  if (base === 10) {
165
175
  return 1e3;
166
176
  }
167
- throw new TypeError(`Unsupported base.`);
177
+ throw new Error("Unsupported base.");
168
178
  };
169
179
  const parseBytes = (value, options) => {
170
180
  const config = {
@@ -179,15 +189,13 @@ const parseBytes = (value, options) => {
179
189
  if (value.length > 100) {
180
190
  throw new TypeError("Value exceeds the maximum length of 100 characters.");
181
191
  }
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
- );
192
+ const match = PARSE_BYTES_REGEX.exec(value);
185
193
  const groups = match?.groups;
186
194
  if (!groups) {
187
195
  return Number.NaN;
188
196
  }
189
197
  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");
198
+ 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");
191
199
  const level = BYTE_SIZES[config.units].findIndex((unit) => unit.short[0].toUpperCase() === type[0]);
192
200
  const base = fromBase(config.base);
193
201
  return localizedNumber * base ** level;
@@ -15,6 +15,7 @@ const ESCAPE_REGEX = /[-/\\^$*+?.()|[\]{}]/g;
15
15
  const ISO_FORMAT = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/i;
16
16
  const COLON_FORMAT = /^(?:(\d+):)?(?:(\d+):)?(\d+)$/;
17
17
  const NUMERIC_STRING_REGEX = /^[+-]?\d+(?:\.\d+)?$/;
18
+ const SIGN_PREFIX_REGEX = /^[-+]/;
18
19
  const parseDuration = (value, options) => {
19
20
  if (typeof value !== "string" || value.length === 0) {
20
21
  return void 0;
@@ -53,14 +54,13 @@ const parseDuration = (value, options) => {
53
54
  if (colonMatch) {
54
55
  let hours = 0;
55
56
  let minutes = 0;
56
- let seconds = 0;
57
57
  if (colonMatch[2] !== void 0) {
58
58
  hours = Number.parseInt(colonMatch[1] ?? "0", 10);
59
59
  minutes = Number.parseInt(colonMatch[2], 10);
60
60
  } else if (colonMatch[1] !== void 0) {
61
61
  minutes = Number.parseInt(colonMatch[1], 10);
62
62
  }
63
- seconds = Number.parseInt(colonMatch[3] ?? "0", 10);
63
+ const seconds = Number.parseInt(colonMatch[3] ?? "0", 10);
64
64
  return hours * 36e5 + minutes * 6e4 + seconds * 1e3;
65
65
  }
66
66
  const currentUnitMapKeys = Object.keys(currentUnitMap);
@@ -84,7 +84,7 @@ const parseDuration = (value, options) => {
84
84
  }
85
85
  const trimmedNumberString = numberString.trim();
86
86
  const sign = trimmedNumberString.startsWith("-") ? -1 : 1;
87
- const absNumberString = trimmedNumberString.replace(/^[-+]/, "");
87
+ const absNumberString = trimmedNumberString.replace(SIGN_PREFIX_REGEX, "");
88
88
  const parsedNumber = Number.parseFloat(absNumberString);
89
89
  const unitKey = currentUnitMap[unitString.toLowerCase()];
90
90
  if (unitKey === void 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/humanizer",
3
- "version": "3.0.0-alpha.8",
3
+ "version": "3.0.0-alpha.9",
4
4
  "description": "Humanizer is a library for humanizing data in a human-readable form.",
5
5
  "keywords": [
6
6
  "visulima",
@@ -69,9 +69,6 @@
69
69
  "files": [
70
70
  "dist"
71
71
  ],
72
- "dependencies": {},
73
- "peerDependencies": {},
74
- "optionalDependencies": {},
75
72
  "engines": {
76
73
  "node": ">=22.13 <=25.x"
77
74
  },