1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Allows to find the lowest ranking index in an index
9 * of suggestions, by comparing it to another array of "most relevant" items
10 * which has been sorted by relevance.
13 * let sortedBrowsers = ["firefox", "safari", "edge", "chrome"];
14 * let myBrowsers = ["brave", "chrome", "firefox"];
15 * let bestBrowserIndex = findMostRelevantIndex(myBrowsers, sortedBrowsers);
16 * // returns "2", the index of firefox in myBrowsers array
18 * @param {Array} items
19 * Array of items to compare against sortedItems.
20 * @param {Array} sortedItems
21 * Array of sorted items that suggestions are evaluated against. Array
22 * should be sorted by relevance, most relevant item first.
25 function findMostRelevantIndex(items, sortedItems) {
26 if (!Array.isArray(items) || !Array.isArray(sortedItems)) {
27 throw new Error("Please provide valid items and sortedItems arrays.");
30 // If the items array is empty, no valid index can be found.
35 // Return 0 if no match was found in the suggestion list.
37 let lowestIndex = Infinity;
38 items.forEach((item, i) => {
39 const index = sortedItems.indexOf(item);
40 if (index !== -1 && index <= lowestIndex) {
50 * Top 100 CSS property names sorted by relevance, most relevant first.
52 * List based on the one used by Chrome devtools :
53 * https://code.google.com/p/chromium/codesearch#chromium/src/third_party/
54 * WebKit/Source/devtools/front_end/sdk/CSSMetadata.js&q=CSSMetadata&
55 * sq=package:chromium&type=cs&l=676
57 * The data is a mix of https://www.chromestatus.com/metrics/css and usage
58 * metrics from popular sites collected via https://gist.github.com/NV/3751436
62 const SORTED_CSS_PROPERTIES = [
94 "background-position",
135 "border-bottom-color",
136 "border-top-right-radius",
138 "border-top-left-radius",
139 "border-bottom-left-radius",
140 "border-bottom-right-radius",
143 "border-right-color",
146 "border-bottom-width",
147 "border-right-width",
166 * Helper to find the most relevant CSS property name in a provided array.
168 * @param items {Array}
169 * Array of CSS property names.
171 function findMostRelevantCssPropertyIndex(items) {
172 return findMostRelevantIndex(items, SORTED_CSS_PROPERTIES);
175 exports.findMostRelevantIndex = findMostRelevantIndex;
176 exports.findMostRelevantCssPropertyIndex = findMostRelevantCssPropertyIndex;