@mll-lab/js-utils 2.39.0 → 2.41.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,4 +1,5 @@
1
1
  export * from './array';
2
+ export * from './assert';
2
3
  export * from './date';
3
4
  export * from './device';
4
5
  export * from './error';
package/dist/index.js CHANGED
@@ -9598,10 +9598,13 @@ function makeBooleanCompareFn(map) {
9598
9598
  * Takes a function that maps the values to sort to Dates and returns a compare function
9599
9599
  * using their timestamps, usable in `Array.toSorted` or similar APIs.
9600
9600
  *
9601
- * null and undefined are coalesced to 0 and thus not distinguished and first in sort order.
9601
+ * null and undefined are coalesced to the oldest possible date and thus not distinguished and first in sort order.
9602
9602
  */
9603
- function makeDateCompareFn(map) {
9604
- return makeNumberCompareFn((sortable) => { var _a; return (_a = map(sortable)) === null || _a === void 0 ? void 0 : _a.getTime(); });
9603
+ function makeDateCompareFn(map,
9604
+ // See https://stackoverflow.com/a/11526569 for an explanation of this value.
9605
+ fallbackValue = new Date(-8640000000000000)) {
9606
+ const fallbackTime = fallbackValue.getTime();
9607
+ return makeNumberCompareFn((sortable) => { var _a, _b; return (_b = (_a = map(sortable)) === null || _a === void 0 ? void 0 : _a.getTime()) !== null && _b !== void 0 ? _b : fallbackTime; });
9605
9608
  }
9606
9609
  /**
9607
9610
  * Returns a compare function for values that are string, null or undefined,
@@ -9620,6 +9623,28 @@ function soleItem(array) {
9620
9623
  return array[0];
9621
9624
  }
9622
9625
 
9626
+ /**
9627
+ * Asserts that a value is a string at runtime.
9628
+ * Throws an error if the value is not a string.
9629
+ *
9630
+ * Use this function instead of unsafe type casts like `as string` when you need
9631
+ * to ensure a value is actually a string at runtime.
9632
+ *
9633
+ * @param value - The value to check
9634
+ * @returns The value as a string if it passes the check
9635
+ * @throws Error if the value is not a string
9636
+ *
9637
+ * @example
9638
+ * const id = assertString(someUnknownValue);
9639
+ * // Now id is safely typed as string and verified at runtime
9640
+ */
9641
+ function assertString(value) {
9642
+ if (typeof value !== 'string') {
9643
+ throw new Error(`Expected a string, but got ${typeof value}`);
9644
+ }
9645
+ return value;
9646
+ }
9647
+
9623
9648
  /**
9624
9649
  * @name toDate
9625
9650
  * @category Common Helpers
@@ -16303,6 +16328,7 @@ exports.GERMAN_THOUSAND_SEPARATOR = GERMAN_THOUSAND_SEPARATOR;
16303
16328
  exports.ISO_DATE_FORMAT = ISO_DATE_FORMAT;
16304
16329
  exports.ISO_DATE_TIME_FORMAT = ISO_DATE_TIME_FORMAT;
16305
16330
  exports.SECONDLESS_DATE_TIME_FORMAT = SECONDLESS_DATE_TIME_FORMAT;
16331
+ exports.assertString = assertString;
16306
16332
  exports.callSequentially = callSequentially;
16307
16333
  exports.ceil = ceil;
16308
16334
  exports.containSameValues = containSameValues;