Skip to content
This repository was archived by the owner on Jan 3, 2019. It is now read-only.

Commit b66426f

Browse files
Merge pull request #12 from dartess/master
added trim leading "?" in init string, parameters without values and tests
2 parents a7de66a + 774ee42 commit b66426f

7 files changed

+58
-4
lines changed

build/url-search-params.amd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/url-search-params.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/url-search-params.max.amd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function decode(str) {
3434
function URLSearchParams(query) {
3535
this[secret] = Object.create(null);
3636
if (!query) return;
37+
if (query.charAt(0) === '?') {
38+
query = query.slice(1);
39+
}
3740
for (var
3841
index, value,
3942
pairs = (query || '').split('&'),
@@ -47,6 +50,11 @@ function URLSearchParams(query) {
4750
decode(value.slice(0, index)),
4851
decode(value.slice(index + 1))
4952
);
53+
} else if (value.length){
54+
this.append(
55+
decode(value),
56+
''
57+
);
5058
}
5159
}
5260
}

build/url-search-params.max.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function decode(str) {
3434
function URLSearchParams(query) {
3535
this[secret] = Object.create(null);
3636
if (!query) return;
37+
if (query.charAt(0) === '?') {
38+
query = query.slice(1);
39+
}
3740
for (var
3841
index, value,
3942
pairs = (query || '').split('&'),
@@ -47,6 +50,11 @@ function URLSearchParams(query) {
4750
decode(value.slice(0, index)),
4851
decode(value.slice(index + 1))
4952
);
53+
} else if (value.length){
54+
this.append(
55+
decode(value),
56+
''
57+
);
5058
}
5159
}
5260
}

build/url-search-params.node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ function decode(str) {
3333
function URLSearchParams(query) {
3434
this[secret] = Object.create(null);
3535
if (!query) return;
36+
if (query.charAt(0) === '?') {
37+
query = query.slice(1);
38+
}
3639
for (var
3740
index, value,
3841
pairs = (query || '').split('&'),
@@ -46,6 +49,11 @@ function URLSearchParams(query) {
4649
decode(value.slice(0, index)),
4750
decode(value.slice(index + 1))
4851
);
52+
} else if (value.length){
53+
this.append(
54+
decode(value),
55+
''
56+
);
4957
}
5058
}
5159
}

src/url-search-params.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function decode(str) {
1111
function URLSearchParams(query) {
1212
this[secret] = Object.create(null);
1313
if (!query) return;
14+
if (query.charAt(0) === '?') {
15+
query = query.slice(1);
16+
}
1417
for (var
1518
index, value,
1619
pairs = (query || '').split('&'),
@@ -24,6 +27,11 @@ function URLSearchParams(query) {
2427
decode(value.slice(0, index)),
2528
decode(value.slice(index + 1))
2629
);
30+
} else if (value.length){
31+
this.append(
32+
decode(value),
33+
''
34+
);
2735
}
2836
}
2937
}

test/url-search-params.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,31 @@ wru.test([
99
wru.assert(/^function|object$/.test(typeof URLSearchParams));
1010
}
1111
}, {
12-
name: 'basics',
12+
name: 'basics (including parameters without values)',
1313
test: function () {
14-
var usp = new URLSearchParams('a=1&b=2');
14+
var usp = new URLSearchParams('a=1&b=2&c');
15+
wru.assert('has keys', usp.has('a') && usp.has('b') && usp.has('c'));
16+
wru.assert('a returns right value', usp.get('a') === '1');
17+
wru.assert('b returns right value', usp.get('b') === '2');
18+
wru.assert('c returns right value', usp.get('c') === '');
19+
wru.assert('a getAll returns right value', usp.getAll('a').join(',') === '1');
20+
wru.assert('b getAll returns right value', usp.getAll('b').join(',') === '2');
21+
wru.assert('c getAll returns right value', usp.getAll('c').join(',') === '');
22+
usp.append('a', '3');
23+
wru.assert('append adds values', usp.getAll('a').join(',') === '1,3');
24+
wru.assert('append preserves get', usp.get('a') === '1');
25+
wru.assert('append does not affect others', usp.getAll('b').join(',') === '2' && usp.getAll('c').join(',') === '');
26+
usp.set('a', '4');
27+
wru.assert('set overwrites known values', usp.getAll('a').join(',') === '4');
28+
usp['delete']('a');
29+
wru.assert('usp can delete', usp.has('a') === false);
30+
wru.assert('usp can return null', usp.get('a') === null);
31+
wru.assert('usp to string works as expected', usp.toString() === 'b=2&c=');
32+
}
33+
}, {
34+
name: 'basics with leading "?"',
35+
test: function () {
36+
var usp = new URLSearchParams('?a=1&b=2');
1537
wru.assert('has keys', usp.has('a') && usp.has('b'));
1638
wru.assert('a returns right value', usp.get('a') === '1');
1739
wru.assert('b returns right value', usp.get('b') === '2');

0 commit comments

Comments
 (0)