@thi.ng/file-io
Version:
Assorted file I/O utils (w/ logging support) for NodeJS/Bun
22 lines (21 loc) • 616 B
JavaScript
import { isString } from "@thi.ng/checks/is-string";
let MASKS = [
[process.env.HOME ? new RegExp(process.env.HOME, "g") : /~/, "~"]
];
const addPathMask = (pattern, mask = "****") => {
MASKS.push([isString(pattern) ? new RegExp(pattern, "g") : pattern, mask]);
};
const setPathMasks = (masks) => MASKS = masks;
const maskedPath = (path) => {
for (let [re, mask] of MASKS) {
path = path.replace(re, mask);
}
return path;
};
const maskHomeDir = (path, home = process.env.HOME, mask = "~") => home ? path.replace(home, mask) : path;
export {
addPathMask,
maskHomeDir,
maskedPath,
setPathMasks
};