rustdoc-search: use ES6 Map for Result
instead of Object
This commit is contained in:
parent
8642c96a33
commit
53f499d475
2 changed files with 40 additions and 21 deletions
|
@ -65,6 +65,11 @@ let Row;
|
||||||
*/
|
*/
|
||||||
let ResultsTable;
|
let ResultsTable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Map<String, ResultObject>}
|
||||||
|
*/
|
||||||
|
let Results;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {{
|
* @typedef {{
|
||||||
* desc: string,
|
* desc: string,
|
||||||
|
@ -80,7 +85,7 @@ let ResultsTable;
|
||||||
* ty: number,
|
* ty: number,
|
||||||
* }}
|
* }}
|
||||||
*/
|
*/
|
||||||
let Results;
|
let ResultObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.
|
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.
|
||||||
|
|
|
@ -903,8 +903,16 @@ function initSearch(rawSearchIndex) {
|
||||||
* @return {ResultsTable}
|
* @return {ResultsTable}
|
||||||
*/
|
*/
|
||||||
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
|
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
|
||||||
const results_others = {}, results_in_args = {}, results_returned = {};
|
const results_others = new Map(), results_in_args = new Map(),
|
||||||
|
results_returned = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add extra data to result objects, and filter items that have been
|
||||||
|
* marked for removal.
|
||||||
|
*
|
||||||
|
* @param {[ResultObject]} results
|
||||||
|
* @returns {[ResultObject]}
|
||||||
|
*/
|
||||||
function transformResults(results) {
|
function transformResults(results) {
|
||||||
const duplicates = new Set();
|
const duplicates = new Set();
|
||||||
const out = [];
|
const out = [];
|
||||||
|
@ -934,24 +942,30 @@ function initSearch(rawSearchIndex) {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function takes a result map, and sorts it by various criteria, including edit
|
||||||
|
* distance, substring match, and the crate it comes from.
|
||||||
|
*
|
||||||
|
* @param {Results} results
|
||||||
|
* @param {boolean} isType
|
||||||
|
* @param {string} preferredCrate
|
||||||
|
* @returns {[ResultObject]}
|
||||||
|
*/
|
||||||
function sortResults(results, isType, preferredCrate) {
|
function sortResults(results, isType, preferredCrate) {
|
||||||
const userQuery = parsedQuery.userQuery;
|
|
||||||
const ar = [];
|
|
||||||
for (const entry in results) {
|
|
||||||
if (hasOwnPropertyRustdoc(results, entry)) {
|
|
||||||
const result = results[entry];
|
|
||||||
result.word = searchWords[result.id];
|
|
||||||
result.item = searchIndex[result.id] || {};
|
|
||||||
ar.push(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
results = ar;
|
|
||||||
// if there are no results then return to default and fail
|
// if there are no results then return to default and fail
|
||||||
if (results.length === 0) {
|
if (results.size === 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
results.sort((aaa, bbb) => {
|
const userQuery = parsedQuery.userQuery;
|
||||||
|
const result_list = [];
|
||||||
|
for (const result of results.values()) {
|
||||||
|
result.word = searchWords[result.id];
|
||||||
|
result.item = searchIndex[result.id] || {};
|
||||||
|
result_list.push(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
result_list.sort((aaa, bbb) => {
|
||||||
let a, b;
|
let a, b;
|
||||||
|
|
||||||
// sort by exact match with regard to the last word (mismatch goes later)
|
// sort by exact match with regard to the last word (mismatch goes later)
|
||||||
|
@ -1060,7 +1074,7 @@ function initSearch(rawSearchIndex) {
|
||||||
nameSplit = hasPath ? null : parsedQuery.elems[0].path;
|
nameSplit = hasPath ? null : parsedQuery.elems[0].path;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const result of results) {
|
for (const result of result_list) {
|
||||||
// this validation does not make sense when searching by types
|
// this validation does not make sense when searching by types
|
||||||
if (result.dontValidate) {
|
if (result.dontValidate) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -1073,7 +1087,7 @@ function initSearch(rawSearchIndex) {
|
||||||
result.id = -1;
|
result.id = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return transformResults(results);
|
return transformResults(result_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1487,19 +1501,19 @@ function initSearch(rawSearchIndex) {
|
||||||
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
|
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
|
||||||
const inBounds = dist <= maxEditDistance || index !== -1;
|
const inBounds = dist <= maxEditDistance || index !== -1;
|
||||||
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
|
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
|
||||||
if (results[fullId] !== undefined) {
|
if (results.has(fullId)) {
|
||||||
const result = results[fullId];
|
const result = results.get(fullId);
|
||||||
if (result.dontValidate || result.dist <= dist) {
|
if (result.dontValidate || result.dist <= dist) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results[fullId] = {
|
results.set(fullId, {
|
||||||
id: id,
|
id: id,
|
||||||
index: index,
|
index: index,
|
||||||
dontValidate: parsedQuery.literalSearch,
|
dontValidate: parsedQuery.literalSearch,
|
||||||
dist: dist,
|
dist: dist,
|
||||||
path_dist: path_dist,
|
path_dist: path_dist,
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue