keeweb/app/scripts/util/comparators.js

30 lines
1.0 KiB
JavaScript
Raw Normal View History

const LastChar = String.fromCharCode(0xfffd);
2015-10-17 23:49:24 +02:00
2017-12-16 19:50:30 +01:00
const ciCompare = (window.Intl && window.Intl.Collator && !/Edge/.test(navigator.userAgent)) // bugged in Edge: #808
2016-07-17 13:30:38 +02:00
? 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 = {
2015-10-17 23:49:24 +02:00
stringComparator: function(field, asc) {
if (asc) {
return function (x, y) { return ciCompare(x[field] || LastChar, y[field] || LastChar); };
} else {
return function (x, y) { return ciCompare(y[field], x[field]); };
}
},
2019-02-05 19:08:06 +01:00
rankComparator: function() {
2019-03-08 08:01:39 +01:00
return function (x, y) { return y.getRank(this.filter.textLower) - x.getRank(this.filter.textLower); };
},
2015-10-17 23:49:24 +02:00
dateComparator: function(field, asc) {
if (asc) {
return function (x, y) { return x[field] - y[field]; };
} else {
return function (x, y) { return y[field] - x[field]; };
}
}
};
module.exports = Comparators;