diff --git a/README.md b/README.md index 428efaa..036ba71 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ href(function (href) { }) ``` +### qs +Sometimes [query +strings](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search) +must be decoded. In order to do this, the `./qs.js` file is included. +```js +const qs = require('qs') +qs('https://www.npmjs.com/search?q=query+string') +// => { q: 'query+string' } +``` + ### virtual-dom example ```js const render = require('virtual-dom/create-element') diff --git a/qs.js b/qs.js new file mode 100644 index 0000000..0c907b9 --- /dev/null +++ b/qs.js @@ -0,0 +1,15 @@ +const reg = new RegExp('([^?=&]+)(=([^&]*))?', 'g') +const decodeURIComponent = window.decodeURIComponent + +module.exports = qs + +// decode a uri into a kv representation :: str -> obj +function qs (uri) { + const obj = {} + uri.replace(/^.*\?/, '').replace(reg, map) + return obj + + function map (a0, a1, a2, a3) { + obj[decodeURIComponent(a1)] = decodeURIComponent(a3) + } +}