url-or-path
Advanced tools
44
index.js
@@ -1,37 +0,39 @@ | ||
import url from 'node:url' | ||
import {fileURLToPath, pathToFileURL} from 'node:url' | ||
const isUrl = (url) => url instanceof URL | ||
const isFileUrl = (url) => isUrl(url) && url.protocol === 'file:' | ||
const NONE_FILE_URL_ERROR_MESSAGE = 'Only `file:` URLs are supported.' | ||
const FILE_PATH_INVALID_ERROR_MESSAGE = 'File path should be a string or URL.' | ||
function verify(urlOrPath) { | ||
const isUrl = (value) => { | ||
try { | ||
const {protocol} = new URL(value) | ||
return !/^[a-z]:$/.test(protocol) | ||
} catch {} | ||
return false | ||
} | ||
function toUrl(urlOrPath) { | ||
let url | ||
if (isUrl(urlOrPath)) { | ||
if (!isFileUrl(urlOrPath)) { | ||
throw new TypeError(NONE_FILE_URL_ERROR_MESSAGE) | ||
} | ||
return true | ||
url = urlOrPath instanceof URL ? urlOrPath : new URL(urlOrPath) | ||
} else if (typeof urlOrPath === 'string') { | ||
url = pathToFileURL(urlOrPath) | ||
} | ||
if (typeof urlOrPath !== 'string') { | ||
if (!(url instanceof URL)) { | ||
throw new TypeError(FILE_PATH_INVALID_ERROR_MESSAGE) | ||
} | ||
return true | ||
if (url.protocol !== 'file:') { | ||
throw new TypeError(NONE_FILE_URL_ERROR_MESSAGE) | ||
} | ||
return url | ||
} | ||
function toPath(urlOrPath) { | ||
verify(urlOrPath) | ||
return isFileUrl(urlOrPath) ? url.fileURLToPath(urlOrPath) : urlOrPath | ||
return fileURLToPath(toUrl(urlOrPath)) | ||
} | ||
function toUrl(urlOrPath) { | ||
verify(urlOrPath) | ||
return isFileUrl(urlOrPath) ? urlOrPath : url.pathToFileURL(urlOrPath) | ||
} | ||
export {toUrl, toUrl as toURL, toPath} |
{ | ||
"name": "url-or-path", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Convert between file URL and path.", | ||
@@ -40,31 +40,30 @@ "homepage": "https://github.com/fisker/url-or-path#readme", | ||
"test-coverage": "c8 ava", | ||
"release": "run-s format test dist" | ||
"release": "run-s format test dist", | ||
"prepare": "husky install" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
}, | ||
"ava": { | ||
"verbose": true | ||
}, | ||
"c8": { | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "13.1.0", | ||
"@fisker/commitlint-config": "1.3.6", | ||
"@fisker/eslint-config": "7.2.0", | ||
"@fisker/eslint-config-ava": "2.0.2", | ||
"@fisker/husky-config": "4.1.0", | ||
"@fisker/lint-staged-config": "3.1.0", | ||
"@fisker/prettier-config": "4.1.1", | ||
"@fisker/eslint-config": "10.0.7", | ||
"@fisker/eslint-config-ava": "2.0.4", | ||
"@fisker/husky-config": "4.1.1", | ||
"@fisker/lint-staged-config": "3.1.4", | ||
"@fisker/prettier-config": "5.0.4", | ||
"ava": "3.15.0", | ||
"c8": "7.8.0", | ||
"cz-conventional-changelog": "3.3.0", | ||
"c8": "7.10.0", | ||
"del-cli": "4.0.1", | ||
"eslint": "7.32.0", | ||
"husky": "7.0.2", | ||
"lint-staged": "11.1.2", | ||
"markdownlint-cli": "0.28.1", | ||
"eslint": "8.5.0", | ||
"husky": "7.0.4", | ||
"lint-staged": "12.1.3", | ||
"markdownlint-cli": "0.30.0", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "2.3.2", | ||
"sort-package-json": "1.50.0" | ||
"prettier": "2.5.1", | ||
"sort-package-json": "1.53.1" | ||
}, | ||
@@ -74,9 +73,3 @@ "publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"c8": { | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
] | ||
} | ||
} |
15
-16.67%29
11.54%5612
-2.97%