From 9d39573988226a72c249eb344d38922c9d2c02da Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Fri, 13 Jul 2018 17:13:11 +0100 Subject: [PATCH] finished implementation --- .travis.yml | 4 +++- package.json | 8 ++++---- src/index.js | 21 ++++++++++++--------- test/mainSpec.js | 23 +++++++++++++++++++---- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3740db3..975386f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,10 @@ matrix: - node_js: "0.12" script: "npm test" - node_js: "4" - script: "npm run travis" + script: "npm test" - node_js: "6" script: "npm run travis" - node_js: "8" script: "npm run travis" + - node_js: "10" + script: "npm run travis" diff --git a/package.json b/package.json index 9140ddc..535cd0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "connection-string", - "version": "0.5.3", + "version": "0.6.0", "description": "Advanced URL Connection String Parser.", "main": "src/index.js", "typings": "src/index.d.ts", @@ -38,9 +38,9 @@ "npm": ">=1.4" }, "devDependencies": { - "coveralls": "3.0.1", - "eslint": "4.19.1", + "coveralls": "3.0.2", + "eslint": "5.1.0", "istanbul": "0.4.5", - "jasmine-node": "1.14.5" + "jasmine-node": "1.15.0" } } diff --git a/src/index.js b/src/index.js index 1012ed4..3f26dfb 100644 --- a/src/index.js +++ b/src/index.js @@ -64,11 +64,11 @@ if (host[0] === '[') { // It is an IPv6, with [::] being the shortest possible - m = host.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+[^/?]*))?)/i); + m = host.match(/(\[([0-9a-z:%]{2,45})](?::(-?[0-9]+[^/?]*))?)/i); isIPv6 = true; } else { // It is either IPv4 or a name - m = host.match(/(([a-z0-9%.-]*)(?::([0-9]+[^/?]*))?)/i); + m = host.match(/(([a-z0-9%.-]*)(?::(-?[0-9]+[^/?]*))?)/i); } if (m) { if (m[2]) { @@ -76,7 +76,7 @@ } if (m[3]) { var p = m[3], port = parseInt(p); - if (port > 0 && port <= 65535 && port.toString() === p) { + if (port > 0 && port < 65536 && port.toString() === p) { h.port = port; } else { throw new Error('Invalid port: ' + p); @@ -183,22 +183,21 @@ var hosts = Array.isArray(this.hosts) ? this.hosts : []; var obj, found; defaults.hosts.forEach(function (dh) { - // TODO: Plus add ignoring case ;) found = false; for (var i = 0; i < hosts.length; i++) { - if (hosts[i].name === dh.name && hosts[i].port === dh.port) { + if (equalStrings(hosts[i].name, dh.name) && hosts[i].port === dh.port) { found = true; break; } } - // TODO: Should do better than just logical check here: if (!found) { obj = {}; - if (dh.name) { + if (isText(dh.name)) { obj.name = dh.name; } - if (dh.port) { - obj.port = dh.port; + var port = parseInt(dh.port); + if (port > 0 && port < 65536) { + obj.port = port; } hosts.push(obj); } @@ -247,6 +246,10 @@ return txt.replace(/^[\s]*|[\s]*$/g, ''); } + function equalStrings(str1, str2) { + return (typeof str1 === 'string' && typeof str2 === 'string') && str1.toUpperCase() === str2.toUpperCase(); + } + /* istanbul ignore next */ Number.isInteger = Number.isInteger || function (value) { return typeof value === 'number' && isFinite(value) && Math.floor(value) === value; diff --git a/test/mainSpec.js b/test/mainSpec.js index 7a1f52e..8a12b58 100644 --- a/test/mainSpec.js +++ b/test/mainSpec.js @@ -140,14 +140,18 @@ describe('port', function () { {port: 12345}] }); }); - it('must not allow port=0', function () { + it('must not allow 0 or negative ports', function () { expect(function () { parse(':0'); }).toThrow('Invalid port: 0'); + expect(function () { + parse(':-1'); + }).toThrow('Invalid port: -1'); }); it('must not allow invalid terminators', function () { - //expect(parse(':12345a')).toEqual({}); - //expect(parse('@:12345a')).toEqual({}); + expect(function () { + parse(':12345a'); + }).toThrow('Invalid port: 12345a'); expect(parse(':abc')).toEqual({}); expect(parse('@:abc123')).toEqual({}); }); @@ -303,6 +307,9 @@ describe('build', function () { it('must support solo hostname', function () { expect(parse('server').build()).toBe('server'); }); + it('must support solo port', function () { + expect(parse(':123').build()).toBe(':123'); + }); it('must support hostname + port', function () { expect(parse('server:123').build()).toBe('server:123'); }); @@ -345,7 +352,15 @@ describe('setDefaults', function () { expect(parse('').setDefaults({protocol: 'abc'})).toEqual({protocol: 'abc'}); }); it('must set the default hostname and port', function () { - expect(parse('').setDefaults({host: 'abc'})).toEqual({}); // cannot set the host directly + expect(parse('my-host').setDefaults({hosts: [{name: 'abc'}]})).toEqual({hosts: [{name: 'my-host'}, {name: 'abc'}]}); + expect(parse('my-host').setDefaults({hosts: [{name: 'my-host'}]})).toEqual({hosts: [{name: 'my-host'}]}); + expect(parse('my-host').setDefaults({hosts: [{name: 'my-host', port: 222}]})).toEqual({ + hosts: [{name: 'my-host'}, { + name: 'my-host', + port: 222 + }] + }); + expect(parse(':111').setDefaults({hosts: [{port: 123}]})).toEqual({hosts: [{port: 111}, {port: 123}]}); expect(parse('').setDefaults({hosts: [{name: 'abc'}]})).toEqual({hosts: [{name: 'abc'}]}); expect(parse('').setDefaults({hosts: [{port: 123}]})).toEqual({hosts: [{port: 123}]}); });