Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 24, 2018
1 parent f45d0ad commit 9ac739c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Unlike the standard URL parser, this one supports the following:

* Fully optional syntax for every element in the connection string
* Configuration of defaults for any parameter that's missing
* Construction of a connection string from all parameters
* Friendlier access to the URL's segments and parameters
* TypeScript declarations are deployed with the library

Expand Down Expand Up @@ -68,14 +69,22 @@ $ npm install connection-string

```js
const parse = require('connection-string');
const obj = parse('my-server:12345');

const obj1 = parse('my-server:12345');

// with a default:
const obj2 = parse('my-server:12345', {user: 'admin'});
```

or as a class:

```js
const ConnectionString = require('connection-string');
const obj = new ConnectionString('my-server:12345');

const obj1 = new ConnectionString('my-server:12345');

// with a default:
const obj2 = new ConnectionString('my-server:12345', {user: 'admin'});
```

* **Browsers**
Expand Down Expand Up @@ -116,7 +125,7 @@ passing `defaults` into the parser/constructor.

#### Method `build() => string`

Constructs and returns the connection string from all the current properties.
Constructs and returns a connection string from all the current properties.

Example:

Expand All @@ -126,5 +135,8 @@ a.setDefaults({user: 'guest', port: 123});
a.build(); //=> 'abc://guest@localhost:123'
```

For any parameter within `params`, if the value is not a string, it will be converted into a JSON
string first, and then URL-encrypted.

[WiKi Pages]:https://github.com/vitaly-t/connection-string/wiki
[Optional Format]:https://github.com/vitaly-t/connection-string/wiki#optional-format
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": "0.4.2",
"version": "0.5.0",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
var params = [];
for (var a in this.params) {
var value = this.params[a];
// TODO: This needs some good testing, including the reverse parsing:
if (typeof value !== 'string') {
value = JSON.stringify(value);
}
Expand Down Expand Up @@ -175,14 +174,12 @@
if (!this.password && isText(defaults.password)) {
this.password = trim(defaults.password);
}
// TODO: This needs more test coverage:
if (!this.segments && Array.isArray(defaults.segments)) {
var s = defaults.segments.filter(isText);
if (s.length) {
this.segments = s;
}
}
// TODO: This needs more test coverage:
if (defaults.params && typeof defaults.params === 'object') {
var keys = Object.keys(defaults.params);
if (keys.length) {
Expand Down
24 changes: 22 additions & 2 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ describe('build', function () {
expect(a.build()).toBe('');
});
it('must encode params', function () {
expect(parse('?a%20b=1').build()).toBe('?a%20b=1');
expect(parse('?a=1&b%20=2').build()).toBe('?a=1&b%20=2');
var obj = {
text: 1,
values: ['one', true]
};
var a = parse('', {params: {value1: obj, value2: 'text'}});
var b = parse(a.build());
expect(JSON.parse(b.params.value1)).toEqual(obj);
});
it('must ignore empty parameter list', function () {
var a = parse('');
Expand Down Expand Up @@ -354,5 +359,20 @@ describe('setDefaults', function () {
it('must set the default params', function () {
expect(parse('').setDefaults({params: {p1: 'abc'}})).toEqual({params: {p1: 'abc'}});
});
it('must skip empty params', function () {
expect(parse('').setDefaults({params: {}})).toEqual({});
});
it('must merge params', function () {
expect(parse('?value1=1').setDefaults({params: {value1: 0, value2: 2}})).toEqual({
params: {
value1: '1',
value2: 2
}
});
});
it('must ignore empty segments', function () {
expect(parse('').setDefaults({segments: ['', 123, true, ' ']})).toEqual({});
expect(parse('').setDefaults({segments: 123})).toEqual({});
});

});

0 comments on commit 9ac739c

Please sign in to comment.