@cpany/utils
Version:
CPany utils package
115 lines (109 loc) • 3.42 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
filterMap: () => filterMap,
isDef: () => isDef,
isUndef: () => isUndef,
listFiles: () => listFiles,
listJsonFiles: () => listJsonFiles,
slash: () => slash,
uniq: () => uniq
});
module.exports = __toCommonJS(src_exports);
// 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
var import_fs = require("fs");
var import_path = __toESM(require("path"));
async function* listJsonFiles(dir) {
try {
if (dir.endsWith(".json")) {
const files = JSON.parse(await import_fs.promises.readFile(dir, "utf8"));
if (Array.isArray(files)) {
for (const contest of files) {
yield contest;
}
} else {
yield files;
}
} else {
const dirents = await import_fs.promises.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const id = import_path.default.join(dir, dirent.name);
yield* listJsonFiles(id);
}
}
} catch {
}
}
async function* listFiles(dir, skipList = /* @__PURE__ */ new Set()) {
try {
const dirents = await import_fs.promises.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const id = import_path.default.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, "/");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
filterMap,
isDef,
isUndef,
listFiles,
listJsonFiles,
slash,
uniq
});