Skip to content

Commit

Permalink
adding support for defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 15, 2017
1 parent 8791d71 commit 1054f99
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 31 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,22 @@ For details and examples see the [WiKi Pages].

## API

After parsing you always get a class instance with all the properties, plus method `build`,
which can construct a new connection string from the current properties.
After parsing you always get a class instance with all the properties, plus method `build([defaults])`,
which can construct a new connection string from the current properties, using an optional set of defaults.

Example:

```js
var a = new ConnectionString('abc://');
a.host = a.host || 'localhost'; // set a default host
var a = new ConnectionString('abc://localhost');

a.build(); //=> 'abc://localhost'

// using defaults:
a.build({
hostname: '127.0.0.1',
port: 12345,
user: 'tester'
}); //=> 'abc://tester@localhost:12345'
```

[WiKi Pages]:https://github.com/vitaly-t/connection-string/wiki
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.2.0",
"version": "0.3.0",
"description": "URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
15 changes: 13 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
export class ConnectionString {
interface IConnectionString {
protocol?: string
user?: string
password?: string
host?: string
hostname?: string
port?: number
segments?: string[]
params?: { [name: string]: string }
};

export class ConnectionString implements IConnectionString {
constructor(cd: string)

protocol: string;
Expand All @@ -10,5 +21,5 @@ export class ConnectionString {
segments: string[];
params: { [name: string]: string };

build(): string;
build(defaults?: IConnectionString): string;
}
51 changes: 34 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
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);
Expand Down Expand Up @@ -90,34 +90,50 @@
}
}

function build() {
function build(defaults) {
defaults = defaults || {};
var cs = {
protocol: this.protocol || defaults.protocol,
host: this.host || defaults.host,
hostname: this.hostname || defaults.hostname,
port: this.port || defaults.port,
user: this.user || defaults.user,
password: this.password || defaults.password,
segments: this.segments || defaults.segments,
params: this.params || defaults.params
};

if (cs.port || cs.hostname) {
cs.host = (cs.hostname || '') + (cs.port ? (':' + cs.port) : '');
}

var s = '';
if (this.protocol) {
s += encodeURI(this.protocol) + '://';
if (cs.protocol) {
s += encodeURI(cs.protocol) + '://';
}
if (this.user) {
s += encodeURI(this.user);
if (this.password) {
s += ':' + encodeURI(this.password);
if (cs.user) {
s += encodeURI(cs.user);
if (cs.password) {
s += ':' + encodeURI(cs.password);
}
s += '@';
} else {
if (this.password) {
s += ':' + encodeURI(this.password) + '@';
if (cs.password) {
s += ':' + encodeURI(cs.password) + '@';
}
}
if (this.host) {
s += this.host;
if (cs.host) {
s += cs.host;
}
if (Array.isArray(this.segments) && this.segments.length) {
this.segments.forEach(function (seg) {
if (Array.isArray(cs.segments) && cs.segments.length) {
cs.segments.forEach(function (seg) {
s += '/' + encodeURI(seg);
});
}
if (this.params) {
if (cs.params) {
var params = [];
for (var a in this.params) {
params.push(encodeURI(a) + '=' + encodeURI(this.params[a]));
for (var a in cs.params) {
params.push(encodeURI(a) + '=' + encodeURI(cs.params[a]));
}
if (params.length) {
s += '?' + params.join('&');
Expand All @@ -136,3 +152,4 @@
window.ConnectionString = ConnectionString;
}
})(this);

8 changes: 8 additions & 0 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ var segment1: string = a.segments[0];
var param1: string = a.params['first'];

var cs = a.build();
var cs = a.build({});

cs = a.build({
hostname: 'server',
port: 123
});

cs = a.build(new ConnectionString(''));
22 changes: 15 additions & 7 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ describe('host', function () {
it('must recognize IPv6 addresses', function () {
expect(parse('[2001:0db8:0000:0000:0000:FF00:0042:8329]')).toEqual({
host: '[2001:0db8:0000:0000:0000:FF00:0042:8329]',
hostname: '2001:0db8:0000:0000:0000:FF00:0042:8329'
hostname: '[2001:0db8:0000:0000:0000:FF00:0042:8329]'
});
expect(parse('[2001:0db8]:123')).toEqual({
host: '[2001:0db8]:123',
hostname: '2001:0db8',
hostname: '[2001:0db8]',
port: 123
});
});
it('must not treat IPv6 scopes as special characters', function () {
expect(parse('[2001:0db8%20]')).toEqual({
host: '[2001:0db8%20]',
hostname: '2001:0db8%20'
hostname: '[2001:0db8%20]'
});
});
it('must skip invalid IPv6 addresses', function () {
Expand All @@ -131,12 +131,12 @@ describe('host', function () {
it('must ignore the invalid ports', function () {
// TODO: consider adding, or not?
// expect(parse('[::]:1a')).toEqual({host: '[::]', hostname: '::'});
expect(parse('[::]:abc')).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});
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});
});
});

Expand Down Expand Up @@ -228,6 +228,9 @@ describe('params', function () {
expect(parse('://?par1=123')).toEqual({params: {par1: '123'}});
expect(parse(':///?par1=123')).toEqual({params: {par1: '123'}});
});
it('it must worl with the user only', function () {
expect(parse('user@?p1=123')).toEqual({user: 'user', params: {p1: '123'}});
});
});

describe('complex', function () {
Expand Down Expand Up @@ -297,4 +300,9 @@ describe('build', function () {
a.params = {};
expect(a.build()).toBe('');
});
it('must use default values', function () {
expect(parse('').build({port: 123})).toBe(':123');
expect(parse('').build({hostname: 'name', segments: ['one']})).toBe('name/one');
expect(parse('user@?first=').build({params: {first: 1}})).toBe('user@?first=1');
});
});

0 comments on commit 1054f99

Please sign in to comment.