Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ufo

unjs24.7mMIT1.5.4TypeScript support: included

URL utils for humans

readme

ufo

npm version npm downloads bundle Codecov

URL utils for humans.

Install

Install using npm or your favourite package manager:

Install package:

# npm
npm install ufo

# yarn
yarn add ufo

# pnpm
pnpm install ufo

# bun
bun install ufo

Import utils:

// ESM
import { normalizeURL, joinURL } from "ufo";

// CommonJS
const { normalizeURL, joinURL } = require("ufo");

// Deno
import { parseURL } from "https://unpkg.com/ufo/dist/index.mjs";

Encoding Utils

decode(text)

Decode text using decodeURIComponent. Returns the original text if it fails.

decodePath(text)

Decode path section of URL (consistent with encodePath for slash encoding).

decodeQueryKey(text)

Decodes query key (consistent with encodeQueryKey for plus encoding).

decodeQueryValue(text)

Decode query value (consistent with encodeQueryValue for plus encoding).

encode(text)

Encode characters that need to be encoded on the path, search and hash sections of the URL.

encodeHash(text)

Encode characters that need to be encoded on the hash section of the URL.

encodeHost(name)

Encodes hostname with punycode encoding.

encodeParam(text)

Encode characters that need to be encoded on the path section of the URL as a param. This function encodes everything encodePath does plus the slash (/) character.

encodePath(text)

Encode characters that need to be encoded on the path section of the URL.

encodeQueryKey(text)

Encode characters that need to be encoded query values on the query section of the URL and also encodes the = character.

encodeQueryValue(input)

Encode characters that need to be encoded query values on the query section of the URL.

Parsing Utils

parseAuth(input)

Takes a string of the form username:password and returns an object with the username and password decoded.

parseFilename(input)

Parses a url and returns last segment in path as filename.

If { strict: true } is passed as the second argument, it will only return the last segment only if ending with an extension.

Example:

// Result: filename.ext
parseFilename("http://example.com/path/to/filename.ext");

// Result: undefined
parseFilename("/path/to/.hidden-file", { strict: true });

parseHost(input)

Takes a string, and returns an object with two properties: hostname and port.

parsePath(input)

Splits the input string into three parts, and returns an object with those three parts.

parseURL(input, defaultProto?)

Takes a URL string and returns an object with the URL's protocol, auth, host, pathname, search, and hash.

Example:

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");
// { pathname: 'foo.com/foo', search: '?test=123', hash: '#token' }

parseURL("foo.com/foo?test=123#token", "https://");
// { protocol: 'https:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }

stringifyParsedURL(parsed)

Takes a ParsedURL object and returns the stringified URL.

Example:

const obj = parseURL("http://foo.com/foo?test=123#token");
obj.host = "bar.com";

stringifyParsedURL(obj); // "http://bar.com/foo?test=123#token"

Query Utils

encodeQueryItem(key, value)

Encodes a pair of key and value into a url query string value.

If the value is an array, it will be encoded as multiple key-value pairs with the same key.

parseQuery(parametersString)

Parses and decodes a query string into an object.

input can be a query string with or without the leading ?

stringifyQuery(query)

Stringfies and encodes a query object into a query string.

Utils

cleanDoubleSlashes(input)

Removes double slashes from the URL.

Example:

cleanDoubleSlashes("//foo//bar//"); // "/foo/bar/"

cleanDoubleSlashes("http://example.com/analyze//http://localhost:3000//");
// Returns "http://example.com/analyze/http://localhost:3000/"

getQuery(input)

Parses and decods the query object of an input URL into an object.

Example:

getQuery("http://foo.com/foo?test=123&unicode=%E5%A5%BD");
// { test: "123", unicode: "好" }

hasLeadingSlash(input)

Checks if the input has a leading slash. (e.g. /foo)

hasProtocol(inputString, opts)

hasTrailingSlash(input, respectQueryAndFragment?)

Checks if the input has a trailing slash.

isEmptyURL(url)

Checks if the input url is empty or /.

isEqual(a, b, options)

Checks if two paths are equal regardless of encoding, trailing slash, and leading slash differences.

You can make slash check strict by setting { trailingSlash: true, leadingSlash: true } as options. You can make encoding check strict by setting { encoding: true } as options.

Example:

isEqual("/foo", "foo"); // true
isEqual("foo/", "foo"); // true
isEqual("/foo bar", "/foo%20bar"); // true

// Strict compare
isEqual("/foo", "foo", { leadingSlash: true }); // false
isEqual("foo/", "foo", { trailingSlash: true }); // false
isEqual("/foo bar", "/foo%20bar", { encoding: true }); // false

isNonEmptyURL(url)

Checks if the input url is not empty nor /.

isRelative(inputString)

Check if a path starts with ./ or ../.

Example:

isRelative("./foo"); // true

isSamePath(p1, p2)

Check two paths are equal or not. Trailing slash and encoding are normalized before comparison.

Example:

isSamePath("/foo", "/foo/"); // true

isScriptProtocol(protocol?)

Checks if the input protocol is any of the dangerous blob:, data:, javascript: or vbscript: protocols.

joinRelativeURL()

Joins multiple URL segments into a single URL and also handles relative paths with ./ and ../.

Example:

joinRelativeURL("/a", "../b", "./c"); // "/b/c"

joinURL(base)

Joins multiple URL segments into a single URL.

Example:

joinURL("a", "/b", "/c"); // "a/b/c"

normalizeURL(input)

Normlizes inputed url:

  • Ensures url is properly encoded - Ensures pathname starts with slash - Preserves protocol/host if provided

Example:

normalizeURL("test?query=123 123#hash, test");
// Returns "test?query=123%20123#hash,%20test"

normalizeURL("http://localhost:3000");
// Returns "http://localhost:3000"

resolveURL(base)

Resolves multiple URL segments into a single URL.

Example:

resolveURL("http://foo.com/foo?test=123#token", "bar", "baz");
// Returns "http://foo.com/foo/bar/baz?test=123#token"

withBase(input, base)

Ensures the URL or pathname has a trailing slash.

If input aleady start with base, it will not be added again.

withFragment(input, hash)

Add/Replace the fragment section of the URL.

Example:

withFragment("/foo", "bar"); // "/foo#bar"
withFragment("/foo#bar", "baz"); // "/foo#baz"
withFragment("/foo#bar", ""); // "/foo"

withHttp(input)

Adds or replaces url protocol to http://.

Example:

withHttp("https://example.com"); // http://example.com

withHttps(input)

Adds or replaces url protocol to https://.

Example:

withHttps("http://example.com"); // https://example.com

withLeadingSlash(input)

Ensures the URL or pathname has a leading slash.

withoutBase(input, base)

Removes the base from the URL or pathname.

If input does not start with base, it will not be removed.

withoutFragment(input)

Removes the fragment section from the URL.

Example:

withoutFragment("http://example.com/foo?q=123#bar")
// Returns "http://example.com/foo?q=123"

withoutHost(input)

Removes the host from the URL preserving everything else.

Example:

withoutHost("http://example.com/foo?q=123#bar")
// Returns "/foo?q=123#bar"

withoutLeadingSlash(input)

Removes leading slash from the URL or pathname.

withoutProtocol(input)

Removes the protocol from the input.

Example:

withoutProtocol("http://example.com"); // "example.com"

withoutTrailingSlash(input, respectQueryAndFragment?)

Removes trailing slash from the URL or pathname.

If second argument is true, it will only remove the trailing slash if it's not part of the query or fragment with cost of more expensive operations.

Example:

withoutTrailingSlash("/foo/"); // "/foo"

withoutTrailingSlash("/path/?query=true", true); // "/path?query=true"

withProtocol(input, protocol)

Adds or Replaces protocol of the input URL.

Example:

withProtocol("http://example.com", "ftp://"); // "ftp://example.com"

withQuery(input, query)

Add/Replace the query section of the URL.

Example:

withQuery("/foo?page=a", { token: "secret" }); // "/foo?page=a&token=secret"

withTrailingSlash(input, respectQueryAndFragment?)

Ensures url ends with a trailing slash.

If seccond argument is true, it will only add the trailing slash if it's not part of the query or fragment with cost of more expensive operation.

Example:

withTrailingSlash("/foo"); // "/foo/"

withTrailingSlash("/path?query=true", true); // "/path/?query=true"

License

MIT

Special thanks to Eduardo San Martin Morote (posva) for encoding utilities

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v1.5.4

compare changes

🩹 Fixes

  • parseURL: Only normalize windows drive letters with file:// protocol (#246)
  • withProtocol: Consider relative protocol (#242)

🏡 Chore

🎨 Styles

  • Lint with prettier defaults (666091d)

❤️ Contributors

v1.5.3

compare changes

🩹 Fixes

  • joinRelativeURL: Avoid lookbehind regex for browser compatibility (#228)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.2

compare changes

🩹 Fixes

  • Use lookbehind regex only inside joinRelativeURL (#226)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.1

compare changes

🩹 Fixes

  • joinRelativeURL: Handle base with protocol (#222)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.0

compare changes

🚀 Enhancements

  • Add withoutHost utility (#212)
  • joinURL: Handle segments with ../ (#217)
  • joinRelativeURL (#220)

🩹 Fixes

  • withoutTrailingSlash: Consider qurry param (#219)

💅 Refactors

  • joinURL: Rewrite with clear syntax and relative ../ support (#218)

📖 Documentation

🏡 Chore

❤️ Contributors

v1.4.0

compare changes

🚀 Enhancements

  • Add withFragment utility (#193)
  • Add withoutFragment util (#199)

🔥 Performance

  • withFragment: Early return when no hash changes required (d6ce037)

🩹 Fixes

  • encodeQueryValue: Encode the slash character (#198)
  • Stringify protocol-relative URLs (#207)
  • withFragment: Use encodeHash for encoding (48237ab)

💅 Refactors

  • resolveUrl: Decouple from $URL (#186)
  • Deprecate $URL and createURL (f1af9b3)
  • normalizeURL: Decouple from $URL (9013029)
  • withoutFragment: Decouple from withFragment (712b8d5)

📖 Documentation

  • Remove mentioning $URL (65e6be8)
  • Update normalizeURL example (011777a)
  • Use jsdocs and automd (#209)

🏡 Chore

  • Update dependencies and lockfile (21e644e)
  • Format readme with prettier (6b00230)
  • Gitignore vitest temp files (89be2d1)
  • Update docs (5ab6d16)

❤️ Contributors

v1.3.2

compare changes

🩹 Fixes

  • parseURL: Parse protocol case-insensitively (#188)
  • Respect fragment in trailing slash utils (#175)

🏡 Chore

❤️ Contributors

v1.3.1

compare changes

🩹 Fixes

  • Test script protocols insensitively (#180)

🏡 Chore

❤️ Contributors

v1.3.0

compare changes

🚀 Enhancements

  • stringifyParsedURL: Support partial url inputs (#166)

🩹 Fixes

  • parseURL: Handle data: and blob protocols (#159)
  • parseURL, hasProtocol, isScriptProtocol: Ignore leading whitespaces (#170)

🏡 Chore

🤖 CI

  • Use conventional commit message (#157)

❤️ Contributors

v1.2.0

compare changes

🚀 Enhancements

  • Add isScriptProtocol util (#156)
  • Generic types for getQuery and parseQuery (#131)
  • Add parseFilename utility (#125)

🩹 Fixes

  • parseQuery: Decode space in query keys (#150)
  • Parsed query types should be string only (#139)
  • stringifyQuery: Filter out empty values (#148)
  • joinURL: Handle leading dot (#35)

🏡 Chore

❤️ Contributors

v1.1.2

compare changes

🩹 Fixes

  • Provide node16 style types field (#133)

📖 Documentation

  • Add jsdocs for parse utils (#134)
  • Add stringifyParsedURL (81e92b2)

🏡 Chore

❤️ Contributors

v1.1.1

compare changes

🩹 Fixes

  • hasProtocol: Expand protocol relative url regexp (#126)

✅ Tests

  • Add more tests for full coverage (#127)

❤️ Contributors

v1.1.0

compare changes

🚀 Enhancements

  • hasProtocol: Strict mode support (#124)
  • Stringify query values (#123)

🩹 Fixes

  • parseHost: Correctly parse hostnames with and without port (#120)
  • Port should be string (75a280c)
  • Encode [] and {} in query values to increase compatibility (#118)
  • hasProtocol: Accept backslash too (#122)

🏡 Chore

🎨 Styles

❤️ Contributors

1.0.1 (2022-11-29)

Bug Fixes

  • fix QueryValue type to be array only as QueryObject values (#101) (3fbad8c)
  • hasProtocol: do not treat windows drive letter as protocol (#106) (67c3dab)
  • query: uniform handling of empty array values (#103) (0b1007d)

1.0.0 (2022-11-14)

Features

Bug Fixes

  • remove leading slash when parsing windows file urls (#87) (187afb0)

0.8.6 (2022-10-15)

Bug Fixes

  • ensure withBase does not prefix URLs with protocol (#68) (b520298)

0.8.5 (2022-07-07)

Bug Fixes

  • withoutBase: preserve leading slash (fc72dd0), closes #64

0.8.4 (2022-05-06)

Features

0.8.3 (2022-03-31)

Bug Fixes

  • withProtocol: handle input without protocol (resolves #52) (4f66249)

0.8.2 (2022-03-31)

Features

  • withHttp, withHttps, withoutProtocol, withProtocol (#48) (f8ee0c0)

Bug Fixes

  • encodeQueryItem: stringify boolean values (#51) (469dd88)

0.8.1 (2022-03-16)

Bug Fixes

  • parseURL: don't fail if hostAndPath not matching (d297e60)

0.8.0 (2022-03-15)

⚠ BREAKING CHANGES

  • hasProtocol additional protocols (#46)

Bug Fixes

0.7.11 (2022-02-25)

Bug Fixes

  • parseURL: exclude hash from hostname match (resolves #42) (53a1cea)

0.7.10 (2022-01-31)

Bug Fixes

  • parseURL: normalize unescaped backslash to slash (07b97af)

0.7.9 (2021-08-17)

Bug Fixes

  • encode plus as %2B (500885b), closes #34
  • parseURL: support url with leading ? (resolves #33) (aab7d5f)

0.7.8 (2021-08-17)

Bug Fixes

0.7.7 (2021-06-30)

Features

  • support query params for trailingSlash utils (#24) (aa31481)

0.7.6 (2021-06-30)

Bug Fixes

  • stringifyParsedURL: check query existence in search (#29) (dd8bc35)

0.7.5 (2021-05-17)

Features

  • parseURL: support default proto (f0f79f0), closes #25

0.7.4 (2021-05-11)

0.7.3 (2021-05-11)

Features

  • support query params for trailingSlash utils (7655066), closes #19

Bug Fixes

  • parseQuery: prevent possible prototype pollution (f4be854)
  • slash encode in path (#23) (912399f)

0.7.2 (2021-04-28)

Bug Fixes

0.7.1 (2021-04-23)

0.7.0 (2021-04-23)

⚠ BREAKING CHANGES

  • add module exports field

Features

  • add module exports field (7c26571)

0.6.11 (2021-04-06)

Bug Fixes

  • pkg: remove exports field to improve compatibility with webpack4 (be3b1ae)

0.6.10 (2021-03-09)

Bug Fixes

0.6.9 (2021-03-05)

Features

0.6.8 (2021-03-05)

Features

  • hasProtocol: optionally allow protocol relative url (#16) (81a3f3e)

0.6.7 (2021-02-22)

Features

  • isEmptyURL and isNonEmptyURL utils (3c1c6d8)

Bug Fixes

  • joinURL, resolveURL: ignore empty url parts (c5fd74d)

0.6.6 (2021-02-10)

Bug Fixes

  • bail base utils if base is / or empty (bb09b06)

0.6.5 (2021-02-10)

Features

  • withBase and withoutBase utils (1667eca)

0.6.4 (2021-02-08)

Bug Fixes

  • joinURL: don't allow double '//' when single slashes are passed (#13) (e44fe2b)

0.6.3 (2021-02-08)

Bug Fixes

  • avoid empty string with without*Slash utils (126667d)

0.6.2 (2021-02-08)

Features

0.6.1 (2021-02-02)

0.6.0 (2021-02-02)

Features

  • decodeQueryValue and decode query params with support for space (#11) (44a0538)

0.5.4 (2021-01-07)

Bug Fixes

  • enable plus encoding for query value (82ab3f1)

0.5.3 (2021-01-06)

Bug Fixes

  • parseQuery: skip ? in query value while parsing (fixes #9) (0cf9f1a)

0.5.2 (2020-12-16)

Bug Fixes

  • pkg: allow requiring package.json (#6) (5fd5dc7)

0.5.1 (2020-12-16)

Bug Fixes

  • update encodeQueryValue to match URL spec for space and plus (9446d9c)

0.5.0 (2020-12-16)

⚠ BREAKING CHANGES

  • api updates and params renamed to query

Features

  • reduce dependency on $URL for less encode/decoding (38631cf)

0.4.0 (2020-12-16)

⚠ BREAKING CHANGES

  • lightweight joinURL + resolveURL

Features

  • lightweight joinURL + resolveURL (9a34622)

0.3.1 (2020-12-16)

Bug Fixes

  • pkg: remove trailing slash in exports (9d94e3c)

0.3.0 (2020-12-16)

⚠ BREAKING CHANGES

  • pkg: add exports and drop browser field
  • custom url parser

Features

Bug Fixes

  • preserve trailingslash from parse state (adc9688)
  • remove password from auth when is not provided (6616f02)

0.2.0 (2020-12-15)

⚠ BREAKING CHANGES

  • rewrite with URL ponyfill (#4)

Features

0.1.6 (2020-12-15)

Bug Fixes

0.1.5 (2020-12-15)

Bug Fixes

  • fix issue with hasProtocol (239cdf7)

0.1.4 (2020-12-12)

Bug Fixes

  • joinURL: preserve params from input path (8f8f10a)

0.1.3 (2020-12-12)

Bug Fixes

  • withParams: prevent double encoding raw params (28eb848)

0.1.2 (2020-12-12)

Bug Fixes

0.1.1 (2020-12-12)

Features

0.1.0 (2020-12-09)

⚠ BREAKING CHANGES

  • preserve relative urls without leading slash

Features

  • add cleanDoubleSlashes (#2) (cbb72de)
  • preserve relative urls without leading slash (34afd1e)

0.0.3 (2020-12-04)

Bug Fixes

0.0.2 (2020-12-04)

0.0.1 (2020-12-04)