https://github.com/bartektelec/easy-queryparams
🌀 Convert JS objects to query strings and vice versa
https://github.com/bartektelec/easy-queryparams
javascript parsing querystring typescript typescript-library
Last synced: 26 days ago
JSON representation
🌀 Convert JS objects to query strings and vice versa
- Host: GitHub
- URL: https://github.com/bartektelec/easy-queryparams
- Owner: bartektelec
- License: mit
- Created: 2022-02-01T18:30:06.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-30T17:40:29.000Z (over 3 years ago)
- Last Synced: 2025-02-15T20:43:16.177Z (11 months ago)
- Topics: javascript, parsing, querystring, typescript, typescript-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/easy-queryparams
- Size: 213 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# easy-queryparams [](https://www.npmjs.com/package/easy-queryparams)
[](https://npmcharts.com/compare/easy-queryparams?interval=30&minimal=true)   
This is a package that let's you easy convert an object to a querystring, or parse it the other way around. It's main difference from JS `URLSearchParams` is that it by default avoids params with nullish values.
The package exposes two methods: `stringify` and `parse`.
## Getting started
- Install this package
```bash
$ npm i easy-queryparams
```
- Import the methods in your project
```ts
import * as qs from 'easy-querystring';
```
## Examples
### Stringify method
This method takes an object as a parameter and converts it to a string, omitting all nullish valued properties.
```ts
import { stringify } from 'easy-queryparams';
const filters = {
minAge: 20,
maxAge: 50,
selected: [1, 2, 3, 4],
location: null,
};
const queryString = stringify(filters); // "minAge=20&maxAge=50&selected=1%2C2%2C3%2C4"
```
#### Splitting arrays to multiple params
You can add a option object if you want all array values to be split for multiple parameters i.e.
```ts
const queryString = stringify(filters, { splitArrays: true }); // "children=Adam&children=Eva&children=Dave"
```
### Parse method
This method takes a query string as a parameter and returns an object. All values that are separated with commas will be converted to an array, and all parsable numbers will return a number type.
```ts
import { parse } from 'easy-queryparams';
const queryString =
'pickedNumbers=1%2C30%2Cabcd%2C50%2C200&location=New%20York';
const filters = parse(queryString);
// {
// pickedNumbers: [1, 30, 'abcd', 50, 200],
// location: 'New York'
// }
```
## TypeScript Support
You can import the `.ts` files from `easy-queryparams/src`, however you will be forced to explicitly assert a type to your object when using the `parse` method, just like you would do with `JSON.parse`.
## Licence
[MIT](https://opensource.org/licenses/MIT)