Skip to content

Commit

Permalink
trim default host names
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 10, 2019
1 parent ac79332 commit f2c9e7d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 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": "2.6.1",
"version": "2.6.2",
"description": "Advanced URL Connection String parser + generator.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,29 @@
return d && typeof d === 'object';
})
.forEach(function (dh) {
var h = {name: isText(dh.name) ? dh.name.trim() : null, port: dh.port, type: dh.type};
var found = false;
for (var i = 0; i < hosts.length; i++) {
var thisHost = fullHostName(hosts[i]), defHost = fullHostName(dh);
var thisHost = fullHostName(hosts[i]), defHost = fullHostName(h);
if (thisHost.toLowerCase() === defHost.toLowerCase()) {
found = true;
break;
}
}
if (!found) {
var obj = {};
if (isText(dh.name)) {
obj.name = dh.name;
if (dh.type && dh.type in hostType) {
obj.type = dh.type;
if (h.name) {
obj.name = h.name;
if (h.type && h.type in hostType) {
obj.type = h.type;
} else {
var t = parseHost(dh.name);
var t = parseHost(h.name);
if (t) {
obj.type = t.type;
}
}
}
var p = dh.port;
var p = h.port;
if (typeof p === 'number' && p > 0 && p < 65536) {
obj.port = p;
}
Expand Down
8 changes: 8 additions & 0 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ describe('setDefaults', () => {
expect(parse('').setDefaults({hosts: [{name: 'abc'}]})).toEqual({hosts: [{name: 'abc', type: 'domain'}]});
expect(parse('').setDefaults({hosts: [{port: 123}]})).toEqual({hosts: [{port: 123}]});
});
it('must ignore trailing spaces for host names', () => {
expect(parse('one').setDefaults({hosts: [{name: ' one '}]})).toEqual({
hosts: [{name: 'one', type: 'domain'}]
});
expect(parse('one').setDefaults({hosts: [{name: ' \t\ttwo\r\n\t '}]})).toEqual({
hosts: [{name: 'one', type: 'domain'}, {name: 'two', type: 'domain'}]
});
});
it('must ignore invalid ports', () => {
expect(parse('').setDefaults({hosts: [{port: '123'}]})).toEqual({});
expect(parse('').setDefaults({hosts: [{port: 'a'}]})).toEqual({});
Expand Down

0 comments on commit f2c9e7d

Please sign in to comment.