min-url
Simple Url Parse and Format
Installation
npm i min-url
Usage
var Url = var url = Url console /* =>{ hash: '#hash', protocol: 'http:', query: { query: 'string' }, pathname: '/p/a/t/h', auth: 'user:pass', hostname: 'host.com', port: 8080 }*/
Api
-
Url.parse(string, [shouldParseQuery])
-
Url.format(object)
-
Url.appendQuery(url, query)
append query string/object to url
Progress
Url.parse(string, shouldParseQuery)
splitTwo(str, sp)
1. var arr = split(str, sp)
2. first = arr[0]
3. second = slice(arr, 1).join(sp)
4. return first, second
splicePattern(str, regexp)
var matched
var rest = replace(str, regexp, function(_matched) {
matched = _matched
return ''
})
return matched, rest
- if url is not string, return url
- get hash
- splitTwo by
#
get rest and hash
- splitTwo by
- get query
- splitTwo by
?
get rest and query - if shouldParseQuery
- query = parse query
- splitTwo by
- get schema
- splicePattern rest by
/^[a-zA-Z][a-zA-Z0-9+-.]*:/
get protocol and rest - protocol = protocol to lowercase
- splicePattern rest by
- if rest startsWith
//
- rest = slice(rest, 2)
- get pathname
- split rest by
/
, get rest and pathname - pathname = '/' + unescape(pathname || '')
- split rest by
- get auth and host
- split rest by
@
, get auth and host - if not host
- host = auth
- auth = null
- split rest by
- get hostname and port
- split host, get hostname and port
- if port
- port = tonumber(port)