ghoststools
Version:
A collection of functions (tools) I commonly use
62 lines (57 loc) • 2.09 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/keys.ts
var removeKeys = /* @__PURE__ */ __name((object, keys) => {
if (!Array.isArray(keys))
keys = [keys];
const entries = Object.entries(object);
return entries.reduce((obj, [k, v]) => {
if (keys.includes(k))
return obj;
else
return { ...obj, [k]: v };
}, {});
}, "removeKeys");
// src/cast.ts
var castToArray = /* @__PURE__ */ __name((item) => Array.isArray(item) ? item : [item], "castToArray");
// src/fs/files.ts
import { lstatSync, readdirSync, existsSync, statSync } from "fs";
import { resolve } from "path";
var readdirRecursive = /* @__PURE__ */ __name((cwd, options = {}) => {
const paths = [];
for (const raw of readdirSync(cwd)) {
const path = resolve(cwd, raw);
const stat = statSync(path);
if (options.filter && !options.filter(raw, path))
continue;
if (stat.isDirectory())
paths.push(...readdirRecursive(path, options));
else
paths.push(path);
}
return paths;
}, "readdirRecursive");
var flattenPaths = /* @__PURE__ */ __name((input, options = {}) => castToArray(input).map((path) => {
if (!existsSync(path))
throw new Error(`File ${path} does not exist`);
const isDirectory = lstatSync(path).isDirectory();
if (!isDirectory)
return path;
return readdirRecursive(path, options);
}).flat(), "flattenPaths");
// src/fs/helpers.ts
import { normalize, extname } from "path";
var posixify = /* @__PURE__ */ __name((path) => path.replace(/\\/g, "/"), "posixify");
var stripTrailingSlash = /* @__PURE__ */ __name((path) => path.replace(/\\+$/, "").replace(/\/+$/, ""), "stripTrailingSlash");
var fullNormalize = /* @__PURE__ */ __name((path) => stripTrailingSlash(normalize(path)), "fullNormalize");
var stripExt = /* @__PURE__ */ __name((path) => path.slice(0, -extname(path).length || void 0), "stripExt");
export {
castToArray,
flattenPaths,
fullNormalize,
posixify,
readdirRecursive,
removeKeys,
stripExt,
stripTrailingSlash
};