Skip to content

Commit

Permalink
join repeated param values - #43
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Oct 18, 2020
1 parent 1e870a1 commit 2172a67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ export class ConnectionString {
m.forEach(s => {
const a = s.split('=');
const prop = decode(a[0]);
const val = decode(a[1]).split(',');
if (prop in params) {
throw new Error(`Parameter "${prop}" repeated.`);
if (Array.isArray(params[prop])) {
(params[prop] as string[]).push(...val);
} else {
params[prop] = [params[prop] as string, ...val];
}
} else {
params[prop] = val.length > 1 ? val : val[0];
}
const val = decode(a[1]).split(',');
params[prop] = val.length > 1 ? val : val[0];
});
this.params = params;
}
Expand Down
14 changes: 10 additions & 4 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,16 @@ describe('path', () => {
});

describe('params', () => {
it('must throw when repeated', () => {
expect(() => {
parse('?one=1&one=2');
}).to.throw('Parameter "one" repeated.');
describe('with repeated names', () => {
it('must join values into array', () => {
expect(parse('?one=1&one=2&two=3,4&two=5')).to.eql({params: {one: ['1', '2'], two: ['3', '4', '5']}});
});
it('must join csv values into array', () => {
expect(parse('?one=1&one=2,3')).to.eql({params: {one: ['1', '2', '3']}});
expect(parse('?one=1&one=2,3&one=4,5')).to.eql({params: {one: ['1', '2', '3', '4', '5']}});
expect(parse('?one=,&one=2,3&one=4,5')).to.eql({params: {one: ['', '', '2', '3', '4', '5']}});
expect(parse('?one=,&one=1,2&one=,')).to.eql({params: {one: ['', '', '1', '2', '', '']}});
});
});
it('must support lack of parameters', () => {
expect(parse('?')).to.eql({});
Expand Down

0 comments on commit 2172a67

Please sign in to comment.