/ufo

🛸 URL utils for humans

Primary LanguageTypeScriptMIT LicenseMIT

🛸 ufo

URL utils for humans

npm version npm downloads Github Actions Codecov bundle

  • Depending on native URL to parse urls and encoding
  • Zero dependency
  • Treeshakable

Install

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

Usage

normalizeURL

  • 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')

joinURL

// 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')

withTrailingSlash

Ensures url ends with a trailing slash

// Result: /foo/
withTrailingSlash('/foo')

withoutTrailingSlash

Ensures url does not ends with a trailing slash

// Result: /foo
withoutTrailingSlash('/foo/')

License

MIT. Made with 💖