@mll-lab/js-utils 2.2.0 → 2.6.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
@@ -1,3 +1,7 @@
1
+ export declare type NonEmptyArray<T> = Array<T> & {
2
+ 0: T;
3
+ };
4
+ export declare function isNonEmptyArray<T>(value: Array<T>): value is NonEmptyArray<T>;
1
5
  /**
2
6
  * Return a new array that does not contain the item at the specified index.
3
7
  */
@@ -18,3 +22,4 @@ export declare const EMPTY_ARRAY: never[];
18
22
  * @example const arr = ['x', ...insertIf(foo === 42, 'y', 'z')]
19
23
  */
20
24
  export declare function insertIf<T>(condition: boolean, ...elements: Array<T>): Array<T>;
25
+ export declare function last<T, A extends Array<T>>(array: A): A extends NonEmptyArray<T> ? T : T | undefined;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Extract a message from a caught error.
3
+ *
4
+ * Since you can throw anything in JavaScript,
5
+ * the given error could be of any type.
6
+ */
7
+ export declare function errorMessage(error: unknown): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -9432,6 +9432,9 @@ if(freeModule){// Export for Node.js.
9432
9432
  freeExports._=_;}else {// Export to the global object.
9433
9433
  root._=_;}}).call(commonjsGlobal);});
9434
9434
 
9435
+ function isNonEmptyArray(value) {
9436
+ return value.length > 0;
9437
+ }
9435
9438
  /**
9436
9439
  * Return a new array that does not contain the item at the specified index.
9437
9440
  */
@@ -9459,6 +9462,10 @@ Object.freeze(EMPTY_ARRAY);
9459
9462
  function insertIf(condition, ...elements) {
9460
9463
  return condition ? elements : [];
9461
9464
  }
9465
+ function last(array) {
9466
+ // @ts-expect-error too magical
9467
+ return array[array.length - 1];
9468
+ }
9462
9469
 
9463
9470
  function toInteger(dirtyNumber) {
9464
9471
  if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
@@ -15104,6 +15111,23 @@ function formatDotlessDate(date) {
15104
15111
  return format(date, DOTLESS_DATE_FORMAT, { locale: de$1 });
15105
15112
  }
15106
15113
 
15114
+ /**
15115
+ * Extract a message from a caught error.
15116
+ *
15117
+ * Since you can throw anything in JavaScript,
15118
+ * the given error could be of any type.
15119
+ */
15120
+ function errorMessage(error) {
15121
+ const message = hasMessage(error) ? error.message : error;
15122
+ if (message === undefined) {
15123
+ return 'undefined';
15124
+ }
15125
+ return typeof message === 'string' ? message : JSON.stringify(error);
15126
+ }
15127
+ function hasMessage(error) {
15128
+ return typeof error === 'object' && error !== null && 'message' in error;
15129
+ }
15130
+
15107
15131
  function pluralize(amount, singular, plural) {
15108
15132
  if (amount === 1) {
15109
15133
  return singular;
@@ -15143,6 +15167,9 @@ const isURL = (value) => {
15143
15167
  const isWord = (value) => isString(value) && /^[\w-]+$/.test(value);
15144
15168
  const isLabId = (value) => isString(value) && /^\d{2}-\d{6}$/.test(value);
15145
15169
  const isRackBarcode = (value) => isString(value) && /^[A-Z]{2}\d{8}$/.test(value);
15170
+ function isNotNullish(value) {
15171
+ return value != null;
15172
+ }
15146
15173
 
15147
15174
  /**
15148
15175
  * Round a number to a given number of decimal places.
@@ -15154,6 +15181,16 @@ function round(number, decimalPlaces) {
15154
15181
  return Math.round(number * factorOfTen) / factorOfTen;
15155
15182
  }
15156
15183
 
15184
+ function includesIgnoreCase(needle, haystack) {
15185
+ if (haystack instanceof Array) {
15186
+ return haystack.some((hay) => includesIgnoreCase(needle, hay));
15187
+ }
15188
+ return haystack.toLowerCase().includes(needle.toLowerCase());
15189
+ }
15190
+ function firstLine(multilineText) {
15191
+ return multilineText.split('\n', 1)[0];
15192
+ }
15193
+
15157
15194
  exports.DOTLESS_DATE_FORMAT = DOTLESS_DATE_FORMAT;
15158
15195
  exports.EMAIL_REGEX = EMAIL_REGEX;
15159
15196
  exports.EMPTY_ARRAY = EMPTY_ARRAY;
@@ -15164,6 +15201,8 @@ exports.ISO_DATE_FORMAT = ISO_DATE_FORMAT;
15164
15201
  exports.ISO_DATE_TIME_FORMAT = ISO_DATE_TIME_FORMAT;
15165
15202
  exports.SECONDLESS_DATE_TIME_FORMAT = SECONDLESS_DATE_TIME_FORMAT;
15166
15203
  exports.containSameValues = containSameValues;
15204
+ exports.errorMessage = errorMessage;
15205
+ exports.firstLine = firstLine;
15167
15206
  exports.formatDotlessDate = formatDotlessDate;
15168
15207
  exports.formatGerman = formatGerman;
15169
15208
  exports.formatGermanDateTime = formatGermanDateTime;
@@ -15171,18 +15210,22 @@ exports.formatGermanDotlessDate = formatGermanDotlessDate;
15171
15210
  exports.formatIsoDate = formatIsoDate;
15172
15211
  exports.formatIsoDateTime = formatIsoDateTime;
15173
15212
  exports.formatSecondlessDateTime = formatSecondlessDateTime;
15213
+ exports.includesIgnoreCase = includesIgnoreCase;
15174
15214
  exports.insertIf = insertIf;
15175
15215
  exports.isAlphanumeric = isAlphanumeric;
15176
15216
  exports.isBSNR = isBSNR;
15177
15217
  exports.isEmail = isEmail;
15178
15218
  exports.isFuture = isFuture;
15179
15219
  exports.isLabId = isLabId;
15220
+ exports.isNonEmptyArray = isNonEmptyArray;
15221
+ exports.isNotNullish = isNotNullish;
15180
15222
  exports.isOnlyDigits = isOnlyDigits;
15181
15223
  exports.isRackBarcode = isRackBarcode;
15182
15224
  exports.isString = isString;
15183
15225
  exports.isToday = isToday;
15184
15226
  exports.isURL = isURL;
15185
15227
  exports.isWord = isWord;
15228
+ exports.last = last;
15186
15229
  exports.parseDate = parseDate;
15187
15230
  exports.parseDotlessDate = parseDotlessDate;
15188
15231
  exports.parseGermanDate = parseGermanDate;