Skip to content

Commit

Permalink
Add sort method, and publish v1.2.0. #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybendy committed Apr 20, 2017
1 parent fe4badf commit 50a9fcd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ search.set("id", 2);
search.toString();
```

### sort

```javascript
search.sort();
```

### forEach

```javascript
Expand Down
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@
return query.join('&');
};


/**
* Sort all name-value pairs
*/
prototype.sort = function () {
var dict = this[__URLSearchParams__], keys = [], k, i, ret = {};
for (k in dict) {
keys.push(k);
}
keys.sort();
for (i = 0; i < keys.length; i ++) {
ret[keys[i]] = dict[keys[i]];
}
this[__URLSearchParams__] = ret;
};


/**
* Returns an iterator allowing to go through all keys of
* the key/value pairs contained in this object.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "url-search-params-polyfill",
"version": "1.1.0",
"version": "1.2.0",
"description": "a simple polyfill for javascript URLSearchParams",
"homepage": "https://github.com/jerrybendy/url-search-params-polyfill",
"main": "index.js",
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ describe('Iterator', function () {

});


describe('Sort', function () {
it ('Sort keys', function () {
var obj = new URLSearchParams('q=flag&key=hello&s=world');
obj.sort();
expect(obj.toString()).to.be.equal('key=hello&q=flag&s=world');
});
});


describe('Others', function () {

var testObj = {
Expand Down

0 comments on commit 50a9fcd

Please sign in to comment.