util.js: Refactor util.strcmp into util.make_strcmp.

This commit is contained in:
Joshua Pan
2017-06-03 14:55:52 -07:00
committed by showell
parent 2bf48e58aa
commit ccd880094e
2 changed files with 11 additions and 2 deletions

View File

@@ -135,6 +135,14 @@ var _ = global._;
} }
}()); }());
(function test_dumb_strcmp() {
Intl.Collator = undefined;
var strcmp = util.make_strcmp();
assert.equal(strcmp('a', 'b'), -1);
assert.equal(strcmp('c', 'c'), 0);
assert.equal(strcmp('z', 'y'), 1);
}());
(function test_is_mobile() { (function test_is_mobile() {
global.window.navigator = { userAgent: "Android" }; global.window.navigator = { userAgent: "Android" };
assert(util.is_mobile()); assert(util.is_mobile());

View File

@@ -138,7 +138,7 @@ exports.rtrim = function (str) {
// doesn't support the ECMAScript Internationalization API // doesn't support the ECMAScript Internationalization API
// Specification, do a dumb string comparison because // Specification, do a dumb string comparison because
// String.localeCompare is really slow. // String.localeCompare is really slow.
exports.strcmp = (function () { exports.make_strcmp = function () {
try { try {
var collator = new Intl.Collator(); var collator = new Intl.Collator();
return collator.compare; return collator.compare;
@@ -149,7 +149,8 @@ exports.strcmp = (function () {
return function util_strcmp(a, b) { return function util_strcmp(a, b) {
return (a < b ? -1 : (a > b ? 1 : 0)); return (a < b ? -1 : (a > b ? 1 : 0));
}; };
}()); };
exports.strcmp = exports.make_strcmp();
exports.escape_regexp = function (string) { exports.escape_regexp = function (string) {
// code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions // code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions