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 25, 2018
1 parent 85d7c75 commit 9ddb0e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 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.0",
"version": "0.5.1",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
22 changes: 12 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var encode = encodeURIComponent;
var decode = decodeURIComponent;
var invalidDefaults = 'Invalid \'defaults\' parameter!';

function ConnectionString(cs, defaults) {

Expand All @@ -15,7 +16,7 @@
}

if (defaults !== undefined && defaults !== null && typeof defaults !== 'object') {
throw new TypeError('Invalid \'defaults\' parameter!');
throw new TypeError(invalidDefaults);
}

// remove all trailing space symbols:
Expand Down Expand Up @@ -154,27 +155,28 @@

function setDefaults(defaults) {
if (!defaults || typeof defaults !== 'object') {
throw new TypeError('Invalid \'defaults\' parameter!');
throw new TypeError(invalidDefaults);
}
if (!this.protocol && isText(defaults.protocol)) {
if (!('protocol' in this) && isText(defaults.protocol)) {
this.protocol = trim(defaults.protocol);
}
if (!this.host && isText(defaults.host)) {
if (!('host' in this) && isText(defaults.host)) {
this.host = trim(defaults.host);
}
if (!this.hostname && isText(defaults.hostname)) {
if (!('hostname' in this) && isText(defaults.hostname)) {
this.hostname = trim(defaults.hostname);
}
if (!this.port && defaults.port > 0) {
this.port = parseInt(defaults.port);
var p = defaults.port;
if (!('port' in this) && Number.isInteger(p) && p >= 0 && p <= 65535) {
this.port = p;
}
if (!this.user && isText(defaults.user)) {
if (!('user' in this) && isText(defaults.user)) {
this.user = trim(defaults.user);
}
if (!this.password && isText(defaults.password)) {
if (!('password' in this) && isText(defaults.password)) {
this.password = trim(defaults.password);
}
if (!this.segments && Array.isArray(defaults.segments)) {
if (!('segments' in this) && Array.isArray(defaults.segments)) {
var s = defaults.segments.filter(isText);
if (s.length) {
this.segments = s;
Expand Down
5 changes: 2 additions & 3 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,11 @@ describe('setDefaults', function () {
expect(parse('').setDefaults({host: 'abc'})).toEqual({host: 'abc'});
expect(parse('').setDefaults({hostname: 'abc'})).toEqual({host: 'abc', hostname: 'abc'});
expect(parse('').setDefaults({port: 123})).toEqual({host: ':123', port: 123});
expect(parse('').setDefaults({port: '123'})).toEqual({host: ':123', port: 123});
});
it('must ignore invalid ports', function () {
expect(parse('').setDefaults({port: ''})).toEqual({});
expect(parse('').setDefaults({port: '123'})).toEqual({});
expect(parse('').setDefaults({port: 'a'})).toEqual({});
expect(parse('').setDefaults({port: 0})).toEqual({});
expect(parse('').setDefaults({port: -1})).toEqual({});
expect(parse('').setDefaults({port: '0'})).toEqual({});
});
it('must set the default user', function () {
Expand Down

0 comments on commit 9ddb0e7

Please sign in to comment.