split-filename-regex.mjs
split-filename-regex.mjs
split-filename-regex.mjs
/**
* Split a file name into its name and extension.
*
* // splitFileNameRegex(fileName: string): { name: string; extension: string | undefined }
* @param {string} fileName The file name to split.
* @returns {Object} An object with the file name and its extension.
* @returns {string} name The name of the file.
* @returns {string | undefined} extension The extension of the file, or undefined if there is no extension.
*/
export default function splitFileNameRegex(fileName) {
const match = fileName.match(/^(.*?)(?:\.([^.]+))?$/);
if (!match) {
return { name: fileName, extension: undefined };
}
const [, name, extension] = match;
return {
name: name || fileName,
extension: extension?.toLowerCase(),
};
}
mjs