Skip to main content

URIUtils

Module Overview

Keywork uses JavaScript's built-in URL Pattern API to create pattern matchers. The syntax is based on path-to-regexp. Wildcards, named capture groups, regular groups, and group modifiers are all supported.

URL patterns can be matched against full URLs, or individual URL components:

Matching URL pathname pattern.
const pattern = new URLPattern({ pathname: '/users/:user' })
const match = pattern.exec('/users/jessie')

console.log(match.pathname.groups.user) // jessie
Matching a full URL pattern.
const pattern = new URLPattern('https://example.com/books/:id')
console.log(pattern.test('https://example.com/books/123')) // true
console.log(pattern.test('https://keywork.app/books/123')) // false
Matching URL pathname pattern with a base URL
const pattern = new URLPattern('/:article', 'https://blog.example.com')

console.log(pattern.test('https://blog.example.com/article')) // true
console.log(pattern.test('https://blog.example.com/article/123')) // false

Peer Dependencies

Support for the URL Pattern API varies across browser runtimes. You may need a polyfill if your app uses a runtime that hasn't yet added URLPattern class.

Run in the root of your project.
$ yarn add urlpattern-polyfill
## Related Entries

External Resources

Usage

import * as URIUtils from 'keywork/uri'

Type Aliases

Path Parsing Functions

Type Cast Functions

Exports

PathBuilder

Re-exports PathBuilder