UNPKG

@jsenv/util

Version:

Set of functions often needed when using Node.js.

20 lines (16 loc) 567 B
import { urlToPathname } from "./urlToPathname.js" export const urlToExtension = (url) => { const pathname = urlToPathname(url) return pathnameToExtension(pathname) } const pathnameToExtension = (pathname) => { const slashLastIndex = pathname.lastIndexOf("/") if (slashLastIndex !== -1) { pathname = pathname.slice(slashLastIndex + 1) } const dotLastIndex = pathname.lastIndexOf(".") if (dotLastIndex === -1) return "" // if (dotLastIndex === pathname.length - 1) return "" const extension = pathname.slice(dotLastIndex) return extension }