From 77cac75c8f76481faa06a944e42d6c18059c7556 Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:05:37 -0400 Subject: [PATCH 1/3] Optimize docs/_assets/js files --- docs/_assets/js/api-search.js | 76 +++++++++++++---------------------- docs/_assets/js/sidebar.js | 2 +- docs/_assets/js/toolbar.js | 44 +++++++++++--------- 3 files changed, 53 insertions(+), 69 deletions(-) diff --git a/docs/_assets/js/api-search.js b/docs/_assets/js/api-search.js index 4950d2067ffe..bcb7b2e17c0b 100644 --- a/docs/_assets/js/api-search.js +++ b/docs/_assets/js/api-search.js @@ -28,66 +28,44 @@ * } * ``` */ -onmessage = function(e) { - var docs = e.data.docs; - var searchTerm = e.data.search; - - var regexForTerm = function(query) { - var escaped = query.replace(/([\.\*\+\?\|\(\)\[\]\\])/g, '\\$1'); - if (query.toLowerCase() != query) { - // Regexp that matches CamelCase subbits: "BiSe" is - // "[a-z]*Bi[a-z]*Se" and matches "BitSet", "ABitSet", ... - return new RegExp(escaped.replace(/([A-Z])/g,"[a-z]*$1")); - } - else { // if query is all lower case make a normal case insensitive search - return new RegExp(escaped, "i"); - } +onmessage = function({ data: { docs, search } }) { + const regexForTerm = (query) => { + const escaped = query.replace(/([\.\*\+\?\|\(\)\[\]\\])/g, '\\$1'); + return query.toLowerCase() !== query + ? new RegExp(escaped.replace(/([A-Z])/g, "[a-z]*$1")) + : new RegExp(escaped, "i"); }; - var searchRegex = regexForTerm(searchTerm); + const searchRegex = regexForTerm(search); - var filterPackages = function(entity) { - switch(entity.kind) { - case "val": - case "def": - case "type": - case "package": - return false; - default: - return true; - } - }; + const filterPackages = (entity) => !["val", "def", "type", "package"].includes(entity.kind); + + const messageParentIfMatches = (parent) => (entity) => { + const fullName = entity.path.join('.'); - // look at this higher order function, such syntax: - var messageParentIfMatches = function(parent) { - return function(entity) { - var fullName = entity.path.join('.'); + if (searchRegex.test(fullName)) { + postMessage({ + type: "entityResult", + package: parent, + entity + }); + } - if (searchRegex.test(fullName)) { + entity.members.forEach((member) => { + if (searchRegex.test(member.name)) { postMessage({ - "type": "entityResult", - "package": parent, - "entity": entity + type: "memberResult", + package: parent, + parent: entity, + member }); } - - var searchChild = function(member) { - if (searchRegex.test(member.name)) { - postMessage({ - "type": "memberResult", - "package": parent, - "parent": entity, - "member": member, - }); - } - }; - entity.members.forEach(searchChild); - }; + }); }; - docs.forEach(function(pack) { + docs.forEach((pack) => { pack.members .filter(filterPackages) .forEach(messageParentIfMatches(pack)); }); -} +}; \ No newline at end of file diff --git a/docs/_assets/js/sidebar.js b/docs/_assets/js/sidebar.js index aa377ed8aa0e..2832486c1d6a 100644 --- a/docs/_assets/js/sidebar.js +++ b/docs/_assets/js/sidebar.js @@ -2,5 +2,5 @@ function toggleSection(titleElement) { const title = $(titleElement); title.siblings("ul").toggleClass("toggled"); - title.children("i.fas").toggleClass("fa-angle-right").toggleClass("fa-angle-down"); + title.children("i.fas").toggleClass("fa-angle-right fa-angle-down"); } diff --git a/docs/_assets/js/toolbar.js b/docs/_assets/js/toolbar.js index be132e7db4a9..8a1ca80c9dcb 100644 --- a/docs/_assets/js/toolbar.js +++ b/docs/_assets/js/toolbar.js @@ -1,20 +1,26 @@ -$(document).ready(function() { - $("#menu-icon").click(() => { - $(".sidebar").toggleClass("toggled"); - }) - $("#search-icon").click(() => { - $("#searchbar").toggleClass("shown"); - $("#search-api-input").focus(); - }) - const searchInput = $("#search-api-input"); - searchInput.keydown(evt => { - if (evt.which == 13) { - const baseUrl = $("#baseurl-input").val(); - window.location = ( - baseUrl + "/api/search.html?" + - "searchTerm=" + searchInput.val() + - "&previousUrl=" + encodeURI(window.location) - ); +$(function() { + const menuIcon = $("#menu-icon"); + const sidebar = $(".sidebar"); + menuIcon.on("click", function() { + sidebar.toggleClass("toggled"); + }); + + const searchIcon = $("#search-icon"); + const searchbar = $("#searchbar"); + const searchApiInput = $("#search-api-input"); + searchIcon.on("click", function() { + searchbar.toggleClass("shown"); + searchApiInput.focus(); + }); + + const baseurlInput = $("#baseurl-input"); + searchApiInput.keydown(function(evt) { + if (evt.which === 13) { // Enter + const baseUrl = baseurlInput.val(); + const searchTerm = searchApiInput.val(); + const previousUrl = encodeURI(window.location); + const searchUrl = `${baseUrl}/api/search.html?searchTerm=${searchTerm}&previousUrl=${previousUrl}`; + window.location = searchUrl; } - }) -}) + }); +}); From 5ffed6de8c42128a8d7cc4048c4995633f65b373 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:10:33 -0400 Subject: [PATCH 2/3] Add Lambda --- docs/_assets/js/toolbar.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/_assets/js/toolbar.js b/docs/_assets/js/toolbar.js index 8a1ca80c9dcb..a799ca661dd9 100644 --- a/docs/_assets/js/toolbar.js +++ b/docs/_assets/js/toolbar.js @@ -1,20 +1,20 @@ $(function() { const menuIcon = $("#menu-icon"); const sidebar = $(".sidebar"); - menuIcon.on("click", function() { + menuIcon.on("click", () => { sidebar.toggleClass("toggled"); }); const searchIcon = $("#search-icon"); const searchbar = $("#searchbar"); const searchApiInput = $("#search-api-input"); - searchIcon.on("click", function() { + searchIcon.on("click", () => { searchbar.toggleClass("shown"); searchApiInput.focus(); }); const baseurlInput = $("#baseurl-input"); - searchApiInput.keydown(function(evt) { + searchApiInput.keydown(evt => { if (evt.which === 13) { // Enter const baseUrl = baseurlInput.val(); const searchTerm = searchApiInput.val(); From 9ad34f98df95660d634e29988ae19a1f4311b957 Mon Sep 17 00:00:00 2001 From: Aviv Keller <38299977+RedYetiDev@users.noreply.github.com> Date: Wed, 3 Apr 2024 12:47:43 -0400 Subject: [PATCH 3/3] Add comments w/ if/then --- docs/_assets/js/api-search.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/_assets/js/api-search.js b/docs/_assets/js/api-search.js index bcb7b2e17c0b..03981350e81b 100644 --- a/docs/_assets/js/api-search.js +++ b/docs/_assets/js/api-search.js @@ -31,9 +31,13 @@ onmessage = function({ data: { docs, search } }) { const regexForTerm = (query) => { const escaped = query.replace(/([\.\*\+\?\|\(\)\[\]\\])/g, '\\$1'); - return query.toLowerCase() !== query - ? new RegExp(escaped.replace(/([A-Z])/g, "[a-z]*$1")) - : new RegExp(escaped, "i"); + if (query.toLowerCase() != query) { + // Regexp that matches CamelCase subbits: "BiSe" is + // "[a-z]*Bi[a-z]*Se" and matches "BitSet", "ABitSet", ... + return new RegExp(escaped.replace(/([A-Z])/g,"[a-z]*$1")); + } + // if query is all lower case make a normal case insensitive search + return new RegExp(escaped, "i"); }; const searchRegex = regexForTerm(search); @@ -68,4 +72,4 @@ onmessage = function({ data: { docs, search } }) { .filter(filterPackages) .forEach(messageParentIfMatches(pack)); }); -}; \ No newline at end of file +};