buda-base / tibetan-sort-js

Tibetan unicode string comparison library for JavaScript
MIT License
4 stars 0 forks source link

use native ICU collation when available #27

Open eroux opened 1 year ago

eroux commented 1 year ago

there should be a small test at init time to see if the ICU collator works for Tibetan, and then use that instead of the JS code if available. This code seems to indicate that it works in modern browsers:

var tibCollator = new Intl.Collator('bo');
var tibSortedArray = ["ང", "རྔ", "ལྔ", "སྔ", "བརྔ", "བསྔ", "ཅ"];
var tibRandomArray = ["ལྔ", "ང", "ཅ", "རྔ", "སྔ", "བརྔ", "བསྔ"];
var tibResultArray = ["ལྔ", "ང", "ཅ", "རྔ", "སྔ", "བརྔ", "བསྔ"];
tibResultArray.sort(tibCollator.compare);
var resPrint = "";
if (JSON.stringify(tibSortedArray)==JSON.stringify(tibResultArray)) {
  resPrint = "ok!";
} else {
  resPrint = "error: "+JSON.stringify(tibRandomArray)+" has been sorted as "+JSON.stringify(tibResultArray)+", should have been "+JSON.stringify(tibSortedArray);
}
console.log(resPrint);

see https://www.bdrc.io/blog/2021/10/29/sorting-out-tibetan-alphabetical-order/

eroux commented 1 year ago

(kind of) blocked by https://bugs.chromium.org/p/chromium/issues/detail?id=1425662