@mll-lab/js-utils 2.10.0 → 2.11.0

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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './array';
2
2
  export * from './date';
3
3
  export * from './error';
4
+ export * from './germanNumber';
4
5
  export * from './number';
5
6
  export * from './pluralize';
6
7
  export * from './predicates';
package/dist/index.js CHANGED
@@ -15138,6 +15138,63 @@ function hasMessage(error) {
15138
15138
  return typeof error === 'object' && error !== null && 'message' in error;
15139
15139
  }
15140
15140
 
15141
+ function __rest(s, e) {
15142
+ var t = {};
15143
+
15144
+ for (var p in s) {
15145
+ if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
15146
+ }
15147
+
15148
+ if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
15149
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
15150
+ }
15151
+ return t;
15152
+ }
15153
+
15154
+ const GERMAN_THOUSAND_SEPARATOR = '.';
15155
+ const GERMAN_DECIMAL_SEPARATOR = ',';
15156
+ const ENGLISH_THOUSAND_SEPARATOR = ',';
15157
+ const ENGLISH_DECIMAL_SEPARATOR = '.';
15158
+ function formatGermanNumber(value, _a = {}) {
15159
+ var { defaultValue } = _a, localeOptions = __rest(_a, ["defaultValue"]);
15160
+ if (!value) {
15161
+ return '';
15162
+ }
15163
+ const parsed = typeof value === 'string' ? Number.parseFloat(value) : value;
15164
+ if (Number.isNaN(parsed)) {
15165
+ return defaultValue !== null && defaultValue !== void 0 ? defaultValue : '';
15166
+ }
15167
+ return parsed.toLocaleString('de-DE', Object.assign(Object.assign({}, localeOptions), { maximumFractionDigits: 6 }));
15168
+ }
15169
+ /**
15170
+ * Parse an input into a number on a best effort basis.
15171
+ *
15172
+ * If the value can not be parsed, this function returns null.
15173
+ */
15174
+ function parseGermanNumber(value) {
15175
+ if (value === null || value === undefined) {
15176
+ return null;
15177
+ }
15178
+ const normalizedValue = value
15179
+ .replace(new RegExp(`\\${GERMAN_THOUSAND_SEPARATOR}`, 'g'), '')
15180
+ .replace(GERMAN_DECIMAL_SEPARATOR, ENGLISH_DECIMAL_SEPARATOR);
15181
+ const parsed = Number.parseFloat(normalizedValue);
15182
+ if (Number.isNaN(parsed)) {
15183
+ return null;
15184
+ }
15185
+ return parsed;
15186
+ }
15187
+ /**
15188
+ * Check if the given input might be on track to become a parseable german number.
15189
+ *
15190
+ * The regular expression must be quite lenient, as it is used to validate numbers
15191
+ * as they are being typed. We just want to improve the user experience by disallowing
15192
+ * them to enter something totally nonsensical.
15193
+ */
15194
+ function isPartialGermanNumber(value) {
15195
+ return /^-?\d*[.\d]*,?\d*$/.test(value);
15196
+ }
15197
+
15141
15198
  /**
15142
15199
  * Round a number to a given number of decimal places.
15143
15200
  *
@@ -15235,9 +15292,13 @@ function pxToNumber(pixels) {
15235
15292
  exports.DOTLESS_DATE_FORMAT = DOTLESS_DATE_FORMAT;
15236
15293
  exports.EMAIL_REGEX = EMAIL_REGEX;
15237
15294
  exports.EMPTY_ARRAY = EMPTY_ARRAY;
15295
+ exports.ENGLISH_DECIMAL_SEPARATOR = ENGLISH_DECIMAL_SEPARATOR;
15296
+ exports.ENGLISH_THOUSAND_SEPARATOR = ENGLISH_THOUSAND_SEPARATOR;
15238
15297
  exports.GERMAN_DATE_FORMAT = GERMAN_DATE_FORMAT;
15239
15298
  exports.GERMAN_DATE_TIME_FORMAT = GERMAN_DATE_TIME_FORMAT;
15299
+ exports.GERMAN_DECIMAL_SEPARATOR = GERMAN_DECIMAL_SEPARATOR;
15240
15300
  exports.GERMAN_DOTLESS_DATE_FORMAT = GERMAN_DOTLESS_DATE_FORMAT;
15301
+ exports.GERMAN_THOUSAND_SEPARATOR = GERMAN_THOUSAND_SEPARATOR;
15241
15302
  exports.ISO_DATE_FORMAT = ISO_DATE_FORMAT;
15242
15303
  exports.ISO_DATE_TIME_FORMAT = ISO_DATE_TIME_FORMAT;
15243
15304
  exports.SECONDLESS_DATE_TIME_FORMAT = SECONDLESS_DATE_TIME_FORMAT;
@@ -15249,6 +15310,7 @@ exports.formatDotlessDate = formatDotlessDate;
15249
15310
  exports.formatGerman = formatGerman;
15250
15311
  exports.formatGermanDateTime = formatGermanDateTime;
15251
15312
  exports.formatGermanDotlessDate = formatGermanDotlessDate;
15313
+ exports.formatGermanNumber = formatGermanNumber;
15252
15314
  exports.formatIsoDate = formatIsoDate;
15253
15315
  exports.formatIsoDateTime = formatIsoDateTime;
15254
15316
  exports.formatSecondlessDateTime = formatSecondlessDateTime;
@@ -15262,6 +15324,7 @@ exports.isLabId = isLabId;
15262
15324
  exports.isNonEmptyArray = isNonEmptyArray;
15263
15325
  exports.isNotNullish = isNotNullish;
15264
15326
  exports.isOnlyDigits = isOnlyDigits;
15327
+ exports.isPartialGermanNumber = isPartialGermanNumber;
15265
15328
  exports.isRackBarcode = isRackBarcode;
15266
15329
  exports.isString = isString;
15267
15330
  exports.isToday = isToday;
@@ -15275,6 +15338,7 @@ exports.parseGermanDate = parseGermanDate;
15275
15338
  exports.parseGermanDateFlexible = parseGermanDateFlexible;
15276
15339
  exports.parseGermanDateTime = parseGermanDateTime;
15277
15340
  exports.parseGermanDotlessDate = parseGermanDotlessDate;
15341
+ exports.parseGermanNumber = parseGermanNumber;
15278
15342
  exports.parseIsoDate = parseIsoDate;
15279
15343
  exports.parseIsoDateTime = parseIsoDateTime;
15280
15344
  exports.parseSecondlessDateTime = parseSecondlessDateTime;