Skip to content

Commit

Permalink
Regex Implementation (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaffa-Cakes authored Oct 17, 2023
1 parent abafdce commit 48eac3e
Showing 1 changed file with 141 additions and 13 deletions.
154 changes: 141 additions & 13 deletions src/WordsList/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,129 @@ export default async function refresh(
out.push(record);
});

function isRegex(value: string): boolean {
return value.startsWith("/") && value.endsWith("/");
}

function runRegex(regex: string, value: string): boolean {
const reg = new RegExp(regex.slice(1, -1));
return reg.test(value);
}

out = out.filter((record) => {
let colNum = 0;

if (columns.includes(Column.Id)) {
if (filters.Id !== undefined && isRegex(filters.Id)) {
if (!runRegex(filters.Id, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.English)) {
if (filters.English !== undefined && isRegex(filters.English)) {
if (!runRegex(filters.English, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.Language)) {
if (filters.Language !== undefined && isRegex(filters.Language)) {
if (!runRegex(filters.Language, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.Sonetic)) {
if (filters.Sonetic !== undefined && isRegex(filters.Sonetic)) {
if (!runRegex(filters.Sonetic, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.Notes)) {
if (filters.Notes !== undefined && isRegex(filters.Notes)) {
if (!runRegex(filters.Notes, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourceId)) {
if (filters.SourceId !== undefined && isRegex(filters.SourceId)) {
if (!runRegex(filters.SourceId, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourceName)) {
if (filters.SourceName !== undefined && isRegex(filters.SourceName)) {
if (!runRegex(filters.SourceName, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourceFileName)) {
if (
filters.SourceFileName !== undefined &&
isRegex(filters.SourceFileName)
) {
if (!runRegex(filters.SourceFileName, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourceReference)) {
if (
filters.SourceReference !== undefined &&
isRegex(filters.SourceReference)
) {
if (!runRegex(filters.SourceReference, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourcePublicationType)) {
if (
filters.SourcePublicationType !== undefined &&
isRegex(filters.SourcePublicationType)
) {
if (!runRegex(filters.SourcePublicationType, record[colNum]))
return false;
}
colNum++;
}

if (columns.includes(Column.SourceDocumentType)) {
if (
filters.SourceDocumentType !== undefined &&
isRegex(filters.SourceDocumentType)
) {
if (!runRegex(filters.SourceDocumentType, record[colNum]))
return false;
}
colNum++;
}

if (columns.includes(Column.SourceLocation)) {
if (
filters.SourceLocation !== undefined &&
isRegex(filters.SourceLocation)
) {
if (!runRegex(filters.SourceLocation, record[colNum])) return false;
}
colNum++;
}

if (columns.includes(Column.SourceNotes)) {
if (filters.SourceNotes !== undefined && isRegex(filters.SourceNotes)) {
if (!runRegex(filters.SourceNotes, record[colNum])) return false;
}
colNum++;
}

return true;
});

return out;
}

Expand Down Expand Up @@ -323,51 +446,56 @@ function generateWhere(columns: Column[], filters: Filters): Where {

let select: Where = {};

function isRegex(value: string): boolean {
return value.startsWith("/") && value.endsWith("/");
}

columns.forEach((column) => {

switch (column) {
case Column.Id:
if (filters.Id !== undefined) select.id = parseInt(filters.Id);
if (filters.Id !== undefined && !isRegex(filters.Id)) select.id = parseInt(filters.Id);
break;
case Column.English:
if (filters.English !== undefined) {
if (filters.English !== undefined && !isRegex(filters.English)) {
select.english = {
contains: filters.English,
mode: "insensitive",
};
}
break;
case Column.Language:
if (filters.Language !== undefined) {
if (filters.Language !== undefined && !isRegex(filters.Language)) {
select.language = {
contains: filters.Language,
mode: "insensitive",
};
}
break;
case Column.Sonetic:
if (filters.Sonetic !== undefined) {
if (filters.Sonetic !== undefined && !isRegex(filters.Sonetic)) {
select.sonetic = {
contains: filters.Sonetic,
mode: "insensitive",
};
}
break;
case Column.Notes:
if (filters.Notes !== undefined) {
if (filters.Notes !== undefined && !isRegex(filters.Notes)) {
select.notes = {
contains: filters.Notes,
mode: "insensitive",
};
}
break;
case Column.SourceId:
if (filters.SourceId !== undefined) {
if (filters.SourceId !== undefined && !isRegex(filters.SourceId)) {
select = ensureSourceExists(select);
select.source!.id = parseInt(filters.SourceId);
}
break;
case Column.SourceName:
if (filters.SourceName !== undefined) {
if (filters.SourceName !== undefined && !isRegex(filters.SourceName)) {
select = ensureSourceExists(select);
select.source!.name = {
contains: filters.SourceName,
Expand All @@ -376,7 +504,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourceFileName:
if (filters.SourceFileName !== undefined) {
if (filters.SourceFileName !== undefined && !isRegex(filters.SourceFileName)) {
select = ensureSourceExists(select);
select.source!.fileName = {
contains: filters.SourceFileName,
Expand All @@ -385,7 +513,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourceReference:
if (filters.SourceReference !== undefined) {
if (filters.SourceReference !== undefined && !isRegex(filters.SourceReference)) {
select = ensureSourceExists(select);
select.source!.reference = {
contains: filters.SourceReference,
Expand All @@ -394,7 +522,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourcePublicationType:
if (filters.SourcePublicationType !== undefined) {
if (filters.SourcePublicationType !== undefined && !isRegex(filters.SourcePublicationType)) {
select = ensureSourceExists(select);
select.source!.publicationType = {
contains: filters.SourcePublicationType,
Expand All @@ -403,7 +531,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourceDocumentType:
if (filters.SourceDocumentType !== undefined) {
if (filters.SourceDocumentType !== undefined && !isRegex(filters.SourceDocumentType)) {
select = ensureSourceExists(select);
select.source!.documentType = {
contains: filters.SourceDocumentType,
Expand All @@ -412,7 +540,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourceLocation:
if (filters.SourceLocation !== undefined) {
if (filters.SourceLocation !== undefined && !isRegex(filters.SourceLocation)) {
select = ensureSourceExists(select);
select.source!.location = {
contains: filters.SourceLocation,
Expand All @@ -421,7 +549,7 @@ function generateWhere(columns: Column[], filters: Filters): Where {
}
break;
case Column.SourceNotes:
if (filters.SourceNotes !== undefined) {
if (filters.SourceNotes !== undefined && !isRegex(filters.SourceNotes)) {
select = ensureSourceExists(select);
select.source!.notes = {
contains: filters.SourceNotes,
Expand Down

0 comments on commit 48eac3e

Please sign in to comment.