Modules@robinpath/url
url

@robinpath/url

0.1.1Node.jsPublic

URL parsing, formatting, and query parameter manipulation utilities using the built-in URL API

Url

URL parsing, formatting, and query parameter manipulation utilities using the built-in URL API

Package: @robinpath/url | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the url module when you need to:

  • Parse a URL string into its component parts -- Use url.parse to perform this operation
  • Format URL component parts into a URL string -- Use url.format to perform this operation
  • Resolve a relative URL against a base URL -- Use url.resolve to perform this operation
  • Get the value of a single query parameter from a URL -- Use url.getParam to perform this operation
  • Remove a query parameter from a URL -- Use url.removeParam to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse a URL string into its component partsAn object with protocol, hostname, port, pathname, search, hash, origin, and href
formatFormat URL component parts into a URL stringThe formatted URL string
resolveResolve a relative URL against a base URLThe resolved absolute URL string
getParamGet the value of a single query parameter from a URLThe value of the query parameter, or null if not found
setParamSet a query parameter on a URL, replacing any existing valueThe URL string with the parameter set
removeParamRemove a query parameter from a URLThe URL string without the specified parameter
getParamsGet all query parameters from a URL as a plain objectAn object containing all query parameter key-value pairs
setParamsSet multiple query parameters on a URL at onceThe URL string with all specified parameters set
getHostnameExtract the hostname from a URLThe hostname portion of the URL
getPathnameExtract the pathname from a URLThe pathname portion of the URL
getProtocolExtract the protocol from a URLThe protocol of the URL including the trailing colon (e.g. "https:")
isValidCheck whether a string is a valid URLTrue if the string is a valid URL, false otherwise
encodeEncode a string for safe use in a URL componentThe URI-encoded string
decodeDecode a URI-encoded stringThe decoded string

Functions

parse

Parse a URL string into its component parts

Module: url | Returns: object -- An object with protocol, hostname, port, pathname, search, hash, origin, and href

url.parse "https://example.com:8080/path?q=1#frag"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string to parse

format

Format URL component parts into a URL string

Module: url | Returns: string -- The formatted URL string

url.format { protocol: "https:", hostname: "example.com", pathname: "/path" }
ParameterTypeRequiredDescription
partsobjectYesAn object with protocol, hostname, port, pathname, search, and hash properties

resolve

Resolve a relative URL against a base URL

Module: url | Returns: string -- The resolved absolute URL string

url.resolve "https://example.com/a/b" "../c"
ParameterTypeRequiredDescription
basestringYesThe base URL
relativestringYesThe relative URL to resolve

getParam

Get the value of a single query parameter from a URL

Module: url | Returns: string -- The value of the query parameter, or null if not found

url.getParam "https://example.com?foo=bar" "foo"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string
paramNamestringYesThe name of the query parameter to retrieve

setParam

Set a query parameter on a URL, replacing any existing value

Module: url | Returns: string -- The URL string with the parameter set

url.setParam "https://example.com" "page" "2"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string
paramNamestringYesThe name of the query parameter to set
valuestringYesThe value to set for the query parameter

removeParam

Remove a query parameter from a URL

Module: url | Returns: string -- The URL string without the specified parameter

url.removeParam "https://example.com?foo=bar&baz=1" "foo"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string
paramNamestringYesThe name of the query parameter to remove

getParams

Get all query parameters from a URL as a plain object

Module: url | Returns: object -- An object containing all query parameter key-value pairs

url.getParams "https://example.com?foo=bar&baz=1"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string

setParams

Set multiple query parameters on a URL at once

Module: url | Returns: string -- The URL string with all specified parameters set

url.setParams "https://example.com" { page: "2", limit: "10" }
ParameterTypeRequiredDescription
urlStringstringYesThe URL string
paramsobjectYesAn object of key-value pairs to set as query parameters

getHostname

Extract the hostname from a URL

Module: url | Returns: string -- The hostname portion of the URL

url.getHostname "https://example.com:8080/path"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string

getPathname

Extract the pathname from a URL

Module: url | Returns: string -- The pathname portion of the URL

url.getPathname "https://example.com/a/b/c"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string

getProtocol

Extract the protocol from a URL

Module: url | Returns: string -- The protocol of the URL including the trailing colon (e.g. "https:")

url.getProtocol "https://example.com"
ParameterTypeRequiredDescription
urlStringstringYesThe URL string

isValid

Check whether a string is a valid URL

Module: url | Returns: boolean -- True if the string is a valid URL, false otherwise

url.isValid "https://example.com"
ParameterTypeRequiredDescription
urlStringstringYesThe string to validate as a URL

encode

Encode a string for safe use in a URL component

Module: url | Returns: string -- The URI-encoded string

url.encode "hello world&foo=bar"
ParameterTypeRequiredDescription
valuestringYesThe string to encode

decode

Decode a URI-encoded string

Module: url | Returns: string -- The decoded string

url.decode "hello%20world%26foo%3Dbar"
ParameterTypeRequiredDescription
valuestringYesThe URI-encoded string to decode

Error Handling

All functions throw on failure. Common errors:

ErrorCause
format requires an object argument with URL componentsCheck the error message for details
@desc "Parse and validate result"
do
  set $result as url.parse "https://example.com:8080/path?q=1#frag"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Param

Retrieve all items and loop through them.

@desc "Get param and iterate results"
do
  set $result as url.getParam "https://example.com?foo=bar" "foo"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Url workflow

Chain multiple url operations together.

@desc "Parse, format, and more"
do
  set $r_parse as url.parse "https://example.com:8080/path?q=1#frag"
  set $r_format as url.format { protocol: "https:", hostname: "example.com", pathname: "/path" }
  set $r_resolve as url.resolve "https://example.com/a/b" "../c"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as url.parse "https://example.com:8080/path?q=1#frag"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/url

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.1 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities