@mll-lab/js-utils 2.18.0 → 2.19.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,4 @@
1
+ import { Maybe } from './types';
1
2
  export declare type NonEmptyArray<T> = Array<T> & {
2
3
  0: T;
3
4
  };
@@ -40,3 +41,10 @@ export declare function toggleElement<T>(array: Array<T>, element: T): Array<T>;
40
41
  * returns sorted array [Jan, Mar, Dec]
41
42
  */
42
43
  export declare function sortByArray<T extends string | number>(subject: Array<T>, recipe: Array<T>): Array<T>;
44
+ /**
45
+ * Takes a function that maps the values to sort and returns a compare function
46
+ * using `String.prototype.localeCompare`, usable in `Array.sort` or similar APIs.
47
+ *
48
+ * null, undefined and the empty string are not distinguished and first in sort order.
49
+ */
50
+ export declare function makeStringCompareFn<TSortable>(map: (sortable: TSortable) => Maybe<string>): (a: TSortable, b: TSortable) => number;
@@ -9491,6 +9491,20 @@ function sortByArray(subject, recipe) {
9491
9491
  }
9492
9492
  return lodash.sortBy(subject, (value) => recipe.indexOf(value));
9493
9493
  }
9494
+ /**
9495
+ * Takes a function that maps the values to sort and returns a compare function
9496
+ * using `String.prototype.localeCompare`, usable in `Array.sort` or similar APIs.
9497
+ *
9498
+ * null, undefined and the empty string are not distinguished and first in sort order.
9499
+ */
9500
+ function makeStringCompareFn(map) {
9501
+ return (a, b) => {
9502
+ var _a, _b;
9503
+ const mappedA = (_a = map(a)) !== null && _a !== void 0 ? _a : '';
9504
+ const mappedB = (_b = map(b)) !== null && _b !== void 0 ? _b : '';
9505
+ return mappedA.localeCompare(mappedB);
9506
+ };
9507
+ }
9494
9508
 
9495
9509
  function toInteger(dirtyNumber) {
9496
9510
  if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
@@ -15484,6 +15498,7 @@ exports.isURL = isURL;
15484
15498
  exports.isWord = isWord;
15485
15499
  exports.joinNonEmpty = joinNonEmpty;
15486
15500
  exports.last = last;
15501
+ exports.makeStringCompareFn = makeStringCompareFn;
15487
15502
  exports.mapSequentially = mapSequentially;
15488
15503
  exports.parseDate = parseDate;
15489
15504
  exports.parseDotlessDate = parseDotlessDate;