Skip to content

Commit

Permalink
[#108,#229][Crum/Search] Nit: Slightly Simplify Result Storage.
Browse files Browse the repository at this point in the history
Also add some TODO's about our future plans.
  • Loading branch information
pishoyg committed Sep 6, 2024
1 parent f9aa6c1 commit 1ce032f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
8 changes: 1 addition & 7 deletions site/data/build/xooxle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ const resultList = document.getElementById('resultList');
const fullWordCheckbox = document.getElementById('fullWordCheckbox');
const regexCheckbox = document.getElementById('regexCheckbox');
class Result {
constructor(path, title, text) {
this.path = path;
this.title = title;
this.text = text;
}
match(query, fullWord, useRegex) {
if (!useRegex) {
query = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
Expand Down Expand Up @@ -57,8 +52,7 @@ const fileMap = (async function () {
catch {
resp = new Response('[{"path": "1.html", "title": "ⲟⲩⲱⲓⲛⲓ", "text": "light" }]');
}
const json = await resp.json().then((resp) => resp);
return Array.from(json).map((dict) => Object.assign(new Result('', '', ''), dict));
return (await resp.json()).map((obj) => Object.assign(new Result(), obj));
})();
let currentAbortController = null;
async function search() {
Expand Down
20 changes: 11 additions & 9 deletions site/xooxle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const fullWordCheckbox = document.getElementById('fullWordCheckbox') as HTMLInpu
const regexCheckbox = document.getElementById('regexCheckbox') as HTMLInputElement;

class Result {
constructor(
readonly path: string,
readonly title: string,
readonly text: string,
) { }
readonly path!: string;
readonly title!: string;
readonly text!: string;

// TODO: (#229) Find all matches, not just the first one.
// TODO: (#229) Return the matching text in context, not just the text on its
// own.
match(query: string, fullWord: boolean, useRegex: boolean): string | null {
if (!useRegex) {
// Escape all the special characters in the string, in order to search
Expand All @@ -27,6 +28,9 @@ class Result {
return null;
}
if (fullWord) {
// We already force matching to be restricted to full words, so there
// is nothing that we need to do expand our match to fall on word
// boundaries. This is already the case.
return match[0];
}
return this.getMatchFullWords(match[0]);
Expand Down Expand Up @@ -74,10 +78,8 @@ const fileMap: Promise<Result[]> = (async function(): Promise<Result[]> {
);
}

const json = await resp.json().then((resp) => resp as object[]);
return Array.from(json).map((dict: object) =>
Object.assign(new Result('', '', ''), dict),
);
return (await resp.json() as object[]).map(
(obj) => Object.assign(new Result(), obj));
})();

// Event listener for the search button.
Expand Down

0 comments on commit 1ce032f

Please sign in to comment.