1.5.0 • Published 8 years ago

min-url v1.5.0

Weekly downloads
175
License
ISC
Repository
github
Last release
8 years ago

min-url

Build status NPM version Downloads Dependency Status

Simple Url Parse and Format

Installation

npm i min-url

Usage

var Url = require('min-url')

var url = Url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string#hash', true)

console.log(url)

/* =>
{ 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
  1. if url is not string, return url
  2. get hash
    1. splitTwo by # get rest and hash
  3. get query
    1. splitTwo by ? get rest and query
    2. if shouldParseQuery
      1. query = parse query
  4. get schema
    1. splicePattern rest by /^[a-zA-Z][a-zA-Z0-9+-.]*:/ get protocol and rest
    2. protocol = protocol to lowercase
  5. if rest startsWith //
    1. rest = slice(rest, 2)
  6. get pathname
    1. split rest by /, get rest and pathname
    2. pathname = '/' + unescape(pathname || '')
  7. get auth and host
    1. split rest by @, get auth and host
    2. if not host
      1. host = auth
      2. auth = null
  8. get hostname and port
    1. split host, get hostname and port
    2. if port
      1. port = tonumber(port)

License

License

1.5.0

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.1

9 years ago

1.3.0

10 years ago

1.2.2

11 years ago

1.2.1

11 years ago

1.1.3

11 years ago

1.1.2

11 years ago

1.1.0

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago