You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.5 KiB

4 years ago
  1. # querystringify
  2. [![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/querystringify.svg?style=flat-square)](http://browsenpm.org/package/querystringify)[![Build Status](http://img.shields.io/travis/unshiftio/querystringify/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/querystringify)[![Dependencies](https://img.shields.io/david/unshiftio/querystringify.svg?style=flat-square)](https://david-dm.org/unshiftio/querystringify)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/querystringify/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/querystringify?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=unshift)
  3. A somewhat JSON compatible interface for query string parsing. This query string
  4. parser is dumb, don't expect to much from it as it only wants to parse simple
  5. query strings. If you want to parse complex, multi level and deeply nested
  6. query strings then you should ask your self. WTF am I doing?
  7. ## Installation
  8. This module is released in npm as `querystringify`. It's also compatible with
  9. `browserify` so it can be used on the server as well as on the client. To
  10. install it simply run the following command from your CLI:
  11. ```
  12. npm install --save querystringify
  13. ```
  14. ## Usage
  15. In the following examples we assume that you've already required the library as:
  16. ```js
  17. 'use strict';
  18. var qs = require('querystringify');
  19. ```
  20. ### qs.parse()
  21. The parse method transforms a given query string in to an object. Parameters
  22. without values are set to empty strings. It does not care if your query string
  23. is prefixed with a `?` or not. It just extracts the parts between the `=` and
  24. `&`:
  25. ```js
  26. qs.parse('?foo=bar'); // { foo: 'bar' }
  27. qs.parse('foo=bar'); // { foo: 'bar' }
  28. qs.parse('foo=bar&bar=foo'); // { foo: 'bar', bar: 'foo' }
  29. qs.parse('foo&bar=foo'); // { foo: '', bar: 'foo' }
  30. ```
  31. ### qs.stringify()
  32. This transforms a given object in to a query string. By default we return the
  33. query string without a `?` prefix. If you want to prefix it by default simply
  34. supply `true` as second argument. If it should be prefixed by something else
  35. simply supply a string with the prefix value as second argument:
  36. ```js
  37. qs.stringify({ foo: bar }); // foo=bar
  38. qs.stringify({ foo: bar }, true); // ?foo=bar
  39. qs.stringify({ foo: bar }, '&'); // &foo=bar
  40. qs.stringify({ foo: '' }, '&'); // &foo=
  41. ```
  42. ## License
  43. MIT