diff --git a/packages/file/src/enums/match-type.enum.ts b/packages/file/src/enums/match-type.enum.ts index 4aa77ed4..df3e9b49 100644 --- a/packages/file/src/enums/match-type.enum.ts +++ b/packages/file/src/enums/match-type.enum.ts @@ -2,5 +2,5 @@ export enum MatchTypeEnum { Base = "BASE", // Means you're matching the base filename without the extension and without the path such as "results" Extension = "EXT", // Means you're matching file extensions (without leading ".") such as "jpeg" Filename = "FILE_NAME", // Means you're matching the base filename and extension such as "results.jpeg" - Path = "PATH", -} \ No newline at end of file + Path = "PATH", // Means you're matching the full path such as "/home/user/results.jpeg" +} diff --git a/packages/file/src/managers/file.manager.spec.ts b/packages/file/src/managers/file.manager.spec.ts index a429bfc9..5cbfb1e3 100644 --- a/packages/file/src/managers/file.manager.spec.ts +++ b/packages/file/src/managers/file.manager.spec.ts @@ -19,4 +19,47 @@ describe("File Manager", () => { expect(cursors[3].line).toBe(10); expect(cursors[3].position).toBe(51); }) -}) \ No newline at end of file + it("should properly find the elements in the text when a regex is passed", async () => { + const fileManager = new FileManager(); + + const cursors = await fileManager.findInFile(/(eleifend)|(pulvinar)/gi, "test-files/lorem_ipsum.txt"); + + expect(cursors.length).toBe(9); + + // eleifend + expect(cursors[0].line).toBe(2); + expect(cursors[0].position).toBe(416); + + // pulvinar + expect(cursors[1].line).toBe(2); + expect(cursors[1].position).toBe(457); + + // pulvinar + expect(cursors[2].line).toBe(6); + expect(cursors[2].position).toBe(24); + + // eleifend + expect(cursors[3].line).toBe(6); + expect(cursors[3].position).toBe(33); + + // pulvinar + expect(cursors[4].line).toBe(8); + expect(cursors[4].position).toBe(213); + + // eleifend + expect(cursors[5].line).toBe(8); + expect(cursors[5].position).toBe(595); + + // pulvinar + expect(cursors[6].line).toBe(10); + expect(cursors[6].position).toBe(332); + + // pulvinar + expect(cursors[7].line).toBe(12); + expect(cursors[7].position).toBe(76); + + // pulvinar + expect(cursors[8].line).toBe(14); + expect(cursors[8].position).toBe(414); + }) +}) diff --git a/packages/file/src/managers/file.manager.ts b/packages/file/src/managers/file.manager.ts index b96bea69..afdc6120 100644 --- a/packages/file/src/managers/file.manager.ts +++ b/packages/file/src/managers/file.manager.ts @@ -3,10 +3,12 @@ import {injectable} from "tsyringe"; import {FileCursorInterface} from "../interfaces/file-cursor.interface"; import fs from "fs"; import * as readline from "readline"; +import {DirectoryListOptions} from "../options/directory-list.options"; +import {FileInfoInterface} from "../interfaces/file-info.interface"; @injectable() export class FileManager { - async findInFile(search: string, filePath: string): Promise { + async findInFile(search: string | RegExp, filePath: string): Promise { const fileStream = fs.createReadStream(filePath); const rl = readline.createInterface({ @@ -23,14 +25,26 @@ export class FileManager { lineIndex++; do { - position = line.indexOf(search, position); + if(typeof search === "string") { + position = line.indexOf(search, position); + } else { + const lineToSearch = line.substring(position); + const substrPosition = lineToSearch.search(search); + + if(substrPosition !== -1) { + // Need to add the new position to the current position else we will return the wrong position + position += substrPosition; + } else { + position = -1; + } + } // If the search string is found, simply add its position if(position !== -1) { fileCursors.push({line: lineIndex, position, lineText: line}) - // Move the position by the length of the search term. - position += search.length; + // Increment the position to continue the search. + position++; } } while (position != -1 || position >= line.length) } @@ -61,4 +75,4 @@ export class FileManager { }) }) } -} \ No newline at end of file +}