1.0.1 • Published 5 years ago
super-url-path v1.0.1
super-url-path
A super library for formatting URL paths, similar to TS static grammar detection.
Install
npm i super-url-path
Usage
Usually it is used in this way:
const SuperUrlPath = require('SuperUrlPath');
const uri = new SuperUrlPath('/abc/des/abc-d_:{abc:string}*x--a{cde?:number}-d/test{value:string}');
console.log(uri.exec('/abc/des/abc-d_:dista*x--a123-d/testdd'));
console.log(uri.router({
abc: 'dddd',
cde: 345,
value: 'dafa'
}))
Variable Grammar
it likes typescript:
abc: string
# or
abc?: string
You should use it like this:
/abc/des/abc-d_:{abc:string}*x--a{cde?:number}-d/test{value:string}
uri.router(options)
create a new url by options.
uri.router({
abc: 'dddd',
cde: 345,
value: 'dafa'
}))
uri.match(url)
Judging whether URL conforms to the rules. it returns a boolean value.
uri.match('/abc/des/abc-d_:dista*x--a123-d/testdd'); // true or false
uri.exec(url)
Get the value of the variable from the rule and return a JSON data format
const SuperUrlPath = require('SuperUrlPath');
const uri = new SuperUrlPath('/abc/des/abc-d_:{abc:string}*x--a{cde?:number}-d/test{value:string}');
console.log(uri.exec('/abc/des/abc-d_:dista*x--a123-d/testdd'));
// => { abc: 'dista', cde: '123', value: 'dd' }
Add custom data format
use SuperUrlPath.addType(name, regexp_string)
- name
string
the datatype name. - regexp_string
string
a String Regular Expression.
const SuperUrlPath = require('SuperUrlPath');
SuperUrlPath.addType('user_id', 'user_(\\d+)_uuid_[a-zA-Z0-9]');
// then you can use like this
const uri = new SuperUrlPath('/interface/api/user/custom_{user: user_id}/{id: number}');
License
super-url-path
is MIT licensed.