@mll-lab/js-utils 2.8.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/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,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) {
@@ -15128,6 +15138,63 @@ function hasMessage(error) {
15128
15138
  return typeof error === 'object' && error !== null && 'message' in error;
15129
15139
  }
15130
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
+
15131
15198
  /**
15132
15199
  * Round a number to a given number of decimal places.
15133
15200
  *
@@ -15205,12 +15272,33 @@ function joinNonEmpty(maybeStrings, separator) {
15205
15272
  return maybeStrings.filter(Boolean).join(separator);
15206
15273
  }
15207
15274
 
15275
+ /**
15276
+ * Converts pixel value to number.
15277
+ *
15278
+ * @example "4px" to 4
15279
+ */
15280
+ function pxToNumber(pixels) {
15281
+ const count = (pixels.match(/px/g) || []).length;
15282
+ if (count > 1 || !pixels.endsWith('px')) {
15283
+ throw new Error(`'${pixels}' does not contain a single 'px'`);
15284
+ }
15285
+ const parsedValue = parseFloat(pixels);
15286
+ if (Number.isNaN(parsedValue)) {
15287
+ throw new Error(`'${pixels}' is not a valid single pixel-value`);
15288
+ }
15289
+ return parsedValue;
15290
+ }
15291
+
15208
15292
  exports.DOTLESS_DATE_FORMAT = DOTLESS_DATE_FORMAT;
15209
15293
  exports.EMAIL_REGEX = EMAIL_REGEX;
15210
15294
  exports.EMPTY_ARRAY = EMPTY_ARRAY;
15295
+ exports.ENGLISH_DECIMAL_SEPARATOR = ENGLISH_DECIMAL_SEPARATOR;
15296
+ exports.ENGLISH_THOUSAND_SEPARATOR = ENGLISH_THOUSAND_SEPARATOR;
15211
15297
  exports.GERMAN_DATE_FORMAT = GERMAN_DATE_FORMAT;
15212
15298
  exports.GERMAN_DATE_TIME_FORMAT = GERMAN_DATE_TIME_FORMAT;
15299
+ exports.GERMAN_DECIMAL_SEPARATOR = GERMAN_DECIMAL_SEPARATOR;
15213
15300
  exports.GERMAN_DOTLESS_DATE_FORMAT = GERMAN_DOTLESS_DATE_FORMAT;
15301
+ exports.GERMAN_THOUSAND_SEPARATOR = GERMAN_THOUSAND_SEPARATOR;
15214
15302
  exports.ISO_DATE_FORMAT = ISO_DATE_FORMAT;
15215
15303
  exports.ISO_DATE_TIME_FORMAT = ISO_DATE_TIME_FORMAT;
15216
15304
  exports.SECONDLESS_DATE_TIME_FORMAT = SECONDLESS_DATE_TIME_FORMAT;
@@ -15222,6 +15310,7 @@ exports.formatDotlessDate = formatDotlessDate;
15222
15310
  exports.formatGerman = formatGerman;
15223
15311
  exports.formatGermanDateTime = formatGermanDateTime;
15224
15312
  exports.formatGermanDotlessDate = formatGermanDotlessDate;
15313
+ exports.formatGermanNumber = formatGermanNumber;
15225
15314
  exports.formatIsoDate = formatIsoDate;
15226
15315
  exports.formatIsoDateTime = formatIsoDateTime;
15227
15316
  exports.formatSecondlessDateTime = formatSecondlessDateTime;
@@ -15235,6 +15324,7 @@ exports.isLabId = isLabId;
15235
15324
  exports.isNonEmptyArray = isNonEmptyArray;
15236
15325
  exports.isNotNullish = isNotNullish;
15237
15326
  exports.isOnlyDigits = isOnlyDigits;
15327
+ exports.isPartialGermanNumber = isPartialGermanNumber;
15238
15328
  exports.isRackBarcode = isRackBarcode;
15239
15329
  exports.isString = isString;
15240
15330
  exports.isToday = isToday;
@@ -15248,10 +15338,13 @@ exports.parseGermanDate = parseGermanDate;
15248
15338
  exports.parseGermanDateFlexible = parseGermanDateFlexible;
15249
15339
  exports.parseGermanDateTime = parseGermanDateTime;
15250
15340
  exports.parseGermanDotlessDate = parseGermanDotlessDate;
15341
+ exports.parseGermanNumber = parseGermanNumber;
15251
15342
  exports.parseIsoDate = parseIsoDate;
15252
15343
  exports.parseIsoDateTime = parseIsoDateTime;
15253
15344
  exports.parseSecondlessDateTime = parseSecondlessDateTime;
15254
15345
  exports.pluralize = pluralize;
15346
+ exports.pxToNumber = pxToNumber;
15255
15347
  exports.round = round;
15348
+ exports.toggleElement = toggleElement;
15256
15349
  exports.withoutIndex = withoutIndex;
15257
15350
  //# sourceMappingURL=index.common.js.map