# query-string-manipulator

> Manipulate query strings without effort

Latest version **1.2.2** (published 2019-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install query-string-manipulator
pnpm add query-string-manipulator
yarn add query-string-manipulator
bun add query-string-manipulator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2019-12-12 |
| First published | 2018-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Pavel Žák |
| Maintainers | just-paja |
| Keywords | query, url, param, parameter, modify, manipulate, set |

## Links

- npm: https://www.npmjs.com/package/query-string-manipulator
- Repository: https://github.com/BerryCloud/js-query-string-manipulator
- Homepage: https://github.com/BerryCloud/js-query-string-manipulator#readme
- Issues: https://github.com/BerryCloud/js-query-string-manipulator/issues
- npm.io page: https://npm.io/package/query-string-manipulator

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2019-12-12
- 1.2.1 — 2019-12-12
- 1.2.0 — 2019-07-31
- 1.1.2 — 2019-06-06
- 1.1.1 — 2019-06-06
- 1.1.0 — 2019-06-06
- 1.0.4 — 2018-03-15
- 1.0.3 — 2018-03-07
- 1.0.2 — 2018-03-01
- 1.0.1 — 2018-01-10
- 1.0.0 — 2018-01-10

## README

# Query String Manipulator

[![CircleCI](https://circleci.com/gh/BerryCloud/js-query-string-manipulator.svg?style=shield)](https://circleci.com/gh/BerryCloud/js-query-string-manipulator)

Effortlessly manipulate query string parameters into your desired URL. You pass url and a set of actions to be done to QSM and you get you URL string back.

## Install

QSM is written for ES modules

```javascript
npm install query-string-manipulator
```

## Usage

Lets assume that you already have it imported

```javascript
import qsm from 'query-string-manipulator';
```

### Set parameters

Lets say that you want to add page number to a search result.

```javascript
qsm('https://www.google.cz/search?q=hello+world', {
  set: {
    num: 20,
  }
});
// https://www.google.cz/search?q=hello+world&num=20
```

It also works if the page number is already set

```javascript
qsm('https://www.google.cz/search?q=hello+world&num=20', {
  set: {
    num: 40,
  }
});
// https://www.google.cz/search?q=hello+world&num=40
```

It also works when passing a parameter as an array.

```javascript
qsm('https://www.google.cz/search?q=hello+world&num=20', {
  set: {
    num: [20, 40, 60],
  }
});
// https://www.google.cz/search?q=hello+world&num=20&num=40&num=60
```

### Remove parameters

Say that you now want to go back to first page

```javascript
qsm('https://www.google.cz/search?q=hello+world&num=20', {
  remove: ['num']
});
// https://www.google.cz/search?q=hello+world
```

Or go to the empty search page
```javascript
qsm('https://www.google.cz/search?q=hello+world&num=20', {
  remove: ['q', 'num']
});
// https://www.google.cz/search
```

### Toggle parameters

Say that you have a button on your page that enables filter and disables it when you click it again.
```javascript
qsm('https://www.google.cz/search?q=hello+world&num=20', {
  toggle: {
    tbm: 'isch',
  }
});
// https://www.google.cz/search?q=hello+world&num=20&tbm=isch

qsm('https://www.google.cz/search?q=hello+world&num=20&tbm=isch', {
  toggle: {
    tbm: 'isch',
  }
});
// https://www.google.cz/search?q=hello+world&num=20
```


### Constants

If you like "symbols", you can go like this:

```javascript
import qsm, {
  URL_REMOVE, // Used for remove
  URL_SET, // Used for set
  URL_TOGGLE, // Used for toggle
} from 'query-string-manipulator';

qsm('http://example.com/', {
  [URL_REMOVE]: ['test'],
  [URL_TOGGLE]: {
    foo: 'bar',
  },
  [URL_SET]: {
    xxx: '123',
  },
})
```

### Support methods

But wait, there is more!

#### Getting URL params

Method `getUrlParams` returns list of all parameters in form of array of objects. It cannot be returned in form of key-pair values because there can be multiple same name query params.

```javascript
getUrlParams('https://example.com/foo?select=users&getId=10')

/* returns
[
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10',
  }
]
*/
```

#### Resolve URL params

Method `resolveUrlParams` returns parameters after changed by user specified actions.

```javascript
const urlParams = [
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10'
  }
];
const paramActions = {
  remove: ['getId'],
  set: {
    select: 'userGroups',
  },
};
resolveUrlParams(urlParams, paramActions)

/* returns
[
  {
    key: 'select',
    value: 'userGroups'
  }
]
*/
```

#### Putting params together

Method `constructUrlParams` returns query string part of the URL from parameters.

```javascript
constructUrlParams([
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10'
  }
])

// returns "select=users&getId=10"
```

---
_Source: https://npm.io/package/query-string-manipulator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
