search-helpers.mjs (739B)
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/. */ 4 5 /** 6 * Escape special characters for regular expressions from a string. 7 * 8 * @param {string} string 9 * The string to sanitize. 10 * @returns {string} The sanitized string. 11 */ 12 export function escapeRegExp(string) { 13 return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); 14 } 15 16 /** 17 * Search a tab list for items that match the given query. 18 */ 19 export function searchTabList(query, tabList) { 20 const regex = RegExp(escapeRegExp(query), "i"); 21 return tabList.filter( 22 ({ title, url }) => regex.test(title) || regex.test(url) 23 ); 24 }