serialize-query-params
Advanced tools
Comparing version 1.3.6 to 2.0.0-rc.0
{ | ||
"name": "serialize-query-params", | ||
"version": "1.3.6", | ||
"version": "2.0.0-rc.0", | ||
"description": "A library for simplifying encoding and decoding URL query parameters.", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"main": "./dist/index.cjs.js", | ||
"types": "./dist/index.d.ts", | ||
"typings": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"files": [ | ||
"lib/", | ||
"esm/" | ||
"dist", | ||
"src" | ||
], | ||
"types": "lib/index.d.ts", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"build:cjs": "tsc", | ||
"build:es": "tsc -m esNext --outDir esm", | ||
"build": "npm run build:cjs && npm run build:es", | ||
"clean": "rimraf lib esm", | ||
"build": "package-bundler --copyPackageJson --rewritePackageJson --tsconfigPath ./tsconfig.build.json", | ||
"clean": "rimraf dist", | ||
"dev": "NODE_ENV=development tsc -w", | ||
"prepublishOnly": "npm run test && npm run clean && npm run build", | ||
"test": "jest", | ||
"test-watch": "jest --watch", | ||
"test-coverage": "jest --coverage" | ||
"prepublishOnly": "npm-run-all test-all clean build", | ||
"test": "vitest run", | ||
"test-watch": "vitest watch", | ||
"test-coverage": "vitest run --coverage", | ||
"test-types": "tsd", | ||
"test-all": "npm-run-all --parallel test test-types" | ||
}, | ||
@@ -44,6 +44,6 @@ "repository": { | ||
}, | ||
"peerDependencies": { | ||
"query-string": ">=5.1.1" | ||
"tsd": { | ||
"directory": "src" | ||
}, | ||
"gitHead": "791e6091524fd7a6f8f6a03ddbe1d53df89e8c37" | ||
"gitHead": "67964960ff04313bddc35eda3a4e92456249bd83" | ||
} |
@@ -26,6 +26,6 @@ <div align="center"> | ||
``` | ||
$ npm install --save serialize-query-params query-string | ||
$ npm install --save serialize-query-params | ||
``` | ||
Note: There is a peer dependency on [query-string](https://github.com/sindresorhus/query-string). For IE11 support, use v5.1.1, otherwise use v6. | ||
By default, serialize-query-params uses [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) to handle interpreting the location string, which means it does not decode `null` and has limited handling of other more advanced URL parameter configurations. If you want access to those features, add a third-party library like [query-string](https://github.com/sindresorhus/query-string) and provide its functions to updateLocation and updateInLocation as needed. | ||
@@ -37,4 +37,6 @@ ### API | ||
- [encodeQueryParams](#encodequeryparams) | ||
- [updateLocation](#usequeryparam) | ||
- [updateLocationIn](#usequeryparams-1) | ||
- [searchStringToObject](#searchstringtoobject) | ||
- [objectToSearchString](#objecttosearchstring) | ||
- [updateLocation](#updatelocation) | ||
- [updateInLocation](#updateinlocation) | ||
- [Type Definitions](./src/types.ts) | ||
@@ -84,2 +86,17 @@ - [Serialization Utility Functions](./src/serialize.ts) | ||
**Array Enum Param** | ||
You can define array enum param using `createEnumArrayParam` or `createEnumDelimitedArrayParam`. It will restricts decoded output to a list of allowed values. | ||
```js | ||
import { createEnumArrayParam } from 'serialize-query-params'; | ||
// feel free to use Enum instead of union types | ||
type Color = 'red' | 'green' | 'blue' | ||
// values other than 'red', 'green' or 'blue' will be decoded as undefined | ||
const ColorArrayEnumParam = createEnumArrayParam<Color[]>(['red', 'green', 'blue']) | ||
``` | ||
**Setting a default value** | ||
@@ -191,13 +208,59 @@ | ||
#### searchStringToObject | ||
```js | ||
function searchStringToObject(searchString: string): EncodedQuery | ||
``` | ||
Default implementation of searchStringToObject powered by URLSearchParams | ||
This converts a search string like `?foo=123&bar=x` to { foo: '123', bar: 'x' } | ||
This is only a very basic version, you may prefer the advanced versions offered | ||
by third party libraries like query-string ("parse") or qs. | ||
**Example** | ||
```js | ||
import { searchStringToObject } from 'serialize-query-params'; | ||
const obj = searchStringToObject('?foo=a&bar=x&foo=z'); | ||
// -> { foo: ['a', 'z'], bar: 'x'} | ||
``` | ||
<br/> | ||
#### objectToSearchString | ||
```js | ||
function objectToSearchString(encodedParams: EncodedQuery): string | ||
``` | ||
Default implementation of objectToSearchString powered by URLSearchParams. | ||
Does not support null values. Does not prefix with "?" | ||
This converts an object { foo: '123', bar: 'x' } to a search string `?foo=123&bar=x` | ||
This is only a very basic version, you may prefer the advanced versions offered | ||
by third party libraries like query-string ("stringify") or qs. | ||
**Example** | ||
```js | ||
import { objectToSearchString } from 'serialize-query-params'; | ||
const obj = objectToSearchString({ foo: ['a', 'z'], bar: 'x' }); | ||
// '?foo=a&foo=z&bar=x' | ||
``` | ||
<br/> | ||
#### updateLocation | ||
```js | ||
export function updateLocation( | ||
function updateLocation( | ||
encodedQuery: EncodedQuery, | ||
location: Location | ||
location: Location, | ||
objectToSearchStringFn = objectToSearchString | ||
): Location { | ||
``` | ||
Updates a location object to have a new query string (the `search` field) based | ||
on the encoded query parameters passed in via `encodedQuery`. Parameters not | ||
Creates a new location-like object with the new query string (the `search` field) | ||
based on the encoded query parameters passed in via `encodedQuery`. Parameters not | ||
specified in `encodedQuery` will be dropped from the URL. | ||
@@ -223,10 +286,12 @@ | ||
```js | ||
export function updateInLocation( | ||
function updateInLocation( | ||
encodedQueryReplacements: EncodedQuery, | ||
location: Location | ||
location: Location, | ||
objectToSearchStringFn = objectToSearchString, | ||
searchStringToObjectFn = searchStringToObject | ||
): Location { | ||
``` | ||
Updates a location object to have a new query string (the `search` field) based | ||
on the encoded query parameters passed in via `encodedQueryReplacements`. Only | ||
Creates a new location-like object with the new query string (the `search` field) | ||
based on the encoded query parameters passed in via `encodedQueryReplacements`. Only | ||
parameters specified in `encodedQueryReplacements` are affected by this update, | ||
@@ -233,0 +298,0 @@ all other parameters are retained. |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
210431
0
53
3572
319
2
1