ufo

repository·main·Indexed 23 days ago

https://github.com/unjs/ufo

A collection of human-friendly URL utilities for parsing, encoding, joining, and normalizing URLs and paths. Version 1.6.4 provides robust tools for handling query strings, Punycode hostname encoding, path manipulation, and security checks for script protocols.

Tokens
3.8K
Snippets
15
Records
28
Agent score
80%

What's inside ufo

  1. Use native URL or ufo.parseURL instead of $URL

    main
    The $URL class and the createURL function are deprecated. For modern applications, you should use the native URL constructor (e.g., new URL(input)) or the ufo.parseURL(input) function provided by the library to parse and manipulate URLs.
  2. Encode query keys and values

    main

    When building query strings, use these functions to ensure keys and values are properly escaped:

    • encodeQueryValue(input): Encodes a value for the query section. It handles strings and QueryValue types (converting objects to JSON). It converts spaces to + and encodes characters like #, &, `, ^, and /.
    • encodeQueryKey(text): Encodes a key for the query section. It uses encodeQueryValue and additionally encodes the = character.
  3. Parse path components with `parsePath()`

    main

    Use parsePath() to split a string into its pathname, search (query string), and hash components. This is useful when you only care about the path-related parts of a URL or a partial URL string.

    parsePath("http://foo.com/foo?test=123#token");
    // { pathname: 'http://foo.com/foo', search: '?test=123', hash: '#token' }
  4. Compare URLs and paths

    main

    Use isEqual to compare two URLs or paths with configurable strictness.

    Options for isEqual(a, b, options):

    • trailingSlash: If false (default), both paths are normalized to have a trailing slash before comparison.
    • leadingSlash: If false (default), both paths are normalized to have a leading slash before comparison.
    • encoding: If false (default), both paths are decoded before comparison.

    Use isSamePath(p1, p2) for a simpler comparison that always normalizes trailing slashes and encoding.

    isSamePath("/foo", "/foo/"); // true
    
    isEqual("/foo", "foo"); // true
    
    isEqual("/foo", "foo"); // true
    
    isEqual("/foo", "foo", { leadingSlash: true }); // false
    
    isEqual("foo/", "foo"); // true
    
    isEqual("foo/", "foo", { trailingSlash: true }); // false
    
    isEqual("/foo bar", "/foo%20bar"); // true
    
    isEqual("/foo bar", "/foo%20bar", { encoding: true }); // false
  5. Decode URL encoded text

    main

    Use these functions to revert encoded URL components back to their original form:

    • decode(text): A safe wrapper around decodeURIComponent. If decoding fails, it returns the original text instead of throwing an error.
    • decodePath(text): Decodes a path section, specifically handling the %252F (encoded slash) case to remain consistent with encodePath.
    • decodeQueryKey(text): Decodes a query key, converting + characters back into spaces.
    • decodeQueryValue(text): Decodes a query value, converting + characters back into spaces.
  6. Parse a query string with parseQuery()

    main

    Use parseQuery() to convert a URL query string into a plain object. The function handles strings with or without a leading ?. If a key appears multiple times in the query string, the resulting value will be an array of strings. To prevent prototype pollution, the keys __proto__ and constructor are automatically ignored.

    parseQuery("?foo=bar&baz=qux");
    // { foo: "bar", baz: "qux" }
    
    parseQuery("tags=javascript&tags=web&tags=dev");
    // { tags: ["javascript", "web", "dev"] }
  7. Encode text for URL sections

    main

    Use the following functions to encode strings for specific parts of a URL to ensure special characters are handled correctly:

    • encode(text): General encoding for path, search, and hash sections. It uses encodeURI and ensures the pipe | character is handled.
    • encodeHash(text): Specifically for the hash (#) section. It preserves {, }, and ^ characters.
    • encodePath(text): For the path section. It encodes characters like #, ?, &, and + to prevent them from being interpreted as URL delimiters.
    • encodeParam(text): For path parameters. It performs everything encodePath does, plus it encodes the slash / character to prevent it from being treated as a path segment separator.