From 3fc1cbc2b07a5631d06b6acbc485d728c0f3be14 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 17 Mar 2019 03:30:39 +0000 Subject: [PATCH] docs --- README.md | 20 ++++++++++---------- package.json | 2 +- src/index.js | 10 ++-------- test/mainSpec.js | 16 ++++++++-------- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c42b1a3..9e49c7e 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ and converts it into an object that contains only what's specified: user: 'user', password: 'password', hosts: [ - {name: 'host1', port: 123, isIPv6: false}, - {name: 'abcd::', port: 456, isIPv6: true} + {name: 'host1', port: 123, type: 'domain'}, + {name: '[abcd::]', port: 456, type: 'IPv6'} ], path: ['one', 'two'], params: { @@ -47,9 +47,9 @@ Unlike the default URL parser, this one supports the following: **Short-syntax examples:** -* `localhost` => `{hosts: [{name: 'localhost', isIPv6: false}]` -* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345, isIPv6: false}]` -* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123, isIPv6: true}]` +* `localhost` => `{hosts: [{name: 'localhost', type: 'domain'}]` +* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345, type: 'domain'}]` +* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123, type: 'IPv6'}]` * `abc:///one?p1=val` => `{protocol: 'abc', path: ['one'], params: {p1: 'val'}}` * `:12345` => `{hosts: [{port: 12345}]` * `username@` => `{user: 'username'}` @@ -75,7 +75,7 @@ $ npm install connection-string const parse = require('connection-string'); const obj = parse('my-server:12345'); -//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]} +//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]} ``` or as a class: @@ -84,7 +84,7 @@ or as a class: const ConnectionString = require('connection-string'); const obj = new ConnectionString('my-server:12345'); -//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]} +//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]} ``` * **Browsers** @@ -103,7 +103,7 @@ const obj = new ConnectionString('my-server:12345'); import {ConnectionString} from 'connection-string' const a = new ConnectionString('my-server:12345'); -//=> {hosts: [{name: 'my-server', port: 12345, isIPv6: false}]} +//=> {hosts: [{name: 'my-server', port: 12345, type: 'domain'}]} ``` See also [WiKi Pages] for more examples and documentation. @@ -161,14 +161,14 @@ is typically not needed. But if you do need `$` encoded everywhere, pass in `{en * `plusForSpace` - Boolean (false), it is used only for parameter values, to encode spaces as `+` instead of `%20`. -### `static parseHost(host) => {name,port,isIPv6} | null` +### `static parseHost(host) => {name,port,type} | null` When using an external list of default hosts, you may need to parse them independently, using this method, so they can be correctly processed by method `setDefaults`. ```js const h = ConnectionString.parseHost('[abcd::]:111'); -//=> {name: 'abcd::', port: 111, isIPv6: true} +//=> {name: 'abcd::', port: 111, type: 'IPv6'} const a = new ConnectionString('test://localhost:222/dbname', {hosts: [h]}); a.toString(); diff --git a/package.json b/package.json index b661305..454f2a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "connection-string", - "version": "1.2.0", + "version": "2.0.0", "description": "Advanced URL Connection String parser.", "main": "src/index.js", "typings": "src/index.d.ts", diff --git a/src/index.js b/src/index.js index 1ad85d8..c0fe5f1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,3 @@ -// TODO: setDefaults issue accepting :: or [::], inconsistent with the host parsing. - (function (window) { 'use strict'; @@ -123,7 +121,7 @@ var m, isIPv6; if (host[0] === '[') { // This is 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 domain/socket @@ -310,11 +308,7 @@ options = options || {}; var a = ''; if (obj.name) { - if (obj.type === hostType.IPv6) { - a = '[' + obj.name + ']'; - } else { - a = encode(obj.name, options); - } + a = obj.type === hostType.IPv6 ? obj.name : encode(obj.name, options); } if (obj.port) { a += ':' + obj.port; diff --git a/test/mainSpec.js b/test/mainSpec.js index bd0d7ab..acc6e3f 100644 --- a/test/mainSpec.js +++ b/test/mainSpec.js @@ -133,13 +133,13 @@ describe('hosts', () => { it('must recognize IPv6 addresses', () => { expect(parse('[2001:0db8:0000:0000:0000:FF00:0042:8329]')).toEqual({ hosts: [{ - name: '2001:0db8:0000:0000:0000:FF00:0042:8329', + name: '[2001:0db8:0000:0000:0000:FF00:0042:8329]', type: 'IPv6' }] }); expect(parse('[2001:0db8]:123')).toEqual({ hosts: [{ - name: '2001:0db8', + name: '[2001:0db8]', port: 123, type: 'IPv6' }] @@ -148,7 +148,7 @@ describe('hosts', () => { it('must not treat IPv6 scopes as special characters', () => { expect(parse('[2001:0db8%20]')).toEqual({ hosts: [{ - name: '2001:0db8%20', + name: '[2001:0db8%20]', type: 'IPv6' }] }); @@ -162,12 +162,12 @@ describe('hosts', () => { expect(() => { parse('[::]:1a'); }).toThrow('Invalid port: 1a'); - expect(parse('[::]:abc')).toEqual({hosts: [{name: '::', type: 'IPv6'}]}); + expect(parse('[::]:abc')).toEqual({hosts: [{name: '[::]', type: 'IPv6'}]}); }); it('must allow valid ports', () => { - expect(parse('[::]:1')).toEqual({hosts: [{name: '::', port: 1, type: 'IPv6'}]}); - expect(parse('[::]:1/')).toEqual({hosts: [{name: '::', port: 1, type: 'IPv6'}]}); - expect(parse('[::]:123?')).toEqual({hosts: [{name: '::', port: 123, type: 'IPv6'}]}); + expect(parse('[::]:1')).toEqual({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]}); + expect(parse('[::]:1/')).toEqual({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]}); + expect(parse('[::]:123?')).toEqual({hosts: [{name: '[::]', port: 123, type: 'IPv6'}]}); }); }); @@ -547,6 +547,6 @@ describe('parseHost', () => { it('must parse valid hosts', () => { expect(parseHost('a')).toEqual({name: 'a', type: 'domain'}); expect(parseHost('a:123')).toEqual({name: 'a', port: 123, type: 'domain'}); - expect(parseHost('[::]:123')).toEqual({name: '::', port: 123, type: 'IPv6'}); + expect(parseHost('[::]:123')).toEqual({name: '[::]', port: 123, type: 'IPv6'}); }); });