settings: Extract sort helpers for various lists.

Giving these functions a name and moving them to
the top-level scope has a couple tactical advantages:

    - names show in tracebacks
    - code is less indented
    - setup code is less cluttered
    - will be easier to add unit tests
    - will make some upcoming diffs nicer

These are technically more `compare_foo` than `sort_foo`,
but we already had a naming convention that was sort of
in place.
This commit is contained in:
Steve Howell
2020-04-12 10:13:47 +00:00
committed by Tim Abbott
parent 4f23f13c55
commit a06d455228
6 changed files with 81 additions and 70 deletions

View File

@@ -14,6 +14,23 @@ exports.maybe_disable_widgets = function () {
}
};
function compare_by_index(a, b, i) {
if (a[i] > b[i]) {
return 1;
} else if (a[i] === b[i]) {
return 0;
}
return -1;
}
function sort_pattern(a, b) {
return compare_by_index(a, b, 0);
}
function sort_url(a, b) {
return compare_by_index(a, b, 1);
}
exports.populate_filters = function (filters_data) {
if (!meta.loaded) {
return;
@@ -45,22 +62,8 @@ exports.populate_filters = function (filters_data) {
parent_container: $("#filter-settings").expectOne(),
}).init();
function compare_by_index(a, b, i) {
if (a[i] > b[i]) {
return 1;
} else if (a[i] === b[i]) {
return 0;
}
return -1;
}
filters_list.add_sort_function("pattern", function (a, b) {
return compare_by_index(a, b, 0);
});
filters_list.add_sort_function("url", function (a, b) {
return compare_by_index(a, b, 1);
});
filters_list.add_sort_function("pattern", sort_pattern);
filters_list.add_sort_function("url", sort_url);
const active_col = $('.admin_filters_table th.active').expectOne();
filters_list.sort(