UNPKG

universal-common

Version:

Library that provides useful missing base class library functionality.

35 lines (34 loc) 1.46 kB
/** * Utility class providing methods for working with file paths. * Currently contains only the getExtension method but could be expanded with * additional path manipulation functionality. */ export default class Path { /** * Extracts the file extension from a path, including the leading dot. * * @param {string} path - The file path to extract the extension from * @returns {string} The file extension with leading dot (e.g., ".txt"), or an empty string if no extension * * @example * Path.getExtension("file.txt") // Returns ".txt" * Path.getExtension("path/to/file") // Returns "" * Path.getExtension(".gitignore") // Returns ".gitignore" (special case for dotfiles) * Path.getExtension("path/file.js") // Returns ".js" * * @description * This method has a few notable behaviors: * 1. It includes the leading dot in the returned extension * 2. It treats dotfiles (files starting with a dot) as their own extension * 3. It returns an empty string for paths with no extension */ static getExtension(path) { let extensionWithoutDot = path.slice((path.lastIndexOf(".") - 1 >>> 0) + 2); if (extensionWithoutDot === "" && path[0] === ".") { return path; } else { return extensionWithoutDot === "" ? extensionWithoutDot : "." + extensionWithoutDot; } } }