README
URI Parser
Just another URI parser.
Install
npm
npm install just-another-url-parser --save
How to use
Parsing URL
Just feed me a url to parse:
let URI = require('just-another-url-parser').URI,
href = 'http://user:pass@host.com:8080/p/a/t/h?a=1&b=true&ccc=test===&b=hello#/h/a/s/h',
uri = new urlparser.URI(href);
console.log(`uri = ${uri}`);
//console output:
uri = {
protocol: 'http',
auth: {
username: 'user',
password: 'pass'
},
host: 'host.com:8080',
hostname: 'host.com',
port: 8080,
search: {
a: {value:'1', values:['1']},
b: {value:'true', values:['true', 'hello']},
ccc: {value:'test===', values:['test===']}
},
hash: '/h/a/s/h',
pathname: '/p/a/t/h',
path: '/p/a/t/h?a=1&b=true&ccc=test===&b=hello',
server: 'http://user:pass@host.com:8080',
absoluteURL: 'http://user:pass@host.com:8080/p/a/t/h',
fullURL: 'http://user:pass@host.com:8080/p/a/t/h?a=1&b=true&ccc=test===&b=hello#/h/a/s/h',
}
You can parse any kind of url:
new URI('helloworld.com');
new URI('ftp://foo:bar@helloworld.com');
new URI('/foo/bar?test=123');
new URI('file:///Users/foo/Downloads');
...
URL Manipulation
You can manipluate the search query to get a new url:
let boringURI = new URI('/foo/bar?hello=world');
console.log(broingURI.path);
//boringURI.path = "/foo/bar?hello=world"
boringURI.search.addItem('hello', 'newWorld');
boringURI.search.setItem('foo', 'bar');
boringURI.search.deleteItem('hello', 'world');
console.log(boringURI.path);
//boringURI.path = "/foo/bar?hello=newWorld&foo=bar"
Wildcard Matching & Replacing
//matching host
new URI('bar.foo.com').match('*.foo.com'); //true
new URI('a.b.c.foo.com').match('**.foo.com'); //true
new URI('foo.com').match('**.foo.com'); //true
new URI('foo.com').match('*.foo.com'); //false
//matching path
new URI('example.com/foo/bar').match('/foo/**'); //true
//matching any part of the uri you like
new URI(' ... ').match([protocol, hostname, port, pathname, queryString|queryObject]);
//replace url if pattern is matched
//http://foo.com/bar => http://example.net/a/b/c/bar
new URI('http://foo.com/bar').replace({hostname:'foo.*'}, 'example.net/a/b/c');