@ou-imdt/utils
Version:
Utility library for interactive media development
20 lines (19 loc) • 878 B
JavaScript
/**
* Splits a file path into its directory name, file name, extension, and parameters.
* @param {string} path - The path string to parse.
* @returns {{dirname: string, filename: string, extension: string, params: string}} An object containing the path's components:
* - `dirname`: The directory name, including the trailing slash.
* - `filename`: The file name without the extension.
* - `extension`: The file extension, including the leading dot.
* - `params`: The URL parameters or fragment, including the leading symbol.
* @see https://gist.github.com/nopjia/e94b5f822744b60cd106
*/
export default function splitPath(path) {
const result = path.replace(/\\/g, '/').match(/(.*\/)?(\..*?|.*?)(\.[^.]*?)?(#.*$|\?.*$|$)/);
return {
dirname: result[1] ?? '',
filename: result[2] ?? '',
extension: result[3] ?? '',
params: result[4] ?? ''
};
}