Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 17, 2019
1 parent ea17d6d commit 3fc1cbc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -20,8 +20,8 @@ and converts it into an object that contains only what's specified:
user: 'user',
password: 'password',
hosts: [
{name: 'host1', port: 123, isIPv6: false},
{name: 'abcd::', port: 456, isIPv6: true}
{name: 'host1', port: 123, type: 'domain'},
{name: '[abcd::]', port: 456, type: 'IPv6'}
],
path: ['one', 'two'],
params: {
Expand All @@ -47,9 +47,9 @@ Unlike the default URL parser, this one supports the following:

**Short-syntax examples:**

* `localhost` => `{hosts: [{name: 'localhost', isIPv6: false}]`
* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345, isIPv6: false}]`
* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123, isIPv6: true}]`
* `localhost` => `{hosts: [{name: 'localhost', type: 'domain'}]`
* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345, type: 'domain'}]`
* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123, type: 'IPv6'}]`
* `abc:///one?p1=val` => `{protocol: 'abc', path: ['one'], params: {p1: 'val'}}`
* `:12345` => `{hosts: [{port: 12345}]`
* `username@` => `{user: 'username'}`
Expand All @@ -75,7 +75,7 @@ $ npm install connection-string
const parse = require('connection-string');

const obj = parse('my-server:12345');
//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]}
//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]}
```

or as a class:
Expand All @@ -84,7 +84,7 @@ or as a class:
const ConnectionString = require('connection-string');

const obj = new ConnectionString('my-server:12345');
//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]}
//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]}
```

* **Browsers**
Expand All @@ -103,7 +103,7 @@ const obj = new ConnectionString('my-server:12345');
import {ConnectionString} from 'connection-string'

const a = new ConnectionString('my-server:12345');
//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]}
//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]}
```

See also [WiKi Pages] for more examples and documentation.
Expand Down Expand Up @@ -161,14 +161,14 @@ is typically not needed. But if you do need `$` encoded everywhere, pass in `{en

* `plusForSpace` - Boolean (false), it is used only for parameter values, to encode spaces as `+` instead of `%20`.

### `static parseHost(host) => {name,port,isIPv6} | null`
### `static parseHost(host) => {name,port,type} | null`

When using an external list of default hosts, you may need to parse them independently, using this method,
so they can be correctly processed by method `setDefaults`.

```js
const h = ConnectionString.parseHost('[abcd::]:111');
//=> {name: 'abcd::', port: 111, isIPv6: true}
//=> {name: 'abcd::', port: 111, type: 'IPv6'}

const a = new ConnectionString('test://localhost:222/dbname', {hosts: [h]});
a.toString();
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "connection-string",
"version": "1.2.0",
"version": "2.0.0",
"description": "Advanced URL Connection String parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
10 changes: 2 additions & 8 deletions src/index.js
@@ -1,5 +1,3 @@
// TODO: setDefaults issue accepting :: or [::], inconsistent with the host parsing.

(function (window) {
'use strict';

Expand Down Expand Up @@ -123,7 +121,7 @@
var m, isIPv6;
if (host[0] === '[') {
// This is IPv6, with [::] being the shortest possible
m = host.match(/(\[([0-9a-z:%]{2,45})](?::(-?[0-9]+[^/?]*))?)/i);
m = host.match(/((\[[0-9a-z:%]{2,45}])(?::(-?[0-9]+[^/?]*))?)/i);
isIPv6 = true;
} else {
// It is either IPv4 or domain/socket
Expand Down Expand Up @@ -310,11 +308,7 @@
options = options || {};
var a = '';
if (obj.name) {
if (obj.type === hostType.IPv6) {
a = '[' + obj.name + ']';
} else {
a = encode(obj.name, options);
}
a = obj.type === hostType.IPv6 ? obj.name : encode(obj.name, options);
}
if (obj.port) {
a += ':' + obj.port;
Expand Down
16 changes: 8 additions & 8 deletions test/mainSpec.js
Expand Up @@ -133,13 +133,13 @@ describe('hosts', () => {
it('must recognize IPv6 addresses', () => {
expect(parse('[2001:0db8:0000:0000:0000:FF00:0042:8329]')).toEqual({
hosts: [{
name: '2001:0db8:0000:0000:0000:FF00:0042:8329',
name: '[2001:0db8:0000:0000:0000:FF00:0042:8329]',
type: 'IPv6'
}]
});
expect(parse('[2001:0db8]:123')).toEqual({
hosts: [{
name: '2001:0db8',
name: '[2001:0db8]',
port: 123,
type: 'IPv6'
}]
Expand All @@ -148,7 +148,7 @@ describe('hosts', () => {
it('must not treat IPv6 scopes as special characters', () => {
expect(parse('[2001:0db8%20]')).toEqual({
hosts: [{
name: '2001:0db8%20',
name: '[2001:0db8%20]',
type: 'IPv6'
}]
});
Expand All @@ -162,12 +162,12 @@ describe('hosts', () => {
expect(() => {
parse('[::]:1a');
}).toThrow('Invalid port: 1a');
expect(parse('[::]:abc')).toEqual({hosts: [{name: '::', type: 'IPv6'}]});
expect(parse('[::]:abc')).toEqual({hosts: [{name: '[::]', type: 'IPv6'}]});
});
it('must allow valid ports', () => {
expect(parse('[::]:1')).toEqual({hosts: [{name: '::', port: 1, type: 'IPv6'}]});
expect(parse('[::]:1/')).toEqual({hosts: [{name: '::', port: 1, type: 'IPv6'}]});
expect(parse('[::]:123?')).toEqual({hosts: [{name: '::', port: 123, type: 'IPv6'}]});
expect(parse('[::]:1')).toEqual({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]});
expect(parse('[::]:1/')).toEqual({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]});
expect(parse('[::]:123?')).toEqual({hosts: [{name: '[::]', port: 123, type: 'IPv6'}]});
});
});

Expand Down Expand Up @@ -547,6 +547,6 @@ describe('parseHost', () => {
it('must parse valid hosts', () => {
expect(parseHost('a')).toEqual({name: 'a', type: 'domain'});
expect(parseHost('a:123')).toEqual({name: 'a', port: 123, type: 'domain'});
expect(parseHost('[::]:123')).toEqual({name: '::', port: 123, type: 'IPv6'});
expect(parseHost('[::]:123')).toEqual({name: '[::]', port: 123, type: 'IPv6'});
});
});

0 comments on commit 3fc1cbc

Please sign in to comment.