diff --git a/README.md b/README.md index b818d551..b359560c 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,13 @@ Import: ```js // CommonJS -const { normalizeURL, joinURL } = require('ufo') +const { normalizeURL, joinURL } = require("ufo"); // ESM -import { normalizeURL, joinURL } from 'ufo' +import { normalizeURL, joinURL } from "ufo"; // Deno -import { parseURL } from 'https://unpkg.com/ufo/dist/index.mjs' +import { parseURL } from "https://unpkg.com/ufo/dist/index.mjs"; ``` **Notice:** You may need to transpile package and add URL polyfill for legacy environments @@ -41,61 +41,61 @@ import { parseURL } from 'https://unpkg.com/ufo/dist/index.mjs' - Preserves protocol/host if provided ```ts -normalizeURL('test?query=123 123#hash, test') +normalizeURL("test?query=123 123#hash, test"); // test?query=123%20123#hash,%20test -normalizeURL('http://localhost:3000') +normalizeURL("http://localhost:3000"); // http://localhost:3000/ ``` ### `joinURL` ```ts -joinURL('a', '/b', '/c') +joinURL("a", "/b", "/c"); // a/b/c ``` ### `resolveURL` ```ts -resolveURL('http://foo.com/foo?test=123#token', 'bar', 'baz') +resolveURL("http://foo.com/foo?test=123#token", "bar", "baz"); // http://foo.com/foo/bar/baz?test=123#token ``` ### `parseURL` ```ts -parseURL('http://foo.com/foo?test=123#token') +parseURL("http://foo.com/foo?test=123#token"); // { protocol: 'http:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' } -parseURL('foo.com/foo?test=123#token') +parseURL("foo.com/foo?test=123#token"); // { pathname: 'foo.com/foo', search: '?test=123', hash: '#token' } -parseURL('foo.com/foo?test=123#token', 'https://') +parseURL("foo.com/foo?test=123#token", "https://"); // { protocol: 'https:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' } ``` ### `stringifyParsedURL` ```ts -const obj = parseURL('http://foo.com/foo?test=123#token') -obj.host = 'bar.com' +const obj = parseURL("http://foo.com/foo?test=123#token"); +obj.host = "bar.com"; -stringifyParsedURL(obj) +stringifyParsedURL(obj); // http://bar.com/foo?test=123#token ``` ### `withQuery` ```ts -withQuery('/foo?page=a', { token: 'secret' }) +withQuery("/foo?page=a", { token: "secret" }); // /foo?page=a&token=secret ``` ### `getQuery` ```ts -getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD') +getQuery("http://foo.com/foo?test=123&unicode=%E5%A5%BD"); // { test: '123', unicode: '好' } ``` @@ -103,10 +103,10 @@ getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD') ```ts // Result: filename.ext -parseFilename('http://example.com/path/to/filename.ext') +parseFilename("http://example.com/path/to/filename.ext"); // Result: undefined -parseFilename('/path/to/.hidden-file', { strict: true }) +parseFilename("/path/to/.hidden-file", { strict: true }); ``` ### `$URL` @@ -122,7 +122,7 @@ Implementing URL interface with improvements: - Punycode support for host encoding ```ts -new $URL('http://localhost:3000/hello?world=true') +new $URL("http://localhost:3000/hello?world=true"); // { protocol: 'http:', host: 'localhost:3000', auth: '', pathname: '/hello', query: { world: 'true' }, hash: '' } ``` @@ -131,14 +131,14 @@ new $URL('http://localhost:3000/hello?world=true') Ensures url ends with a trailing slash. ```ts -withTrailingSlash('/foo') +withTrailingSlash("/foo"); // /foo/ ``` Set the second option to `true` to support query parameters: ```ts -withTrailingSlash('/path?query=true', true) +withTrailingSlash("/path?query=true", true); // /path/?query=true ``` @@ -147,14 +147,14 @@ withTrailingSlash('/path?query=true', true) Ensures url does not ends with a trailing slash. ```ts -withoutTrailingSlash('/foo/') +withoutTrailingSlash("/foo/"); // /foo ``` Set the second option to `true` to support query parameters: ```ts -withoutTrailingSlash('/path/?query=true', true) +withoutTrailingSlash("/path/?query=true", true); // /path?query=true ``` @@ -163,10 +163,10 @@ withoutTrailingSlash('/path/?query=true', true) Ensures url does not have double slash (except for protocol). ```ts -cleanDoubleSlashes('//foo//bar//') +cleanDoubleSlashes("//foo//bar//"); // /foo/bar/ -cleanDoubleSlashes('http://example.com/analyze//http://localhost:3000//') +cleanDoubleSlashes("http://example.com/analyze//http://localhost:3000//"); // http://example.com/analyze/http://localhost:3000/ ``` @@ -175,7 +175,7 @@ cleanDoubleSlashes('http://example.com/analyze//http://localhost:3000//') Check two paths are equal or not. Trailing slash and encoding are normalized before comparison. ```ts -isSamePath('/foo', '/foo/') +isSamePath("/foo", "/foo/"); // true ``` @@ -184,7 +184,7 @@ isSamePath('/foo', '/foo/') Check if a path starts with `./` or `../`. ```ts -isRelative('./foo') +isRelative("./foo"); // true ``` @@ -193,7 +193,7 @@ isRelative('./foo') Ensures url protocol is `http` ```ts -withHttp('https://example.com') +withHttp("https://example.com"); // http://example.com ``` @@ -202,7 +202,7 @@ withHttp('https://example.com') Ensures url protocol is `https` ```ts -withHttps('http://example.com') +withHttps("http://example.com"); // https://example.com ``` @@ -211,7 +211,7 @@ withHttps('http://example.com') Changes url protocol passed as second argument ```ts -withProtocol('http://example.com', 'ftp://') +withProtocol("http://example.com", "ftp://"); // ftp://example.com ``` @@ -220,7 +220,7 @@ withProtocol('http://example.com', 'ftp://') Removes url protocol ```ts -withoutProtocol('http://example.com') +withoutProtocol("http://example.com"); // example.com ``` @@ -229,19 +229,19 @@ withoutProtocol('http://example.com') Compare two URLs regardless of their slash condition or encoding: ```ts -isEqual('/foo', 'foo') +isEqual("/foo", "foo"); // true -isEqual('foo/', 'foo') +isEqual("foo/", "foo"); // true -isEqual('/foo bar', '/foo%20bar') +isEqual("/foo bar", "/foo%20bar"); // true // Strict compare -isEqual('/foo', 'foo', { leadingSlash: true }) +isEqual("/foo", "foo", { leadingSlash: true }); // false -isEqual('foo/', 'foo', { trailingSlash: true }) +isEqual("foo/", "foo", { trailingSlash: true }); // false -isEqual('/foo bar', '/foo%20bar', { encoding: true }) +isEqual("/foo bar", "/foo%20bar", { encoding: true }); // false ``` @@ -250,11 +250,11 @@ isEqual('/foo bar', '/foo%20bar', { encoding: true }) Add a fragment (or hash) to a URL: ```ts -withFragment('/foo', 'bar') +withFragment("/foo", "bar"); // /foo#bar -withFragment('/foo#bar', 'baz') +withFragment("/foo#bar", "baz"); // /foo#baz -withFragment('/foo#bar', '') +withFragment("/foo#bar", ""); // /foo ``` @@ -265,6 +265,7 @@ withFragment('/foo#bar', '') Special thanks to Eduardo San Martin Morote ([posva](https://github.com/posva)) for [encoding utilities](https://github.com/vuejs/vue-router-next/blob/v4.0.1/src/encoding.ts) + [npm-version-src]: https://img.shields.io/npm/v/ufo?style=flat&colorA=18181B&colorB=F0DB4F [npm-version-href]: https://npmjs.com/package/ufo [npm-downloads-src]: https://img.shields.io/npm/dm/ufo?style=flat&colorA=18181B&colorB=F0DB4F