url-id-replace
v1.0.1
Published
Used for replacing parts of a path that look like IDs
Downloads
331
Readme
url-id-replace
Replace things like IDs and other regexes in a URL path with a placeholder
Sample Usage
const parse = require('url-id-replace')();
console.log(parse('/users/12345')); // /users/*
console.log(parse('/users/123e4567-e89b-12d3-a456-426655440000/')); // /users/*/
Built-in Matchers
A few common matchers are already provided:
const builtInMatchers = urlIdReplace.getBuiltInMatchers();
// Returns an object that looks like this:
{
digits: ..., // Regex for matching only digits
uuid: .., // Regex for matching only UUIDs
hexLowercase: ..., // Regex for matching only lowercase hex values that are at least 7 characters
hexUppercase: ..., // Regex for matching only uppercase hex values that are at least 7 chracters
iso8061: ... // Regex for matching ISO8061 timestamp values
}
Currently, only digits
and uuid
are included in the list of default matchers. (IE: if you use
the default parameters when building the parser.) The others are provided for your convenience if
you need them. Simply follow the next step for adding custom matchers.
Custom Matchers
Custom matchers are regexes that can be specified when the parser is created
const parse = require('url-id-replace')({
matchers: [/^ITEM-\d*$/]
});
console.log(parse('/items/ITEM-12345/location')); // /items/*/location
console.log(parse('/items/123')); // items/123 -- the default matchers were overwritten
This will override the default matchers. The default matchers are still available if you need them
by calling getDefaultMatchers()
. This returns an array of the default RegExps, to which you can
then add other matchers:
const urlIdReplace = require('url-id-replace');
const parse = urlIdReplace({
matchers: urlIdReplace.getDefaultMatchers().concat(/^ITEM-\d*$/)
});
console.log(parse('/items/ITEM-12345/location')); // /items/*/location
console.log(parse('/items/123')); // items/*
You can include the built-in matchers in a similar way:
const urlIdReplace = require('url-id-replace');
const parse = urlIdReplace({
matchers: [urlIdReplace.getBuiltInMatchers().hexLowercase]
});
console.log(parse('/items/abc348f/location')); // /items/*/location
Changing the Placeholder
Instead of *
, you can specify any string you want to be used for matches:
const parse = require('url-id-replace')({
placeholder: 'X'
});
console.log(parse('/items/123/location/')); // items/X/location/