Skip to content

Commit

Permalink
Merge pull request #6 from vitaly-t/to-string
Browse files Browse the repository at this point in the history
adding host conversion
  • Loading branch information
vitaly-t committed Jul 15, 2018
2 parents 1f1008d + 4140bce commit 02d5140
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Both the root function and class `ConnectionString` take a second optional param
If it is specified, the parser will call method `setDefaults` automatically (see below).

The object returned by the parser contains all the properties as specified in the connection string,
plus two methods: `setDefaults` and `build` (see below).
plus two methods: `setDefaults` and `toString` (see below).

#### Method `setDefaults(defaults) => ConnectionString`

Expand All @@ -127,7 +127,7 @@ specified within the connection string, and returns the same object (itself).
You can make use of this method either explicitly, after constructing the class, or implicitly, by
passing the `defaults` object into the parser/constructor.

#### Method `build() => string`
#### Method `toString() => string`

Constructs and returns a connection string from all the current properties.

Expand All @@ -136,7 +136,7 @@ Example:
```js
const a = new ConnectionString('abc://localhost');
a.setDefaults({user: 'guest'});
a.build(); //=> 'abc://guest@localhost'
a.toString(); //=> 'abc://guest@localhost'
```

For any parameter within `params`, if the value is not a string, it will be converted into a JSON
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.7.0",
"version": "0.7.1",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface IHost {
name?: string
port?: number
isIPv6?: boolean
toString?: () => string
}

interface IConnectionDefaults {
Expand All @@ -23,7 +24,7 @@ export class ConnectionString {
segments?: string[];
params?: { [name: string]: string };

build(): string;
toString(): string;

setDefaults(defaults: IConnectionDefaults): ConnectionString;
}
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
if (!this.hosts) {
this.hosts = [];
}
Object.defineProperty(h, 'toString', {
value: fullHostName.bind(null, h)
});
this.hosts.push(h);
}
}
Expand Down Expand Up @@ -124,7 +127,7 @@
}
}

function build() {
function toString() {
var s = '';
if (this.protocol) {
s += encode(this.protocol) + '://';
Expand Down Expand Up @@ -193,6 +196,9 @@
if (port > 0 && port < 65536) {
obj.port = port;
}
Object.defineProperty(obj, 'toString', {
value: fullHostName.bind(null, obj)
});
hosts.push(obj);
}
});
Expand Down Expand Up @@ -259,7 +265,7 @@
return (typeof str1 === 'string' && typeof str2 === 'string') && str1.toUpperCase() === str2.toUpperCase();
}

Object.defineProperty(ConnectionString.prototype, 'build', {value: build});
Object.defineProperty(ConnectionString.prototype, 'toString', {value: toString});
Object.defineProperty(ConnectionString.prototype, 'setDefaults', {value: setDefaults});

ConnectionString.ConnectionString = ConnectionString; // to make it more TypeScript-friendly
Expand Down
4 changes: 2 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ a.params = {
first: '123',
second: 'hello!'
};
var cs = a.build();
var cs = a.toString();
a.setDefaults({
hosts: [
{ name: '[::]', port: 123, isIPv6: true }
Expand All @@ -26,5 +26,5 @@ a.setDefaults({
user: '',
password: ''
});
cs = a.build();
cs = a.toString();
var qq = a.setDefaults(new src_1.ConnectionString(''));
4 changes: 2 additions & 2 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ a.params = {
second: 'hello!'
};

let cs = a.build();
let cs = a.toString();
a.setDefaults({
hosts: [
{name: '[::]', port: 123, isIPv6: true}
Expand All @@ -32,6 +32,6 @@ a.setDefaults({
password: ''
});

cs = a.build();
cs = a.toString();

const qq: ConnectionString = a.setDefaults(new ConnectionString(''));
30 changes: 15 additions & 15 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function parse(cs, defaults) {
}

function create(obj) {
return (new ConnectionString('', obj)).build();
return (new ConnectionString('', obj)).toString();
}

describe('init', () => {
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('complex', () => {
});
});

describe('build', () => {
describe('toString', () => {
it('must encode protocol', () => {
expect(create({protocol: 'abc 123?456'})).toBe('abc%20123%3F456://');
});
Expand All @@ -310,47 +310,47 @@ describe('build', () => {
expect(create({password: 'pass 1?2'})).toBe(':pass%201%3F2@');
});
it('must support user + password', () => {
expect(parse('user:pass@').build()).toBe('user:pass@');
expect(parse('user:pass@').toString()).toBe('user:pass@');
});
it('must encode non-IPv6 host name', () => {
expect(parse('one%20two%20three!').build()).toBe('one%20two%20three');
expect(parse('one%20two%20three!').toString()).toBe('one%20two%20three');
});
it('must not encode IPv6 host name', () => {
expect(parse('[123::%20:%20:456]').build()).toBe('[123::%20:%20:456]');
expect(parse('[123::%20:%20:456]').toString()).toBe('[123::%20:%20:456]');
});
it('must support solo hostname', () => {
expect(parse('server').build()).toBe('server');
expect(parse('[123::]').build()).toBe('[123::]');
expect(parse('server').toString()).toBe('server');
expect(parse('[123::]').toString()).toBe('[123::]');
});
it('must support solo port', () => {
expect(parse(':123').build()).toBe(':123');
expect(parse(':123').toString()).toBe(':123');
});
it('must support hostname + port', () => {
expect(parse('server:123').build()).toBe('server:123');
expect(parse('[::]:123').build()).toBe('[::]:123');
expect(parse('server:123').toString()).toBe('server:123');
expect(parse('[::]:123').toString()).toBe('[::]:123');
});
it('must encode segments', () => {
expect(parse('/a%20b').build()).toBe('/a%20b');
expect(parse('/a/b%20/c').build()).toBe('/a/b%20/c');
expect(parse('/a%20b').toString()).toBe('/a%20b');
expect(parse('/a/b%20/c').toString()).toBe('/a/b%20/c');
});
it('must ignore empty segment list', () => {
const a = parse('');
a.segments = [];
expect(a.build()).toBe('');
expect(a.toString()).toBe('');
});
it('must encode params', () => {
const obj = {
text: 1,
values: ['one', true]
};
const a = parse('', {params: {value1: obj, value2: 'text'}});
const b = parse(a.build());
const b = parse(a.toString());
expect(JSON.parse(b.params.value1)).toEqual(obj);
});
it('must ignore empty parameter list', () => {
const a = parse('');
a.params = {};
expect(a.build()).toBe('');
expect(a.toString()).toBe('');
});
});

Expand Down

0 comments on commit 02d5140

Please sign in to comment.