Unquery 🍫
Build and control query-strings with confidence.
It helps you to parse and stringify query-strings
in a predictable way.
Instalation
To install, use:
Yarn:
yarn add --save unquery
Npm:
npm install --save unquery
Usage
Example 1 - Basic Usage
const querySchema = console
"foo": "bar" "baz": 1
Example 2 - Different types usage
const querySchema = console
"foo": "str" "bar": 123 "baz": 1 2 3 "date": "2020-01-01" "num": 20
Example 3 - Stringify usage
const querySchema = const stringified = console
"?foo=str&bar=123&baz[]=1&baz[]=2&baz[]=3&date[]=2020-01-01"
API
Unqueryinput: string, schema: object, config: object
string
@param input: Input to be parsed.
object
@param schema: Your query-string schema, that's gonna be parsed.
object
@param config: Options to customize the schema that's gonna be generated.
Options
- arrayFormat
- Description Array format to parse query-string. Use the same format used by query-string.
- Type
string
- Default
none
- Values Each value will parse the following query-strings into an array:
"bracket": "foo[]=1&foo[]=2&foo[]=3""index": "foo[0]=1&foo[1]=2&foo[3]=3""comma": "foo=1,2,3""none": "foo=1&foo=2&foo=3"
- skipNull
- Description Skip
null
values to be parsed. - Type
boolean
- Default
false
// { value: 123, notInQueryValue: null }// { value: 123 } - Description Skip
- skipUnknown
- Description Skip
unknown
values to be parsed. - Type
boolean
- Default
true
// { value: "foo", unknown: "bar" }// { value: "foo" } - Description Skip
Unquery Methods
When you create an Unquery Object
, your query will receive some super powers ⚡️!
const query = // query is an Unquery Object
Global API
-
addLocationURL
- Description Add query-string to URL. This keeps all current search.
- Type
(query: object | string, options: StringifyOptions) => void
- Default
null
- Example
// https://yoursite.com/const querySchema =// https://yoursite.com?foo=bar&baz=42// https://yoursite.com?foo=bar&baz=42&date=2020-09-01 -
replaceLocationURL
- Description Replace all URL search by the query-string.
- Type
(query: object | string, options: StringifyOptions) => void
- Default
null
- Example
// https://yoursite.com/const querySchema =// https://yoursite.com?foo=bar&baz=42// https://yoursite.com?date=2020-09-01 -
clearLocationURL
- Description Clear all query-string from URL without reload the page.
- Type
() => void
- Default
null
- Example
// https://yoursite.com/?foo=bar&baz=42const query =// https://yoursite.com/ -
stringify
- Description Stringify an object into a query string.
- Type
(queryObject: UnqueryObject, options: StringifyOptions) => string
- Default
// You can set your unqueryOptions by calling setOptions(options)arrayFormat: unqueryOptionsarrayFormat // default: 'none'- Examples
const stringified =// "startDate=2020-01-01&viewId=4" -
setOptions
- Description Set default options to use in your entire app.
- Type
(options: UnqueryOptions) => UnqueryOptions
- Examples
const query =// { date: "2020-02-01", foo: ['1','2','3'] }const stringified =// 'data=01/02/2020&foo[0]=1&foo[1]=2&foo[2]=3