diff --git a/README.md b/README.md index 7cb790d..fcf8c05 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ connection-string ================= -URL Connection String Parser - _for all browsers and Node.js versions._ +URL Connection String Parser, with fully optional syntax. [![Build Status](https://travis-ci.org/vitaly-t/connection-string.svg?branch=master)](https://travis-ci.org/vitaly-t/connection-string) [![Coverage Status](https://coveralls.io/repos/vitaly-t/connection-string/badge.svg?branch=master)](https://coveralls.io/r/vitaly-t/connection-string?branch=master) @@ -12,7 +12,7 @@ Takes a URL connection string (with every element being optional): protocol://user:password@hostname:12345/seg1/seg2?p1=val1&p2=val2 ``` -and converts it into an object: +and converts it into an object that contains only what's specified: ```js { @@ -30,7 +30,20 @@ and converts it into an object: } ``` -It only sets properties that are present in the connection string. See the [Optional Format]. +**Short-syntax examples:** + +* `localhost` => `{host: 'localhost', hostname: 'localhost'}` +* `localhost:12345` => `{host: 'localhost:12345', hostname: 'localhost', port: 12345}` +* `abc:///seg1?p1=val` => `{protocol: 'abc', segments: ['seg1'], params: {p1: 'val'}}` +* `:12345` => `{host: ':12345', port: 12345}` +* `username@` => `{user: 'username'}` +* `:pass123@` => `{password: 'pass123'}` +* `/seg1/seg2` => `{segments: ['seg1', 'seg2']}` +* `?param1=1¶m2=2` => `{params: {param1: '1', param2: '2'}}` + +For a complete list of short-syntax examples see the [Optional Format]. + +All browsers and Node.js versions are supported. ## Installing diff --git a/package.json b/package.json index ca9404e..38883c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "connection-string", - "version": "0.1.4", + "version": "0.1.5", "description": "URL Connection String Parser.", "main": "src/index.js", "typings": "src/index.d.ts", diff --git a/src/index.js b/src/index.js index 261f234..47e0d47 100644 --- a/src/index.js +++ b/src/index.js @@ -45,14 +45,14 @@ if (cs[0] !== '/') { if (cs[0] === '[') { // It is an IPv6, with [::] being the shortest possible - m = cs.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+))?)/i); + m = cs.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+)[?/]?$)?)/i); } else { // It is either IPv4 or a name - m = cs.match(/(([a-z0-9.-]*)(?::([0-9]+))?)/i); + m = cs.match(/(([a-z0-9.-]*)(?::([0-9]+)[?/]?$)?)/i); } if (m) { if (m[1]) { - result.host = m[1]; + result.host = m[1].replace(/[/?]/, ''); } if (m[2]) { result.hostname = m[2]; diff --git a/test/mainSpec.js b/test/mainSpec.js index 731fe45..77e6ce7 100644 --- a/test/mainSpec.js +++ b/test/mainSpec.js @@ -69,7 +69,7 @@ describe('password', function () { }); }); -describe('user+password', function () { +describe('user + password', function () { it('must allow skipping both', function () { expect(parse('@')).toEqual({}); expect(parse(':@')).toEqual({}); @@ -83,6 +83,10 @@ describe('host', function () { hostname: 'server' }); }); + it('must skip port endings', function () { + expect(parse('local:123/')).toEqual({host: 'local:123', hostname: 'local', port: 123}); + expect(parse('local:123?')).toEqual({host: 'local:123', hostname: 'local', port: 123}); + }); it('must not allow URL characters', function () { expect(parse('server%20')).toEqual({ host: 'server', @@ -117,6 +121,15 @@ describe('host', function () { expect(parse('[a]:123')).toEqual({}); expect(parse('[a-b-c]')).toEqual({}); }); + it('must ignore the invalid ports', function () { + expect(parse('[::]:1a')).toEqual({host: '[::]', hostname: '::'}); + expect(parse('[::]:abc')).toEqual({host: '[::]', hostname: '::'}); + }); + it('must allow valid ports', function () { + expect(parse('[::]:1')).toEqual({host: '[::]:1', hostname: '::', port: 1}); + expect(parse('[::]:1/')).toEqual({host: '[::]:1', hostname: '::', port: 1}); + expect(parse('[::]:123?')).toEqual({host: '[::]:123', hostname: '::', port: 123}); + }); }); describe('port', function () { @@ -132,6 +145,12 @@ describe('port', function () { port: 0 }); }); + it('must not allow invalid terminators', function () { + expect(parse(':12345a')).toEqual({}); + expect(parse('@:12345a')).toEqual({}); + expect(parse(':abc')).toEqual({}); + expect(parse('@:abc123')).toEqual({}); + }); }); describe('segments', function () {