@mll-lab/js-utils 2.9.0 → 2.12.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/array.d.ts CHANGED
@@ -23,3 +23,9 @@ export declare const EMPTY_ARRAY: never[];
23
23
  */
24
24
  export declare function insertIf<T>(condition: boolean, ...elements: Array<T>): Array<T>;
25
25
  export declare function last<T, A extends Array<T>>(array: A): A extends NonEmptyArray<T> ? T : T | undefined;
26
+ /**
27
+ * Appends element to array if not included or removes it.
28
+ *
29
+ * Never mutates the given array, always returns a new array.
30
+ */
31
+ export declare function toggleElement<T>(array: Array<T>, element: T): Array<T>;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Detects if a mobile device is used
3
+ */
4
+ export declare function isMobileDevice(): boolean;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ export declare const GERMAN_THOUSAND_SEPARATOR = ".";
2
+ export declare const GERMAN_DECIMAL_SEPARATOR = ",";
3
+ export declare const ENGLISH_THOUSAND_SEPARATOR = ",";
4
+ export declare const ENGLISH_DECIMAL_SEPARATOR = ".";
5
+ declare type FormatNumberOptions = Intl.NumberFormatOptions & {
6
+ defaultValue?: string;
7
+ };
8
+ export declare function formatGermanNumber(value: number | string | null | undefined, { defaultValue, ...localeOptions }?: FormatNumberOptions): string;
9
+ /**
10
+ * Parse an input into a number on a best effort basis.
11
+ *
12
+ * If the value can not be parsed, this function returns null.
13
+ */
14
+ export declare function parseGermanNumber(value: string | null | undefined): number | null;
15
+ /**
16
+ * Check if the given input might be on track to become a parseable german number.
17
+ *
18
+ * The regular expression must be quite lenient, as it is used to validate numbers
19
+ * as they are being typed. We just want to improve the user experience by disallowing
20
+ * them to enter something totally nonsensical.
21
+ */
22
+ export declare function isPartialGermanNumber(value: string): boolean;
23
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -9466,6 +9466,16 @@ function last(array) {
9466
9466
  // @ts-expect-error too magical
9467
9467
  return array[array.length - 1];
9468
9468
  }
9469
+ /**
9470
+ * Appends element to array if not included or removes it.
9471
+ *
9472
+ * Never mutates the given array, always returns a new array.
9473
+ */
9474
+ function toggleElement(array, element) {
9475
+ return array.includes(element)
9476
+ ? array.filter((e) => e !== element)
9477
+ : array.concat(element);
9478
+ }
9469
9479
 
9470
9480
  function toInteger(dirtyNumber) {
9471
9481
  if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
@@ -15111,6 +15121,14 @@ function formatDotlessDate(date) {
15111
15121
  return format(date, DOTLESS_DATE_FORMAT, { locale: de$1 });
15112
15122
  }
15113
15123
 
15124
+ /**
15125
+ * Detects if a mobile device is used
15126
+ */
15127
+ function isMobileDevice() {
15128
+ const userAgent = typeof window.navigator === 'undefined' ? '' : navigator.userAgent;
15129
+ return Boolean(userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i));
15130
+ }
15131
+
15114
15132
  /**
15115
15133
  * Extract a message from a caught error.
15116
15134
  *
@@ -15128,6 +15146,63 @@ function hasMessage(error) {
15128
15146
  return typeof error === 'object' && error !== null && 'message' in error;
15129
15147
  }
15130
15148
 
15149
+ function __rest(s, e) {
15150
+ var t = {};
15151
+
15152
+ for (var p in s) {
15153
+ if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
15154
+ }
15155
+
15156
+ if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
15157
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
15158
+ }
15159
+ return t;
15160
+ }
15161
+
15162
+ const GERMAN_THOUSAND_SEPARATOR = '.';
15163
+ const GERMAN_DECIMAL_SEPARATOR = ',';
15164
+ const ENGLISH_THOUSAND_SEPARATOR = ',';
15165
+ const ENGLISH_DECIMAL_SEPARATOR = '.';
15166
+ function formatGermanNumber(value, _a = {}) {
15167
+ var { defaultValue } = _a, localeOptions = __rest(_a, ["defaultValue"]);
15168
+ if (!value) {
15169
+ return '';
15170
+ }
15171
+ const parsed = typeof value === 'string' ? Number.parseFloat(value) : value;
15172
+ if (Number.isNaN(parsed)) {
15173
+ return defaultValue !== null && defaultValue !== void 0 ? defaultValue : '';
15174
+ }
15175
+ return parsed.toLocaleString('de-DE', Object.assign(Object.assign({}, localeOptions), { maximumFractionDigits: 6 }));
15176
+ }
15177
+ /**
15178
+ * Parse an input into a number on a best effort basis.
15179
+ *
15180
+ * If the value can not be parsed, this function returns null.
15181
+ */
15182
+ function parseGermanNumber(value) {
15183
+ if (value === null || value === undefined) {
15184
+ return null;
15185
+ }
15186
+ const normalizedValue = value
15187
+ .replace(new RegExp(`\\${GERMAN_THOUSAND_SEPARATOR}`, 'g'), '')
15188
+ .replace(GERMAN_DECIMAL_SEPARATOR, ENGLISH_DECIMAL_SEPARATOR);
15189
+ const parsed = Number.parseFloat(normalizedValue);
15190
+ if (Number.isNaN(parsed)) {
15191
+ return null;
15192
+ }
15193
+ return parsed;
15194
+ }
15195
+ /**
15196
+ * Check if the given input might be on track to become a parseable german number.
15197
+ *
15198
+ * The regular expression must be quite lenient, as it is used to validate numbers
15199
+ * as they are being typed. We just want to improve the user experience by disallowing
15200
+ * them to enter something totally nonsensical.
15201
+ */
15202
+ function isPartialGermanNumber(value) {
15203
+ return /^-?\d*[.\d]*,?\d*$/.test(value);
15204
+ }
15205
+
15131
15206
  /**
15132
15207
  * Round a number to a given number of decimal places.
15133
15208
  *
@@ -15225,9 +15300,13 @@ function pxToNumber(pixels) {
15225
15300
  exports.DOTLESS_DATE_FORMAT = DOTLESS_DATE_FORMAT;
15226
15301
  exports.EMAIL_REGEX = EMAIL_REGEX;
15227
15302
  exports.EMPTY_ARRAY = EMPTY_ARRAY;
15303
+ exports.ENGLISH_DECIMAL_SEPARATOR = ENGLISH_DECIMAL_SEPARATOR;
15304
+ exports.ENGLISH_THOUSAND_SEPARATOR = ENGLISH_THOUSAND_SEPARATOR;
15228
15305
  exports.GERMAN_DATE_FORMAT = GERMAN_DATE_FORMAT;
15229
15306
  exports.GERMAN_DATE_TIME_FORMAT = GERMAN_DATE_TIME_FORMAT;
15307
+ exports.GERMAN_DECIMAL_SEPARATOR = GERMAN_DECIMAL_SEPARATOR;
15230
15308
  exports.GERMAN_DOTLESS_DATE_FORMAT = GERMAN_DOTLESS_DATE_FORMAT;
15309
+ exports.GERMAN_THOUSAND_SEPARATOR = GERMAN_THOUSAND_SEPARATOR;
15231
15310
  exports.ISO_DATE_FORMAT = ISO_DATE_FORMAT;
15232
15311
  exports.ISO_DATE_TIME_FORMAT = ISO_DATE_TIME_FORMAT;
15233
15312
  exports.SECONDLESS_DATE_TIME_FORMAT = SECONDLESS_DATE_TIME_FORMAT;
@@ -15239,6 +15318,7 @@ exports.formatDotlessDate = formatDotlessDate;
15239
15318
  exports.formatGerman = formatGerman;
15240
15319
  exports.formatGermanDateTime = formatGermanDateTime;
15241
15320
  exports.formatGermanDotlessDate = formatGermanDotlessDate;
15321
+ exports.formatGermanNumber = formatGermanNumber;
15242
15322
  exports.formatIsoDate = formatIsoDate;
15243
15323
  exports.formatIsoDateTime = formatIsoDateTime;
15244
15324
  exports.formatSecondlessDateTime = formatSecondlessDateTime;
@@ -15249,9 +15329,11 @@ exports.isBSNR = isBSNR;
15249
15329
  exports.isEmail = isEmail;
15250
15330
  exports.isFuture = isFuture;
15251
15331
  exports.isLabId = isLabId;
15332
+ exports.isMobileDevice = isMobileDevice;
15252
15333
  exports.isNonEmptyArray = isNonEmptyArray;
15253
15334
  exports.isNotNullish = isNotNullish;
15254
15335
  exports.isOnlyDigits = isOnlyDigits;
15336
+ exports.isPartialGermanNumber = isPartialGermanNumber;
15255
15337
  exports.isRackBarcode = isRackBarcode;
15256
15338
  exports.isString = isString;
15257
15339
  exports.isToday = isToday;
@@ -15265,11 +15347,13 @@ exports.parseGermanDate = parseGermanDate;
15265
15347
  exports.parseGermanDateFlexible = parseGermanDateFlexible;
15266
15348
  exports.parseGermanDateTime = parseGermanDateTime;
15267
15349
  exports.parseGermanDotlessDate = parseGermanDotlessDate;
15350
+ exports.parseGermanNumber = parseGermanNumber;
15268
15351
  exports.parseIsoDate = parseIsoDate;
15269
15352
  exports.parseIsoDateTime = parseIsoDateTime;
15270
15353
  exports.parseSecondlessDateTime = parseSecondlessDateTime;
15271
15354
  exports.pluralize = pluralize;
15272
15355
  exports.pxToNumber = pxToNumber;
15273
15356
  exports.round = round;
15357
+ exports.toggleElement = toggleElement;
15274
15358
  exports.withoutIndex = withoutIndex;
15275
15359
  //# sourceMappingURL=index.common.js.map