Install
Install using npm or yarn:
npm i ufo
yarn add ufo
Import:
const { normalizeURL, joinURL } = require('ufo')
import { normalizeURL, joinURL } from 'ufo'
Notice: You may need to transpile package and add URL polyfill for legacy environments
Usage
normalizeURL
- Ensures URL is properly encoded
- Ensures pathname starts with slash
- Preserves protocol/host if provided
normalizeURL('test?query=123 123#hash, test')
normalizeURL('http://localhost:3000')
joinURL
joinURL('a', '/b', '/c')
resolveURL
joinURL('http://foo.com/foo?test=123#token', 'bar', 'baz')
withQuery
withQuery('/foo?page=a', { token: 'secret' })
getQuery
getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD')
$URL
Implementing URL interface with some improvements:
- Supporting schemeless and hostless URLs
- Supporting relative URLs
- Preserving trailing-slash status
- Decoded and mutable classs properties (
protocol
, host
, auth
, pathname
, query
, hash
) - Consistent URL parser independent of environment
- Consistent encoding independent of environment
- Punycode support for host encoding
withTrailingSlash
Ensures url ends with a trailing slash
withTrailingSlash('/foo')
withoutTrailingSlash
Ensures url does not ends with a trailing slash
withoutTrailingSlash('/foo/')
cleanDoubleSlashes
Ensures url does not have double slash (except for protocol)
cleanDoubleSlashes('//foo//bar//')
cleanDoubleSlashes('http://example.com/analyze//http://localhost:3000//')
License
MIT
Special thanks to Eduardo San Martin Morote (posva) for encoding utlities