Utils: Renamed sortBy() to sort(). It now works without the 2nd argument.

This commit is contained in:
satyr 2010-06-17 08:18:47 +09:00
Родитель 590eea2851
Коммит 75b1028433
5 изменённых файлов: 32 добавлений и 24 удалений

Просмотреть файл

@ -118,7 +118,7 @@ function changeExternalCallSettings() {
function loadSkinList() {
var {skins, currentSkin} = skinService;
var $list = $("#skin-list").empty(), id = -1;
for each (let skin in Utils.sortBy(skins, function(s) s.uri.spec))
for each (let skin in Utils.sort(skins, function (s) s.uri.spec))
$list.append(createSkinElement(skin, ++id, skin === currentSkin));
if (currentSkin === skinService.customSkin) openSkinEditor();
}

Просмотреть файл

@ -207,7 +207,7 @@ function maybeRemindCommand(document, commandsByDomain) {
if (reminderPeriod > 1 &&
Utils.history.visitsToDomain(domain) % reminderPeriod) return;
var cmd = Utils.sortBy(commands, Math.random)[0];
var cmd = Utils.sort(commands, Math.random)[0];
new SuggestionMemory("main_parser").getScore("", cmd.id) ||
showEnabledCommandNotification(document, cmd.name);
}

Просмотреть файл

@ -155,7 +155,7 @@ ParserQuery.prototype = {
sugg._verb.cmd.id);
sugg.frequencyMatchScore = pow(.1, 1 / (freq + 1));
}
Utils.sortBy(this._suggestionList, "score", "v");
Utils.sort(this._suggestionList, "score", true);
},
};
@ -261,9 +261,9 @@ Parser.prototype = {
_sortGenericVerbCache: function P__sortGenericVerbCache() {
var suggMemory = this._suggestionMemory;
if (!suggMemory) return;
Utils.sortBy(
Utils.sort(
this._rankedVerbsThatUseGenericNouns,
function minusSMScore(v) -suggMemory.getScore("", v.cmd.id));
function bySMScore(v) suggMemory.getScore("", v.cmd.id), true);
},
};

Просмотреть файл

@ -653,30 +653,38 @@ function parseHtml(htmlText, callback) {
// **//Deprecated.//** Use native {{{trim()}}} instead.
Utils.trim = String.trim;
// === {{{ Utils.sortBy(array, key, descending = false) }}} ===
// Sorts an array by specified {{{key}}} and returns it. e.g.:
// === {{{ Utils.sort(array, key, descending = false) }}} ===
// Sorts an {{{array}}} without implicit string conversion and returns it,
// optionally performing Schwartzian Transformation
// by specified {{{key}}}. e.g.:
// {{{
// sortBy(["abc", "d", "ef"], "length") //=> ["d", "ef", "abc"]
// sortBy([1, 2, 3], function (x) -x) //=> [3, 2, 1]
// [42, 16, 7].sort() //=> [16, 42, 7]
// sort([42, 16, 7]) //=> [7, 16, 42]
// sort(["abc", "d", "ef"], "length") //=> ["d", "ef", "abc"]
// sort([1, 2, 3], function (x) -x) //=> [3, 2, 1]
// }}}
//
// {{{array}}} is the target array.
//
// {{{key}}} is either a string specifying the key property,
// {{{key}}} is an optional string specifying the key property
// or a function that maps each of {{{array}}}'s item to a sort key.
//
// Sorts descending if {{{descending}}}.
function sortBy(array, key, descending) {
var pluck = typeof key === "function" ? key : function pluck(x) x[key];
var sortee = ([{key: pluck(v), val: v} for each (v in array)]
.sort(descending ? sortBy.dSorter : sortBy.aSorter));
for (let i in sortee) array[i] = sortee[i].val;
function sort(array, key, descending) {
array.forEach(function transform(v, i, a) a[i] = {key: this(v), val: v},
typeof key === "function" ? key :
(key != null
? function pry(x) x[key]
: function idt(x) x));
// Because our Monkey uses Merge Sort, "swap the values if plus" works.
array.sort(descending
? function dsc(a, b) a.key < b.key
: function asc(a, b) a.key > b.key);
array.forEach(function mrofsnart(v, i, a) a[i] = v.val);
return array;
}
// Because our Monkey uses Merge Sort, "swap the values if plus" works.
sortBy.aSorter = function byKeyAsc(a, b) a.key > b.key;
sortBy.dSorter = function byKeyDsc(a, b) a.key < b.key;
Utils.sortBy = Utils.sort;
// === {{{ Utils.uniq(array, key, strict = false) }}} ===
// Removes duplicates from an array by comparing string versions of them

Просмотреть файл

@ -279,19 +279,19 @@ function testCmdManagerCatchesExceptionsInCmds() {
}
}
function testUtilsSortBy() {
function testUtilsSort() {
var strArray = ["abc", "d", "ef", "ghij", "klm", "nop", "qrstuvw", "xyz"];
this.assertEquals(strArray.slice().sort() + "",
Utils.sortBy(strArray.slice(), String) + "");
Utils.sort(strArray.slice()) + "");
this.assertEquals(strArray.slice().sort().reverse() + "",
Utils.sortBy(strArray.slice(), String, true) + "");
Utils.sort(strArray.slice(), String, true) + "");
this.assertEquals(
strArray.slice().sort(function(a, b) a.length - b.length) + "",
Utils.sortBy(strArray.slice(), "length") + "");
Utils.sort(strArray.slice(), "length") + "");
// (-2|-1|0|1|2) x 99
var numArray = [(Math.random() * 5 | 0) - 2 for each(i in Array(99) + 0)];
var numArray = [(Math.random() * 5 | 0) - 2 for (i in Utils.seq(99))];
this.assertEquals(numArray.slice().sort(function (a, b) b - a) + "",
Utils.sortBy(numArray, function (x) -x) + "");
Utils.sort(numArray, function (x) -x) + "");
}
function testUtilsUniq() {