Skip to content

Commit

Permalink
Made various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
YewoMhango committed Oct 22, 2021
1 parent 8eecadc commit cd4f41b
Show file tree
Hide file tree
Showing 11 changed files with 1,261 additions and 878 deletions.
59 changes: 43 additions & 16 deletions handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const bookNames = [

const bookNamesLowerCase = bookNames.map((s) => s.toLowerCase());

function handleChapterTextRequest([_, book, chapter]) {
function handleChapterTextRequest({ book, chapter, language }) {
chapter = Number(chapter);
book = book.toLowerCase();

Expand All @@ -49,7 +49,7 @@ function handleChapterTextRequest([_, book, chapter]) {
}
}

function handleChapterJsonRequest([_, book, chapter]) {
function handleChapterJsonRequest({ book, chapter, language }) {
chapter = Number(chapter);
book = book.toLowerCase();

Expand Down Expand Up @@ -85,16 +85,16 @@ function renderChapterAsHTMLWithItalics(book, chapter) {
return html;
}

function handleSearchRequest([_, type, language, searchString]) {
function handleSearchRequest(query) {
const supportedSearchTypes = {
exact: exactMatchSearch,
standard: standardSearch,
};

if (Object.keys(supportedSearchTypes).includes(type.toLowerCase())) {
if (Object.keys(supportedSearchTypes).includes(query.type.toLowerCase())) {
return {
type: "search",
data: supportedSearchTypes[type.toLowerCase()](searchString, language),
data: supportedSearchTypes[query.type.toLowerCase()](query),
};
} else {
return { type: "search", data: "Search method not supported" };
Expand All @@ -105,16 +105,29 @@ const max_search_results = 500;

/**
*
* @param {String} searchString
* @param {String} langauge
* @param {String} searchText
* @param {String} language
*/
function standardSearch(searchString, langauge) {
function standardSearch({ language, searchText, from, to }) {
let searchResults = [];
let keywords = searchString.split(" ").map((s) => s.toLowerCase());
const keywords = searchText.split(" ").map((s) => s.toLowerCase());

if (
bookNamesLowerCase.indexOf(from.toLowerCase()) >
bookNamesLowerCase.indexOf(to.toLowerCase())
) {
[from, to] = [to, from];
}

const fromIdx = Math.max(bookNamesLowerCase.indexOf(from.toLowerCase()), 0);
const toIdx = Math.min(
bookNamesLowerCase.indexOf(to.toLowerCase()) + 1,
all_verses.length
);

for (
let i = 0;
i < all_verses.length && searchResults.length < max_search_results;
let i = fromIdx;
i < toIdx && searchResults.length < max_search_results;
i++
) {
let currentBook = all_verses[i];
Expand Down Expand Up @@ -150,13 +163,26 @@ function standardSearch(searchString, langauge) {
return searchResults;
}

function exactMatchSearch(searchString) {
function exactMatchSearch({ language, searchText, from, to }) {
let searchResults = [];
searchString = searchString.toLowerCase();
searchText = searchText.toLowerCase();

if (
bookNamesLowerCase.indexOf(from.toLowerCase()) >
bookNamesLowerCase.indexOf(to.toLowerCase())
) {
[from, to] = [to, from];
}

const fromIdx = Math.max(bookNamesLowerCase.indexOf(from.toLowerCase()), 0);
const toIdx = Math.min(
bookNamesLowerCase.indexOf(to.toLowerCase()) + 1,
all_verses.length
);

for (
let i = 0;
i < all_verses.length && searchResults.length < max_search_results;
let i = fromIdx;
i < toIdx && searchResults.length < max_search_results;
i++
) {
let currentBook = all_verses[i];
Expand All @@ -175,7 +201,8 @@ function exactMatchSearch(searchString) {
currentChapter[k]
.toLowerCase()
.replace(/\[|\]/g, "")
.includes(searchString)
.includes(searchText) ||
currentChapter[k].toLowerCase().includes(searchText)
) {
searchResults.push({
book: bookNames[i],
Expand Down
26 changes: 9 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,23 @@ app.on("ready", () => {
app.quit();
});

ipcMain.on("request", (e, requestPath) => {
// console.log(requestPath);
win.webContents.send("response", requestHandler("/" + requestPath));
ipcMain.on("request", (e, request) => {
// console.log(request);
win.webContents.send("response", requestHandler(request));
});
});

const handlers = require("./handlers");

function requestHandler(request) {
let result;

if (
(result = /^\/English\/([\d\w ]+)\/Chapters\/(\d{1,3}).txt$/i.exec(request))
) {
if (request.type == "chapterText") {
// console.log("Being handled as chapter text request...");
return handlers.handleChapterTextRequest(result);
} else if (
(result = /^\/English\/([\d\w ]+)\/Chapters\/(\d{1,3}).json$/i.exec(
request
))
) {
return handlers.handleChapterTextRequest(request.details);
} else if (request.type == "chapterJson") {
// console.log("Being handled as chapter json request...");
return handlers.handleChapterJsonRequest(result);
} else if ((result = /^\/search\/(\w+)\/(\w+)\/(.+)$/i.exec(request))) {
return handlers.handleChapterJsonRequest(request.details);
} else if (request.type == "search") {
// console.log("Being handled as search request...");
return handlers.handleSearchRequest(result);
return handlers.handleSearchRequest(request.details);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "MyLamp",
"version": "1.0.0",
"description": "Desktop Bible app built with electron",
"description": "Desktop Bible app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
Loading

0 comments on commit cd4f41b

Please sign in to comment.