Perf: avoid string comparision

This commit is contained in:
SukkaW
2024-12-22 00:54:08 +08:00
parent 57aeb267f6
commit af04018be9
3 changed files with 15 additions and 16 deletions

View File

@@ -1,20 +1,26 @@
import fsp from 'node:fs/promises';
import { sep } from 'node:path';
interface TreeFileType {
type: 'file',
// eslint-disable-next-line sukka/no-export-const-enum -- TODO: fix this in the future
export const enum TreeFileType {
FILE = 1,
DIRECTORY = 2
}
interface TreeFile {
type: TreeFileType.FILE,
name: string,
path: string
}
interface TreeDirectoryType {
type: 'directory',
type: TreeFileType.DIRECTORY,
name: string,
path: string,
children: TreeTypeArray
}
export type TreeType = TreeDirectoryType | TreeFileType;
export type TreeType = TreeDirectoryType | TreeFile;
export type TreeTypeArray = TreeType[];
type VoidOrVoidArray = void | VoidOrVoidArray[];
@@ -35,7 +41,7 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
if (child.isDirectory()) {
const newNode: TreeDirectoryType = {
type: 'directory',
type: TreeFileType.DIRECTORY,
name: child.name,
path: childRelativeToRoot,
children: []
@@ -45,8 +51,8 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
continue;
}
if (child.isFile()) {
const newNode: TreeFileType = {
type: 'file',
const newNode: TreeFile = {
type: TreeFileType.FILE,
name: child.name,
path: childRelativeToRoot
};