What is url-value-parser?
The url-value-parser npm package is designed to parse URLs and extract meaningful values from them. It is particularly useful for extracting parameters, paths, and other components from URLs in a structured manner.
What are url-value-parser's main functionalities?
Parse URL and extract values
This feature allows you to parse a URL and extract dynamic values from it. In the example, the URL '/user/12345/profile' is parsed to extract the user ID '12345' and replace it with a placeholder ':id'.
const UrlValueParser = require('url-value-parser');
const parser = new UrlValueParser();
const result = parser.parse('/user/12345/profile');
console.log(result); // { path: '/user/:id/profile', params: { id: '12345' } }
Custom parameter patterns
This feature allows you to define custom patterns for parameters. In the example, a custom pattern for MongoDB ObjectIDs is used to parse the URL and extract the product ID.
const UrlValueParser = require('url-value-parser');
const parser = new UrlValueParser({ parameterPattern: /[a-f0-9]{24}/i });
const result = parser.parse('/product/5f8d0d55b54764421b7156c3');
console.log(result); // { path: '/product/:id', params: { id: '5f8d0d55b54764421b7156c3' } }
Parse query strings
This feature allows you to parse URLs with query strings and extract the query parameters. In the example, the URL '/search?q=nodejs&sort=asc' is parsed to extract the query parameters 'q' and 'sort'.
const UrlValueParser = require('url-value-parser');
const parser = new UrlValueParser();
const result = parser.parse('/search?q=nodejs&sort=asc');
console.log(result); // { path: '/search', query: { q: 'nodejs', sort: 'asc' } }
Other packages similar to url-value-parser
query-string
The query-string package is used to parse and stringify URL query strings. It focuses on handling query parameters rather than the entire URL structure. Compared to url-value-parser, it is more specialized in dealing with query strings and offers utilities for manipulating them.
url-parse
The url-parse package provides a way to parse URLs into their components (protocol, hostname, port, pathname, query, etc.). It offers a more comprehensive breakdown of the URL structure compared to url-value-parser, which focuses on extracting dynamic values and parameters.
path-to-regexp
The path-to-regexp package is used to convert paths to regular expressions and extract parameters from them. It is similar to url-value-parser in that it can extract dynamic values from paths, but it is more focused on path matching and less on parsing the entire URL.
UrlValueParser
A helper ES6 class letting you extract values from URL paths,
leaving the other parts untouched.
It uses an internal class ValueDetector
determining
what is a value and what is not. By default the following
path chunks are considered values:
- decimal numbers
- strings in UUID format
- hex numbers consisting of 7 or more characters
and consistent lower or upper case
- long base64 encoded strings
- JSON Web Tokens (JWT)
You can customize all of the logic by providing options,
overriding methods or providing your own value detector.
See the source - it's easy, i promise.
Usage
const UrlValueParser = require('url-value-parser');
const parser = new UrlValueParser();
parser.parsePathValues('/some/path/154/userId/ABC363AFE2');
parser.replacePathValues('/some/path/154/userId/ABC363AFE2', '#id');
Options
- replaceMasks - use custom masks instead of built-in
- extraMasks - add your custom masks additionally to the built-in ones
- minHexLength - when using built-in masks, count only long enough HEX values, DEFAULT: 7
- minBase64Length - when using built-in masks, count only long enough base64 values, DEFAULT: 66
If strings are provided in an array to replaceMasks and extraMasks,
then they're automatically converted into RegExp
Example:
const parser = new UrlValueParser({
minHexLength: 4,
extraMasks: [
/^z_.*$/,
'^[0-9]+\\.[0-9]+$'
]
});
License
MIT No Attribution License