keeweb/app/scripts/util/data/comparators.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

const LastChar = String.fromCharCode(0xfffd);
2015-10-17 23:49:24 +02:00
2019-08-16 23:05:39 +02:00
const ciCompare =
window.Intl && window.Intl.Collator && !/Edge/.test(navigator.userAgent) // bugged in Edge: #808
? new Intl.Collator(undefined, { sensitivity: 'base' }).compare
: (x, y) => x.toLocaleLowerCase().localeCompare(y.toLocaleLowerCase());
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const Comparators = {
2019-08-18 10:17:09 +02:00
stringComparator(field, asc) {
2015-10-17 23:49:24 +02:00
if (asc) {
2019-08-16 23:05:39 +02:00
return function(x, y) {
return ciCompare(x[field] || LastChar, y[field] || LastChar);
};
2015-10-17 23:49:24 +02:00
} else {
2019-08-16 23:05:39 +02:00
return function(x, y) {
return ciCompare(y[field], x[field]);
};
2015-10-17 23:49:24 +02:00
}
},
2019-08-18 10:17:09 +02:00
rankComparator() {
2019-08-16 23:05:39 +02:00
return function(x, y) {
2019-08-18 16:14:47 +02:00
return y.getRank(this.filter) - x.getRank(this.filter);
2019-08-16 23:05:39 +02:00
};
},
2019-08-18 10:17:09 +02:00
dateComparator(field, asc) {
2015-10-17 23:49:24 +02:00
if (asc) {
2019-08-16 23:05:39 +02:00
return function(x, y) {
return x[field] - y[field];
};
2015-10-17 23:49:24 +02:00
} else {
2019-08-16 23:05:39 +02:00
return function(x, y) {
return y[field] - x[field];
};
2015-10-17 23:49:24 +02:00
}
}
};
2019-09-15 14:16:32 +02:00
export { Comparators };