mirror of
https://github.com/Jermolene/TiddlyWiki5.git
synced 2026-01-20 19:41:32 -08:00
Update makeCompareFunction to support locale
This commit is contained in:
parent
3ea816556e
commit
0ebca08036
1 changed files with 6 additions and 12 deletions
|
|
@ -1020,12 +1020,12 @@ exports.stringifyNumber = function(num) {
|
|||
return num + "";
|
||||
};
|
||||
|
||||
exports.makeCompareFunction = function(type,options) {
|
||||
options = options || {};
|
||||
exports.makeCompareFunction = function(type,options = {}) {
|
||||
// set isCaseSensitive to true if not defined in options
|
||||
var isCaseSensitive = (options.isCaseSensitive === false) ? false : true,
|
||||
gt = options.invert ? -1 : +1,
|
||||
lt = options.invert ? +1 : -1,
|
||||
locale = exports.checkLanguageCode(options.locale) ? options.locale : undefined,
|
||||
compare = function(a,b) {
|
||||
if(a > b) {
|
||||
return gt ;
|
||||
|
|
@ -1043,11 +1043,8 @@ exports.makeCompareFunction = function(type,options) {
|
|||
return compare($tw.utils.parseInt(a),$tw.utils.parseInt(b));
|
||||
},
|
||||
"string": function(a,b) {
|
||||
if(!isCaseSensitive) {
|
||||
a = a.toLowerCase();
|
||||
b = b.toLowerCase();
|
||||
}
|
||||
return compare("" + a,"" + b);
|
||||
const sorter = Intl.Collator(locale, { sensitivity: isCaseSensitive ? "variant" : "accent" });
|
||||
return options.invert ? sorter.compare(b.toString(), a.toString()) : sorter.compare(a.toString(), b.toString());
|
||||
},
|
||||
"date": function(a,b) {
|
||||
var dateA = $tw.utils.parseDate(a),
|
||||
|
|
@ -1064,11 +1061,8 @@ exports.makeCompareFunction = function(type,options) {
|
|||
return compare($tw.utils.compareVersions(a,b),0);
|
||||
},
|
||||
"alphanumeric": function(a,b) {
|
||||
if(!isCaseSensitive) {
|
||||
a = a.toLowerCase();
|
||||
b = b.toLowerCase();
|
||||
}
|
||||
return options.invert ? b.localeCompare(a,undefined,{numeric: true,sensitivity: "base"}) : a.localeCompare(b,undefined,{numeric: true,sensitivity: "base"});
|
||||
const sorter = Intl.Collator(locale, {numeric: true, sensitivity: isCaseSensitive ? "case" : "base"});
|
||||
return options.invert ? sorter.compare(b, a) : sorter.compare(a, b);
|
||||
}
|
||||
};
|
||||
return (types[type] || types[options.defaultType] || types.number);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue