UNPKG

@stryke/path

Version:

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

60 lines (58 loc) 2.21 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_join_paths = require('./join-paths.cjs'); //#region src/append.ts /** * If not already a parent path, append the base path from the beginning of the given child path. * * @example * ```ts * appendPath("src/index.ts", "/home/user/project"); * // returns "/home/user/project/src/index.ts" * * appendPath("/user/dev/app.ts", "/user/dev"); * // returns "/user/dev/app.ts" * * appendPath("docs/readme.md"); * // returns "<current_working_directory>/docs/readme.md" * * appendPath("src/index.ts", "/home/user/project", { skipIfAlreadyParent: false }); * // returns "/home/user/project/src/index.ts" * * appendPath("/home/user/project/src/index.ts", "/home/user/project", { skipIfAlreadyParent: false }); * // returns "/home/user/project/src/index.ts" * ``` * * @param childPath - The child path to append to the {@link parentPath} * @param parentPath - The parent path to add the {@link childPath} to * @param options - Options for appending the path * @returns The {@link parentPath} with the {@link childPath} appended */ function appendPath(childPath, parentPath = require_cwd.cwd(), options = {}) { return require_slash.slash(options.skipIfAlreadyParent !== false && require_is_parent_path.isParentPath(childPath, parentPath) ? childPath : require_join_paths.joinPaths(parentPath, childPath)); } const append = appendPath; /** * Append the extension to the given path. * * @example * ```ts * appendExtension("/home/user/project/src/index", ".ts"); * // returns "/home/user/project/src/index.ts" * appendExtension("/home/user/project/src/index.ts", ".js"); * // returns "/home/user/project/src/index.ts.js" * ``` * * @param path - The path to append the extension to. * @param extension - The extension to append. * @returns The path with the appended extension. */ function appendExtension(path, extension) { return `${path}.${extension.replace(/^\./, "")}`; } //#endregion exports.append = append; exports.appendExtension = appendExtension; exports.appendPath = appendPath;