UNPKG

@cpany/utils

Version:
72 lines (68 loc) 1.57 kB
// src/guard.ts function isUndef(object) { return object === void 0 || object === null; } function isDef(object) { return object !== void 0 && object !== null; } // src/array.ts function uniq(array) { return Array.from(new Set(array)); } function filterMap(array, fn) { return array.map(fn).filter((u) => isDef(u)); } // src/fs.ts import { promises } from "fs"; import path from "path"; async function* listJsonFiles(dir) { try { if (dir.endsWith(".json")) { const files = JSON.parse(await promises.readFile(dir, "utf8")); if (Array.isArray(files)) { for (const contest of files) { yield contest; } } else { yield files; } } else { const dirents = await promises.readdir(dir, { withFileTypes: true }); for (const dirent of dirents) { const id = path.join(dir, dirent.name); yield* listJsonFiles(id); } } } catch { } } async function* listFiles(dir, skipList = /* @__PURE__ */ new Set()) { try { const dirents = await promises.readdir(dir, { withFileTypes: true }); for (const dirent of dirents) { const id = path.join(dir, dirent.name); if (dirent.name.startsWith(".") || skipList.has(id)) { continue; } if (dirent.isDirectory()) { yield* listFiles(id, skipList); } else { yield id; } } } catch { } } // src/path.ts function slash(path2) { return path2.replace(/\\/g, "/"); } export { filterMap, isDef, isUndef, listFiles, listJsonFiles, slash, uniq };