UNPKG

@flystorage/file-storage

Version:

File-storage abstraction: multiple filesystems, one API.

32 lines (31 loc) 976 B
import { posix } from 'node:path'; export class PathPrefixer { separator; joinFunc; prefix = ''; constructor(prefix = '', separator = '/', joinFunc = 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, ''); } }