Skip to content

Commit

Permalink
Fixed major code smells (#9)
Browse files Browse the repository at this point in the history
* Fixed major code smells
* Added test for extractSegment
  • Loading branch information
petruki authored May 27, 2023
1 parent ff2f74f commit 093d9ef
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 56 deletions.
11 changes: 5 additions & 6 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export default class CacheHandler {
output.segment = output.segment.filter((segment) => {
if (ignoreCase) {
return segment.toLowerCase().indexOf(query.toLowerCase()) >= 0;
} else {
return segment.indexOf(query) >= 0;
}

return segment.indexOf(query) >= 0;
});

return output.segment.length;
});

Expand Down Expand Up @@ -116,9 +117,7 @@ export default class CacheHandler {
return this.cache.filter((storedData) => {
if (storedData.query.length <= query.length) {
if (ignoreCase) {
if (!query.toLowerCase().startsWith(storedData.query.toLowerCase())) {
return false;
}
return query.toLowerCase().startsWith(storedData.query.toLowerCase());
}

if (query.startsWith(storedData.query) && storedData.exp > Date.now()) {
Expand All @@ -134,7 +133,7 @@ export default class CacheHandler {
}

/**
* Verifies if options has been changed, if so it will gather the content from the source again
* Verifies if options has been changed, if so it will fetch the content from the source again
*
* @param storedData
* @param FetchOptions
Expand Down
19 changes: 11 additions & 8 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function validateQuery(query: string, limit?: number): void {
}

export function validateContext(context: Context): void {
if (!context || !context.url.length) {
if (!context?.url.length) {
throw new InvalidContext(
"context is not defined properly - use setContext() to define the context",
);
Expand Down Expand Up @@ -58,19 +58,19 @@ export function extractSegment(
let offset;
if (previewLength === -1) {
// Find the last line position
const lastPos = from.trimLeft().search(LINE_BREAK);
const lastPos = from.trimStart().search(LINE_BREAK);
offset = from.substring(
0,
lastPos > 0 ? lastPos + 1 : from.search(LAST_LINE),
);
} else {
if (trimContent) {
offset = from.trimLeft().substring(
offset = from.trimStart().substring(
0,
from.trimLeft().indexOf(LINE_BREAK) + previewLength,
from.trimStart().indexOf(LINE_BREAK) + previewLength,
);
} else {
offset = from.trimLeft().substring(
offset = from.trimStart().substring(
0,
previewLength === 0 ? query.length : previewLength,
);
Expand All @@ -83,9 +83,11 @@ export function extractSegment(
/* Extract content segment from the found query to the last complete line,
* However, if the previewLenght is shorter than the size of this line, it will display the established range.
*/
return (trimContent
? offset.substring(0, lastLine > 0 ? lastLine : offset.length)
: offset).trim();
if (trimContent) {
return offset.substring(0, lastLine > 0 ? lastLine : offset.length).trim();
}

return offset.trim();
}

/**
Expand Down Expand Up @@ -116,5 +118,6 @@ export function findFirstPos(
}
return firstPos;
}

return foundIndex;
}
9 changes: 5 additions & 4 deletions src/skimming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ export class Skimming {
}

if (!results.length) {
for (let i = 0; i < this.context.files.length; i++) {
const element = this.context.files[i];
const content = await this.readDocument(this.context.url, element);
for (const file of this.context.files) {
const content = await this.readDocument(this.context.url, file);

const segment = this.skimContent(
content,
Expand All @@ -74,7 +73,7 @@ export class Skimming {

if (segment.length) {
const output = {
file: element,
file,
segment,
found: segment.length,
cache: false,
Expand Down Expand Up @@ -162,6 +161,8 @@ export class Skimming {
});
}
}

result.body?.cancel();
throw new NotContentFound(url, doc);
}
}
Loading

0 comments on commit 093d9ef

Please sign in to comment.