UNPKG

@stryke/path

Version:

A package containing various utilities that expand the functionality of NodeJs's built-in `path` module

45 lines (43 loc) 1.88 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_cwd = require('./cwd.cjs'); const require_slash = require('./slash.cjs'); const require_is_parent_path = require('./is-parent-path.cjs'); const require_file_path_fns = require('./file-path-fns.cjs'); //#region src/replace.ts /** * Replace the base path from the beginning of the given path. * * @example * ```ts * replacePath("/home/user/project/src/index.ts", "/home/user/project"); * // returns "src/index.ts" * ``` * * @param childPath - The child path to replace the {@link parentPath} substring from * @param parentPath - The parent path to remove from the {@link childPath} parameter * @returns The {@link childPath} with the {@link parentPath} path removed */ function replacePath(childPath, parentPath = require_cwd.cwd()) { return require_is_parent_path.isParentPath(childPath, parentPath) ? require_slash.slash(childPath).replace(require_slash.slash(parentPath), "").replace(/^\//, "") : childPath; } /** * Replace the extension of a given path with the provided value. * * @example * ```ts * replaceExtension("/home/user/project/src/index.ts", ".js"); * // returns "/home/user/project/src/index.js" * replaceExtension("/home/user/project/src/index.ts"); * // returns "/home/user/project/src/index" * ``` * * @param path - The path that will have its current extension replaced * @param replacement - The value (or an empty string) to replace the current extension with * @returns The path with the replaced extension */ function replaceExtension(path, replacement = "", options) { return path.replace(!replacement || replacement.includes(".") ? require_file_path_fns.findFileDotExtensionSafe(path, options) : require_file_path_fns.findFileExtensionSafe(path, options), replacement); } //#endregion exports.replaceExtension = replaceExtension; exports.replacePath = replacePath;