new-url
v1.0.0
Published
Simple function to convert strings to urls
Readme
newURL
Simple function to convert strings to urls.
Given a string returns a valid URL.
Mostly used to fix urls without protocol, like "example.com".
No need for regexes, using only the native URL constructor.
Install
npm install new-urlUsage
import { newURL } from "new-url";- Turn
"example.com"into"https://example.com/":
const url = newURL("example.com").href; // "https://example.com/"- Works with any string:
const url = newURL("example").href; // "https://example/"const url = newURL(
"ftp://user:[email protected]:8000/path/name?foo=bar&bar=foo#hash"
);
url.href; // "https://example.com/"
url.hash; // "#hash"
url.host; // "www.example.com:8000"
url.hostname; // "www.example.com"
url.href; // "ftp://user:[email protected]:8000/path/name?foo=bar&bar=foo#hash"
url.origin; // "ftp://www.example.com:8000"
url.password; // "password"
url.pathname; // "/path/name"
url.port; // "8000"
url.protocol; // "ftp:"
url.search; // "?foo=bar&bar=foo"
url.username; // "user"See the test file for more examples.
Ideas
- [ ] Add protocol as parameter, ex:
newURL("example.com", "ftp").href; // "ftp://example.com/"
