Skip to content

Commit

Permalink
improving defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 26, 2018
1 parent e4d24bd commit b55e889
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
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.5.2",
"version": "0.5.3",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
17 changes: 8 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
interface IConnectionString {
interface IConnectionDefaults {
protocol?: string
user?: string
password?: string
host?: string
hostname?: string
port?: number
user?: string
password?: string
segments?: string[]
params?: { [name: string]: string }
}

export class ConnectionString implements IConnectionString {
constructor(cs: string, defaults?: IConnectionString)
export class ConnectionString {
constructor(cs: string, defaults?: IConnectionDefaults)

protocol?: string;
user?: string;
password?: string;
host?: string;
hostname?: string;
port?: number;
user?: string;
password?: string;
segments?: string[];
params?: { [name: string]: string };

build(): string;

setDefaults(defaults: IConnectionString): ConnectionString;
setDefaults(defaults: IConnectionDefaults): ConnectionString;
}
9 changes: 3 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@
if (!('protocol' in this) && isText(defaults.protocol)) {
this.protocol = trim(defaults.protocol);
}
if (!('host' in this) && isText(defaults.host)) {
this.host = trim(defaults.host);
}
if (!('hostname' in this) && isText(defaults.hostname)) {
this.hostname = trim(defaults.hostname);
}
Expand All @@ -185,7 +182,7 @@
if (defaults.params && typeof defaults.params === 'object') {
var keys = Object.keys(defaults.params);
if (keys.length) {
if (this.params) {
if (this.params && typeof(this.params) === 'object') {
for (var a in defaults.params) {
if (!(a in this.params)) {
this.params[a] = defaults.params[a];
Expand All @@ -199,7 +196,7 @@
}
}
}
if (this.port || this.hostname) {
if ('port' in this || 'hostname' in this) {
this.host = (this.hostname || '') + (this.port >= 0 ? (':' + this.port) : '');
}
return this;
Expand All @@ -213,7 +210,7 @@
return txt.replace(/^[\s]*|[\s]*$/g, '');
}

// istanbul ignore next
/* istanbul ignore next */
Number.isInteger = Number.isInteger || function (value) {
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
};
Expand Down
4 changes: 2 additions & 2 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ describe('setDefaults', function () {
it('must set the default protocol', function () {
expect(parse('').setDefaults({protocol: 'abc'})).toEqual({protocol: 'abc'});
});
it('must set the default host', function () {
expect(parse('').setDefaults({host: 'abc'})).toEqual({host: 'abc'});
it('must set the default hostname and port', function () {
expect(parse('').setDefaults({host: 'abc'})).toEqual({}); // cannot set the host directly
expect(parse('').setDefaults({hostname: 'abc'})).toEqual({host: 'abc', hostname: 'abc'});
expect(parse('').setDefaults({port: 123})).toEqual({host: ':123', port: 123});
});
Expand Down

0 comments on commit b55e889

Please sign in to comment.