UriTemplatesEs
This is a ported library version built to support es standard
.
Can find original library here.
URI Templates (RFC6570) in JavaScript, including de-substitution.
It is tested against the official test suite, including the extended tests (more that 300 tests).
The 'de-substitution' extracts parameter values from URIs. It is also tested against the official test suite (including extended tests).
Usages
Install library using the next command:
npm i uri-templates-es --save
Create a template object:
; const template1 = '/date/{colour}/{shape}/';const template2 = '/prefix/{?params*}';
Example of substitution using an object:
// '/categories/green/round/'const uri1 = template1; // '/prefix/?a=A&b=B&c=Cconst uri2 = template2;
Example of substitution using a callback:
// '/categories/example_colour/example_shape/'const uri1b = template1;
Example of guess variables from URI ('de-substitution'):
const uri2b = '/prefix/?beep=boop&bleep=bloop';const params = template2;/*{ params: { beep: 'boop', bleep: 'bloop' }*/
While templates can be ambiguous (e.g. '{var1}{var2}'
), it will still produce something that reconstructs into the original URI.
It can handle all the cases in the official test suite, including the extended tests:
const template = '{/id*}{?fields,token}'; const values = template;/*{ id: ['person', 'albums'], fields: ['id', 'name', 'picture'], token: '12345'}*/