Skip to content

Instantly share code, notes, and snippets.

@fabien
Created November 19, 2015 21:30
Show Gist options
  • Save fabien/092fb747a196e681c2c0 to your computer and use it in GitHub Desktop.
Save fabien/092fb747a196e681c2c0 to your computer and use it in GitHub Desktop.
Sortkey collation using ilib
var _ = require('lodash');
var ilib = require('ilib');
var Locale = require('ilib/lib/Locale.js');
var Collator = require('ilib/lib/Collator.js');
var defaults = { locale: 'en', precision: 5 };
module.exports = {};
module.exports.ilib = ilib;
module.exports.Locale = Locale;
module.exports.Collator = Collator;
module.exports.defaults = defaults;
// See: http://docs.jedlsoft.com/ilib/jsdoc/index.html
module.exports.getAvailableLocales = function(callback) {
if (_.isFunction(callback)) {
Locale.getAvailableLocales(false, callback);
} else {
return Locale.getAvailableLocales();
}
};
module.exports.getAvailableStyles = function(locale) {
return Collator.getAvailableStyles(locale);
};
module.exports.getCollator = function(options) {
if (_.isString(options)) options = { locale: options };
return new Collator(_.defaults(options || {}, defaults));
};
Collator.prototype.hexSortKey = function(str, precision) {
str = String(str);
precision = precision || defaults.precision; // -1 = unlimited
if (precision > 0) str = str.substr(0, precision);
return new Buffer(this.sortKey(str), 'binary').toString('hex');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment