@@ -903,8 +903,16 @@ function initSearch(rawSearchIndex) {
903
903
* @return {ResultsTable}
904
904
*/
905
905
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
906
- const results_others = {}, results_in_args = {}, results_returned = {};
906
+ const results_others = new Map(), results_in_args = new Map(),
907
+ results_returned = new Map();
907
908
909
+ /**
910
+ * Add extra data to result objects, and filter items that have been
911
+ * marked for removal.
912
+ *
913
+ * @param {[ResultObject]} results
914
+ * @returns {[ResultObject]}
915
+ */
908
916
function transformResults(results) {
909
917
const duplicates = new Set();
910
918
const out = [];
@@ -934,24 +942,30 @@ function initSearch(rawSearchIndex) {
934
942
return out;
935
943
}
936
944
945
+ /**
946
+ * This function takes a result map, and sorts it by various criteria, including edit
947
+ * distance, substring match, and the crate it comes from.
948
+ *
949
+ * @param {Results} results
950
+ * @param {boolean} isType
951
+ * @param {string} preferredCrate
952
+ * @returns {[ResultObject]}
953
+ */
937
954
function sortResults(results, isType, preferredCrate) {
938
- const userQuery = parsedQuery.userQuery;
939
- const ar = [];
940
- for (const entry in results) {
941
- if (hasOwnPropertyRustdoc(results, entry)) {
942
- const result = results[entry];
943
- result.word = searchWords[result.id];
944
- result.item = searchIndex[result.id] || {};
945
- ar.push(result);
946
- }
947
- }
948
- results = ar;
949
955
// if there are no results then return to default and fail
950
- if (results.length === 0) {
956
+ if (results.size === 0) {
951
957
return [];
952
958
}
953
959
954
- results.sort((aaa, bbb) => {
960
+ const userQuery = parsedQuery.userQuery;
961
+ const result_list = [];
962
+ for (const result of results.values()) {
963
+ result.word = searchWords[result.id];
964
+ result.item = searchIndex[result.id] || {};
965
+ result_list.push(result);
966
+ }
967
+
968
+ result_list.sort((aaa, bbb) => {
955
969
let a, b;
956
970
957
971
// sort by exact match with regard to the last word (mismatch goes later)
@@ -1060,7 +1074,7 @@ function initSearch(rawSearchIndex) {
1060
1074
nameSplit = hasPath ? null : parsedQuery.elems[0].path;
1061
1075
}
1062
1076
1063
- for (const result of results ) {
1077
+ for (const result of result_list ) {
1064
1078
// this validation does not make sense when searching by types
1065
1079
if (result.dontValidate) {
1066
1080
continue;
@@ -1073,7 +1087,7 @@ function initSearch(rawSearchIndex) {
1073
1087
result.id = -1;
1074
1088
}
1075
1089
}
1076
- return transformResults(results );
1090
+ return transformResults(result_list );
1077
1091
}
1078
1092
1079
1093
/**
@@ -1487,19 +1501,19 @@ function initSearch(rawSearchIndex) {
1487
1501
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
1488
1502
const inBounds = dist <= maxEditDistance || index !== -1;
1489
1503
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
1490
- if (results[ fullId] !== undefined ) {
1491
- const result = results[ fullId] ;
1504
+ if (results.has( fullId) ) {
1505
+ const result = results.get( fullId) ;
1492
1506
if (result.dontValidate || result.dist <= dist) {
1493
1507
return;
1494
1508
}
1495
1509
}
1496
- results[ fullId] = {
1510
+ results.set( fullId, {
1497
1511
id: id,
1498
1512
index: index,
1499
1513
dontValidate: parsedQuery.literalSearch,
1500
1514
dist: dist,
1501
1515
path_dist: path_dist,
1502
- };
1516
+ }) ;
1503
1517
}
1504
1518
}
1505
1519
0 commit comments