UNPKG

file-url

Version:

Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`

26 lines (19 loc) 661 B
import path from 'path'; export default function fileUrl(filePath, options = {}) { if (typeof filePath !== 'string') { throw new TypeError(`Expected a string, got ${typeof filePath}`); } const {resolve = true} = options; let pathName = filePath; if (resolve) { pathName = path.resolve(filePath); } pathName = pathName.replace(/\\/g, '/'); // Windows drive letter must be prefixed with a slash. if (pathName[0] !== '/') { pathName = `/${pathName}`; } // Escape required characters for path components. // See: https://tools.ietf.org/html/rfc3986#section-3.3 return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent); }