@flystorage/file-storage
Version:
File-storage abstraction: multiple filesystems, one API.
36 lines (35 loc) • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathPrefixer = void 0;
const node_path_1 = require("node:path");
class PathPrefixer {
separator;
joinFunc;
prefix = '';
constructor(prefix = '', separator = '/', joinFunc = node_path_1.posix.join) {
this.separator = separator;
this.joinFunc = joinFunc;
if (prefix.length > 0) {
this.prefix = this.joinFunc(prefix, this.separator);
}
}
prefixFilePath(path) {
return this.prefix.length > 0 ? this.joinFunc(this.prefix, path) : path;
}
prefixDirectoryPath(path) {
let fullPath = this.prefix.length > 0
? this.joinFunc(this.prefix, path)
: path;
if (fullPath.length > 0 && !fullPath.endsWith(this.separator)) {
fullPath = `${fullPath}${this.separator}`;
}
return fullPath;
}
stripFilePath(path) {
return path.substring(this.prefix.length);
}
stripDirectoryPath(path) {
return this.stripFilePath(path).replace(/\/+$/g, '');
}
}
exports.PathPrefixer = PathPrefixer;