From b58086fdc9c67047194977e357e006d350fde860 Mon Sep 17 00:00:00 2001 From: yeluoqiuzhi <827884374@qq.com> Date: Sat, 8 Jul 2017 16:46:24 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20=20it=20can=20parse=20anything?= =?UTF-8?q?=20look=20like=20key=3Dvalue=20now?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++++-- index.js | 2 +- test.js | 83 +++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 5c5bf6a..a47ad62 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # any-qs -parse any query parameters from url +parse anything look like key=value, different key=value pairs can separate with '&', '#', '?', '\\', ',', or ';' ## install @@ -11,7 +11,27 @@ parse any query parameters from url 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 diff --git a/index.js b/index.js index 2cb9700..64a59c6 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ var params = {}, tempArr = decodeURI(url) .replace(/\+/g, ' ') - .match(/\w+=[^&#?\/]+/g); + .match(/\w+=[^&#?\,;]+/g); if (!tempArr) { return {}; } diff --git a/test.js b/test.js index 1be3697..245f85d 100644 --- a/test.js +++ b/test.js @@ -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); + }); + }); }); \ No newline at end of file