@stryke/path
Version:
A package containing various utilities that expand the functionality of NodeJs's built-in `path` module
58 lines (56 loc) • 2.02 kB
JavaScript
import { cwd } from "./cwd.mjs";
import { slash } from "./slash.mjs";
import { isParentPath } from "./is-parent-path.mjs";
import { joinPaths } from "./join-paths.mjs";
//#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 = cwd(), options = {}) {
return slash(options.skipIfAlreadyParent !== false && isParentPath(childPath, parentPath) ? childPath : 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
export { append, appendExtension, appendPath };
//# sourceMappingURL=append.mjs.map