URL utils for humans
- Depending on native
URL
to parse urls and encoding - Zero dependency
- Treeshakable
Install using npm or yarn:
npm i @nuxt/ufo
# or
yarn add @nuxt/ufo
Import:
// CommonJS
const { normalizeURL, joinURL } = require('@nuxt/ufo')
// ESM
import { normalizeURL, joinURL } from '@nuxt/ufo'
Notice: You may need to transpile package and add URL polyfill for legacy environments
- Ensures URL is properly encoded
- Ensures pathname starts with slash
- Preserves protocol/host if provided
// Result: /test?query=123%20123#hash,%20test
normalizeURL('test?query=123 123#hash, test')
// Result: http://loalhost:3000/
normalizeURL('http://localhost:3000')
// Result: /a/b/c
joinURL('a', '/b', '/c')
// Result: http://foo.com/foo/bar/baz?test=123#token
joinURL('http://foo.com/foo?test=123#token', 'bar', 'baz')
Ensures url ends with a trailing slash
// Result: /foo/
withTrailingSlash('/foo')
Ensures url does not ends with a trailing slash
// Result: /foo
withoutTrailingSlash('/foo/')
MIT. Made with ๐