@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/array.d.ts +2 -2
- package/dist/assert.d.ts +16 -0
- package/dist/assert.test.d.ts +1 -0
- package/dist/index.common.js +29 -3
- package/dist/index.common.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +29 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/array.d.ts
CHANGED
|
@@ -65,9 +65,9 @@ export declare function makeBooleanCompareFn<TSortable>(map: (sortable: TSortabl
|
|
|
65
65
|
* Takes a function that maps the values to sort to Dates and returns a compare function
|
|
66
66
|
* using their timestamps, usable in `Array.toSorted` or similar APIs.
|
|
67
67
|
*
|
|
68
|
-
* null and undefined are coalesced to
|
|
68
|
+
* null and undefined are coalesced to the oldest possible date and thus not distinguished and first in sort order.
|
|
69
69
|
*/
|
|
70
|
-
export declare function makeDateCompareFn<TSortable>(map: (sortable: TSortable) => Maybe<Date
|
|
70
|
+
export declare function makeDateCompareFn<TSortable>(map: (sortable: TSortable) => Maybe<Date>, fallbackValue?: Date): (a: TSortable, b: TSortable) => number;
|
|
71
71
|
/**
|
|
72
72
|
* Returns a compare function for values that are string, null or undefined,
|
|
73
73
|
* using `String.prototype.localeCompare`, usable in `Array.toSorted` or similar APIs.
|
package/dist/assert.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that a value is a string at runtime.
|
|
3
|
+
* Throws an error if the value is not a string.
|
|
4
|
+
*
|
|
5
|
+
* Use this function instead of unsafe type casts like `as string` when you need
|
|
6
|
+
* to ensure a value is actually a string at runtime.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to check
|
|
9
|
+
* @returns The value as a string if it passes the check
|
|
10
|
+
* @throws Error if the value is not a string
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const id = assertString(someUnknownValue);
|
|
14
|
+
* // Now id is safely typed as string and verified at runtime
|
|
15
|
+
*/
|
|
16
|
+
export declare function assertString(value: unknown): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.common.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
|
|
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
|
-
|
|
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;
|