Skip to content

Commit

Permalink
Update Hex Rexp and Rewrite Recursive Function
Browse files Browse the repository at this point in the history
  • Loading branch information
NoBody-UU committed Aug 2, 2023
1 parent 88c4632 commit 466fba7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "taskwizard",
"version": "0.1.0",
"version": "0.1.1",
"description": "A versatile package containing useful code snippets for simple tasks",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
"dist"
],
"engines": {
"node": ">=10"
Expand Down
36 changes: 12 additions & 24 deletions src/lib/files.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { lstatSync, readdirSync } from "fs";
import { extname, join } from "path";
import { join } from "path";
import { readdir } from 'node:fs/promises';

/**
* Recursively search for files with specified extensions in a folder and its subfolders.
* Recursively search for files in a folder.
*
* @param {string} folderPath - The path to the folder where the search should start.
* @param {string[]} [allowedExtensions=[".js"]] - An array of allowed file extensions to include in the search. Default: [".js"]
* @param {string} path - The path to the folder where the search should start.
* @returns {string[]} - An array containing the absolute file paths of the found files.
*/
export function searchFilesRecursive(folderPath: string, allowedExtensions: string[] = [".js"]): string[] {
const filePaths: string[] = [];
const readFilesRecursively = (directory: string) => {
const files = readdirSync(join(process.cwd(), directory));
files.forEach((file) => {
const stat = lstatSync(join(process.cwd(), directory, file));
if (stat.isDirectory()) {
readFilesRecursively(join(directory, file));
} else {
const extension = extname(file);
if (!allowedExtensions.includes(extension)) return;
const filePath = join(process.cwd(), directory, file);
filePaths.push(`${filePath}`);
}
});
};
readFilesRecursively(folderPath);
return filePaths;
}
export async function searchFilesRecursive(path: string): Promise<string[]> {
const paths: string[] = [];
for (const file of (await readdir(path, { withFileTypes: true }))) {
const dir = join(path, file.name);
paths.push(...(file.isDirectory() ? await searchFilesRecursive(dir) : [dir]));
}
return paths
}
2 changes: 1 addition & 1 deletion src/lib/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @returns Returns true if the text is a valid hexadecimal color code, otherwise returns false.
*/
export function isHex(colorCode: string): boolean {
return /^#[0-9A-F]{6}$/i.test(colorCode);
return /^#?([a-f0-9]{6}|[a-f0-9]{3})$/.test(colorCode);
}

/**
Expand Down

0 comments on commit 466fba7

Please sign in to comment.