1 | import {parse as parseNb} from './number';
|
2 | import {Date as SugarDate} from 'sugar-date';
|
3 |
|
4 | /** Sorting utilities */
|
5 |
|
6 | /**
|
7 | * Case insensitive compare function for passed strings
|
8 | * @param {String} First string
|
9 | * @param {String} Second string
|
10 | * @return {Number} -1 if first string lower than second one
|
11 | * 0 if first string same order as second one
|
12 | * 1 if first string greater than second one
|
13 | */
|
14 | export const ignoreCase = (a, b) => {
|
15 | let x = a.toLowerCase();
|
16 | let y = b.toLowerCase();
|
17 | return x < y ? -1 : (x > y ? 1 : 0);
|
18 | };
|
19 |
|
20 | /**
|
21 | * Compare function for sorting passed numbers in ascending manner
|
22 | * @param {Number} First number
|
23 | * @param {Number} Second number
|
24 | * @return {Number} Negative, zero or positive number
|
25 | */
|
26 | export const numSortAsc = (a, b) => (a - b);
|
27 |
|
28 | /**
|
29 | * Compare function for sorting passed numbers in descending manner
|
30 | * @param {Number} First number
|
31 | * @param {Number} Second number
|
32 | * @return {Number} Negative, zero or positive number
|
33 | */
|
34 | export const numSortDesc = (a, b) => (b - a);
|
35 |
|
36 | /**
|
37 | * Compare function for sorting passed dates in ascending manner according to
|
38 | * the corresponding UTC numeric value (returned by getTime)
|
39 | * @param {Date} First date object
|
40 | * @param {Date} Second date object
|
41 | * @return {Number} Negative, zero or positive number
|
42 | */
|
43 | export const dateSortAsc = (date1, date2) => date1.getTime() - date2.getTime();
|
44 |
|
45 | /**
|
46 | * Compare function for sorting passed dates in descending manner according to
|
47 | * the corresponding UTC numeric value (returned by getTime)
|
48 | * @param {Date} First date object
|
49 | * @param {Date} Second date object
|
50 | * @return {Number} Negative, zero or positive number
|
51 | */
|
52 | export const dateSortDesc = (date1, date2) => date2.getTime() - date1.getTime();
|
53 |
|
54 | /**
|
55 | * Curried compare function for sorting passed formatted numbers in desired
|
56 | * fashion according to supplied compare function and decimal separator
|
57 | * @param {Function} Compare function
|
58 | * @param {String} [decimal=','] Decimal separator
|
59 | * @return {Function} Compare function receiving parsed numeric arguments
|
60 | */
|
61 | export const sortNumberStr = (compareFn, decimal = ',') => {
|
62 | return (numStr1, numStr2) => {
|
63 | let num1 = parseNb(numStr1, decimal);
|
64 | let num2 = parseNb(numStr2, decimal);
|
65 | return compareFn(num1, num2);
|
66 | };
|
67 | };
|
68 |
|
69 | /**
|
70 | * Curried compare function for sorting passed formatted dates in desired
|
71 | * fashion according to supplied compare function and locale
|
72 | * @param {Function} Compare function
|
73 | * @param {String} [locale='en-us'] Locale code
|
74 | * @return {Function} Compare function receiving parsed date arguments
|
75 | */
|
76 | export const sortDateStr = (compareFn, locale = 'en-us') => {
|
77 | return (dateStr1, dateStr2) => {
|
78 | let date1 = SugarDate.create(dateStr1, locale);
|
79 | let date2 = SugarDate.create(dateStr2, locale);
|
80 | return compareFn(date1, date2);
|
81 | };
|
82 | };
|