-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbyAny.ts
More file actions
32 lines (27 loc) · 1.02 KB
/
Copy pathbyAny.ts
File metadata and controls
32 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import byDate from "../sortables/byDate";
import { sortable } from "../types/types";
import byNumber from "../sortables/byNumber";
import byString from "../sortables/byString";
import { SortOption } from "../interfaces/interfaces";
import { isDate, isNumber, isString } from "../utils/typeCheck";
/**
* the sortable that works on **`number`, `string`, and `Date` values**,
* provided the types are not mixed
* @param options the options to sort the values
*
* {@link https://sort-es.netlify.app/by-default byDefault docs}
* @version 1.8.0
*/
const byAny = <T extends string | number | Date>(
options: SortOption = { desc: false }
): sortable<T> => {
return (first, second) => {
if (isNumber(first) && isNumber(second))
return byNumber(options)(first, second);
if (isString(first) && isString(second))
return byString(options)(first, second);
if (isDate(first) && isDate(second)) return byDate(options)(first, second);
throw new Error("incorrect types of the 2 parameters");
};
};
export default byAny;