Skip to content

Commit

Permalink
docs + better host support.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 14, 2017
1 parent 61981af commit e985633
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
connection-string
=================

URL Connection String Parser - _for all browsers and Node.js versions._
URL Connection String Parser, with fully optional syntax.

[![Build Status](https://travis-ci.org/vitaly-t/connection-string.svg?branch=master)](https://travis-ci.org/vitaly-t/connection-string)
[![Coverage Status](https://coveralls.io/repos/vitaly-t/connection-string/badge.svg?branch=master)](https://coveralls.io/r/vitaly-t/connection-string?branch=master)
Expand All @@ -12,7 +12,7 @@ Takes a URL connection string (with every element being optional):
protocol://user:password@hostname:12345/seg1/seg2?p1=val1&p2=val2
```

and converts it into an object:
and converts it into an object that contains only what's specified:

```js
{
Expand All @@ -30,7 +30,20 @@ and converts it into an object:
}
```

It only sets properties that are present in the connection string. See the [Optional Format].
**Short-syntax examples:**

* `localhost` => `{host: 'localhost', hostname: 'localhost'}`
* `localhost:12345` => `{host: 'localhost:12345', hostname: 'localhost', port: 12345}`
* `abc:///seg1?p1=val` => `{protocol: 'abc', segments: ['seg1'], params: {p1: 'val'}}`
* `:12345` => `{host: ':12345', port: 12345}`
* `username@` => `{user: 'username'}`
* `:pass123@` => `{password: 'pass123'}`
* `/seg1/seg2` => `{segments: ['seg1', 'seg2']}`
* `?param1=1&param2=2` => `{params: {param1: '1', param2: '2'}}`

For a complete list of short-syntax examples see the [Optional Format].

All browsers and Node.js versions are supported.

## Installing

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connection-string",
"version": "0.1.4",
"version": "0.1.5",
"description": "URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
if (cs[0] !== '/') {
if (cs[0] === '[') {
// It is an IPv6, with [::] being the shortest possible
m = cs.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+))?)/i);
m = cs.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+)[?/]?$)?)/i);
} else {
// It is either IPv4 or a name
m = cs.match(/(([a-z0-9.-]*)(?::([0-9]+))?)/i);
m = cs.match(/(([a-z0-9.-]*)(?::([0-9]+)[?/]?$)?)/i);
}
if (m) {
if (m[1]) {
result.host = m[1];
result.host = m[1].replace(/[/?]/, '');
}
if (m[2]) {
result.hostname = m[2];
Expand Down
21 changes: 20 additions & 1 deletion test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('password', function () {
});
});

describe('user+password', function () {
describe('user + password', function () {
it('must allow skipping both', function () {
expect(parse('@')).toEqual({});
expect(parse(':@')).toEqual({});
Expand All @@ -83,6 +83,10 @@ describe('host', function () {
hostname: 'server'
});
});
it('must skip port endings', function () {
expect(parse('local:123/')).toEqual({host: 'local:123', hostname: 'local', port: 123});
expect(parse('local:123?')).toEqual({host: 'local:123', hostname: 'local', port: 123});
});
it('must not allow URL characters', function () {
expect(parse('server%20')).toEqual({
host: 'server',
Expand Down Expand Up @@ -117,6 +121,15 @@ describe('host', function () {
expect(parse('[a]:123')).toEqual({});
expect(parse('[a-b-c]')).toEqual({});
});
it('must ignore the invalid ports', function () {
expect(parse('[::]:1a')).toEqual({host: '[::]', hostname: '::'});
expect(parse('[::]:abc')).toEqual({host: '[::]', hostname: '::'});
});
it('must allow valid ports', function () {
expect(parse('[::]:1')).toEqual({host: '[::]:1', hostname: '::', port: 1});
expect(parse('[::]:1/')).toEqual({host: '[::]:1', hostname: '::', port: 1});
expect(parse('[::]:123?')).toEqual({host: '[::]:123', hostname: '::', port: 123});
});
});

describe('port', function () {
Expand All @@ -132,6 +145,12 @@ describe('port', function () {
port: 0
});
});
it('must not allow invalid terminators', function () {
expect(parse(':12345a')).toEqual({});
expect(parse('@:12345a')).toEqual({});
expect(parse(':abc')).toEqual({});
expect(parse('@:abc123')).toEqual({});
});
});

describe('segments', function () {
Expand Down

0 comments on commit e985633

Please sign in to comment.