Skip to content
This repository has been archived by the owner on Mar 14, 2020. It is now read-only.

Commit

Permalink
🎁 it can parse anything look like key=value now
Browse files Browse the repository at this point in the history
  • Loading branch information
vrgamespace committed Jul 8, 2017
1 parent 8e9fdb4 commit b58086f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 33 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@

# any-qs

parse any query parameters from url
parse anything look like key=value, different key=value pairs can separate with '&', '#', '?', '\\', ',', or ';'

## install

```shell
npm i -S any-qs
```

## usage
## use to parse anything look like key=value

```js
let rawStr = 'nick=yeluoqiuzhi,email=test@email.com; url=http://github.com';
/**
* @type {string} string encoded with encodedURI
*/
let encodedStr = 'nick=yeluoqiuzhi,email=test@email.com;%20url=http://github.com';
anyQs(rawStr);
anyQs(encodedStr);
/*
// two results are the same:
{
nick: 'yeluoqiuzhi',
email: 'test@email.com',
url: 'http://github.com'
}
*/
```
## use to parse url

### decodeURI

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
var params = {},
tempArr = decodeURI(url)
.replace(/\+/g, ' ')
.match(/\w+=[^&#?\/]+/g);
.match(/\w+=[^&#?\,;]+/g);
if (!tempArr) {
return {};
}
Expand Down
83 changes: 53 additions & 30 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,66 @@ const anyQs = require('./index');
const expect = require('chai').expect;

describe('any-qs', () => {
it('should parse all param in url', () => {
const url = 'https://www.baidu.com/?cid=id_34&product=%E5%A4%9A%E5%A4%9A%E6%96%87%E5%AD%97#?value=32&key=key110&system=多多测试';
const params = anyQs(url);

expect(params).to.deep.equal({
cid: 'id_34',
product: '多多文字',
value: 32,
key: 'key110',
system: '多多测试'
describe('parse url', () => {
it('should parse all param in url', () => {
const url = 'https://www.baidu.com/?cid=id_34&product=%E5%A4%9A%E5%A4%9A%E6%96%87%E5%AD%97#?value=32&key=key110&system=多多测试';
const params = anyQs(url);

expect(params).to.deep.equal({
cid: 'id_34',
product: '多多文字',
value: 32,
key: 'key110',
system: '多多测试'
});
});
});

it('should replace + with one space', () => {
const url = 'https://www.google.co.jp/?gfe_rd=cr&ei=2DVeWYrjGo3XqAH_24qQCA#newwindow=1&q=just+a+test+suit';
const params = anyQs(url);
it('should replace + with one space', () => {
const url = 'https://www.google.co.jp/?gfe_rd=cr&ei=2DVeWYrjGo3XqAH_24qQCA#newwindow=1&q=just+a+test+suit';
const params = anyQs(url);

expect(params).to.deep.equal({
gfe_rd: 'cr',
ei: '2DVeWYrjGo3XqAH_24qQCA',
newwindow: 1,
q: 'just a test suit'
expect(params).to.deep.equal({
gfe_rd: 'cr',
ei: '2DVeWYrjGo3XqAH_24qQCA',
newwindow: 1,
q: 'just a test suit'
});
});
});

it('should return empty object when match nothing', () => {
const url = 'http://www.baidu.com';
const params = anyQs(url);
it('should return empty object when match nothing', () => {
const url = 'http://www.baidu.com';
const params = anyQs(url);

expect(params).to.be.empty;
});
expect(params).to.be.empty;
});

it('should convert string to number', () => {
const url = 'http://www.baidu.com?name=yeluoqiuzhi&born=1994&age=@24&height=174.5';
const params = anyQs(url);
expect(typeof params.born).to.equal('number');
console.log(params.height);
it('should convert string to number', () => {
const url = 'http://www.baidu.com?name=yeluoqiuzhi&born=1994&age=@24&height=174.5';
const params = anyQs(url);
expect(typeof params.born).to.equal('number');
});
});

describe('parse anything look like key=value', () => {
let rawStr = 'nick=yeluoqiuzhi,email=test@email.com; url=http://github.com';
/**
* @type {string} string encoded with encodedURI
*/
let encodedStr = 'nick=yeluoqiuzhi,email=test@email.com;%20url=http://github.com';
let result = {
nick: 'yeluoqiuzhi',
email: 'test@email.com',
url: 'http://github.com'
};

it('should parse raw string', () => {
const params = anyQs(rawStr);
expect(params).to.deep.equal(result);
});

it('should parse encoded string', () => {
const params = anyQs(encodedStr);
expect(params).to.deep.equal(result);
});
});
});

0 comments on commit b58086f

Please sign in to comment.