diff --git a/package.json b/package.json index de9443b..518c497 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 4f5b8d2..862497f 100644 --- a/src/index.js +++ b/src/index.js @@ -231,9 +231,10 @@ 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; @@ -241,18 +242,18 @@ } 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; } diff --git a/test/mainSpec.js b/test/mainSpec.js index 0ff292f..59c80dd 100644 --- a/test/mainSpec.js +++ b/test/mainSpec.js @@ -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({});