Skip to content

Commit

Permalink
Merge pull request #692 from magieno/improve-file-module-with-find-in…
Browse files Browse the repository at this point in the history
…-files

- Added test for the regex
  • Loading branch information
etiennenoel authored Apr 15, 2024
2 parents 650d10a + a2268c5 commit d2ca569
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/file/src/enums/match-type.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Path = "PATH", // Means you're matching the full path such as "/home/user/results.jpeg"
}
45 changes: 44 additions & 1 deletion packages/file/src/managers/file.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,47 @@ describe("File Manager", () => {
expect(cursors[3].line).toBe(10);
expect(cursors[3].position).toBe(51);
})
})
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);
})
})
24 changes: 19 additions & 5 deletions packages/file/src/managers/file.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileCursorInterface[]> {
async findInFile(search: string | RegExp, filePath: string): Promise<FileCursorInterface[]> {
const fileStream = fs.createReadStream(filePath);

const rl = readline.createInterface({
Expand All @@ -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)
}
Expand Down Expand Up @@ -61,4 +75,4 @@ export class FileManager {
})
})
}
}
}

0 comments on commit d2ca569

Please sign in to comment.