Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for Bindi characters alongwith the normal words #281

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions api/lib/searchOperators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const lodash = require('lodash');
const anvaad = require('anvaad-js');

// defining this as an object was the only way I could access
// AsteriskMariadbTranslation, and AsteriskAsciiValue in the firstLetterStartToQuery function..
const constantsObj = {
Expand All @@ -8,6 +10,27 @@ const constantsObj = {
DecSearchOperators: [43, 45, 42, 34, 39],
};

// Pairing the bindi characters with their non-bind counterparts
const bindiCharsUni = {
ਸ: 'ਸ਼',
ਖ: 'ਖ਼',
ਗ: 'ਗ਼',
ਜ: 'ਜ਼',
ਫ: 'ਫ਼',
};

// Generating an object with the ASCII codes of the bindi characters
const bindiCharacters = Object.entries(bindiCharsUni).reduce((acc, [key, value]) => {
const char = anvaad.unicode(key, true);
const asciiCode = anvaad.ascii(char).replaceAll(',', '');

const charWithBindi = anvaad.unicode(value, true);
const asciiCodeWithBindi = anvaad.ascii(charWithBindi).replaceAll(',', '');

acc[asciiCode] = asciiCodeWithBindi;
return acc;
}, {});

const replaceAsterisksAndQuotes = str => {
let res = str;

Expand Down Expand Up @@ -69,6 +92,38 @@ const getQueryConditionsAndParams = (
};
};

const hasBindiCharacter = charCode => {
if (bindiCharacters[charCode]) {
return bindiCharacters[charCode];
}
return false;
};

const generateBindiQuery = (charCodeQuery, charCodeQueryWildcard, result) => {
let bindiCharQuery = charCodeQuery;
let bindiCharQueryWildcard = charCodeQueryWildcard;
const updatedResult = result;

charCodeQuery.split(',').forEach(charCode => {
const bindiCharCode = hasBindiCharacter(charCode);
if (bindiCharCode) {
bindiCharQuery = bindiCharQuery.replaceAll(charCode, bindiCharCode);
bindiCharQueryWildcard = bindiCharQueryWildcard.replaceAll(charCode, bindiCharCode);
}
});

if (charCodeQuery !== bindiCharQuery) {
updatedResult.condition = `${updatedResult.condition} OR ${updatedResult.condition}`;
updatedResult.parameters = [
...updatedResult.parameters,
bindiCharQuery,
bindiCharQueryWildcard,
];
}

return updatedResult;
};

module.exports = {
AsteriskAsciiValue: constantsObj.AsteriskAsciiValue,
AsteriskMariadbTranslation: constantsObj.AsteriskMariadbTranslation,
Expand Down Expand Up @@ -100,10 +155,12 @@ module.exports = {
parameters: [modifiedSearchQuery],
};
}
return {
condition: 'v.FirstLetterStr BETWEEN ? AND ?',
const result = {
columns: ' LEFT JOIN tokenized_firstletters t ON t.verseid = v.ID',
condition: 't.token BETWEEN ? AND ?',
parameters: [charCodeQuery, charCodeQueryWildcard],
};
return generateBindiQuery(charCodeQuery, charCodeQueryWildcard, result);
},
firstLetterAnywhereToQuery: (charCodeQuery, charCodeQueryWildcard) => {
if (constantsObj.SearchOperators.some(operator => charCodeQuery.includes(operator))) {
Expand Down Expand Up @@ -132,11 +189,12 @@ module.exports = {
parameters: [modifiedSearchQuery],
};
}
return {
const result = {
columns: ' LEFT JOIN tokenized_firstletters t ON t.verseid = v.ID',
condition: 't.token BETWEEN ? AND ?',
parameters: [charCodeQuery, charCodeQueryWildcard],
};
return generateBindiQuery(charCodeQuery, charCodeQueryWildcard, result);
},
fullWordRomanizedToQuery: searchQuery => {
if (constantsObj.SearchOperators.some(operator => searchQuery.includes(operator))) {
Expand Down
Loading