Skip to content

Documentation / @andrew_l/toolkit / qs

Variable: qs ​

const qs: object

Simple query stringy interface that supports encoding/decoding of Array, Set, Map, Object, BigInt

Type Declaration ​

merge ​

merge: (...values) => Record<string, any>

Merge first level values

Parameters ​

values ​

...Record<string, any>[]

Returns ​

Record<string, any>

parse ​

parse: {(value): Record<string, string>; <T>(value, defaults): T; <T>(value, defaults): Partial<T>; }

Call Signature ​

(value): Record<string, string>

Parse query string as is without type casting

Parameters ​
value ​

string

Returns ​

Record<string, string>

Call Signature ​

<T>(value, defaults): T

Parse query string and use default object as type cast schema

Type Parameters ​
T ​

T extends Record<string, any>

Parameters ​
value ​

string

defaults ​

Partial<T>

Returns ​

T

Call Signature ​

<T>(value, defaults): Partial<T>

Parse query params and use default object as type cast schema

Type Parameters ​
T ​

T extends Record<string, any>

Parameters ​
value ​

Record<string, any>

defaults ​

T

Returns ​

Partial<T>

parseValue ​

parseValue: <T>(value, asType) => TypeOfMap[T] | undefined

Parse string query value as a type

Type Parameters ​

T ​

T extends keyof TypeOfMap

Parameters ​

value ​

any

asType ​

T

Returns ​

TypeOfMap[T] | undefined

stringify ​

stringify: (obj, options?) => string

Simple function to transform object into query string (not standards)

Parameters ​

obj ​

Record<string, any>

options? ​

StringifyOptions

Returns ​

string

stringifyValue ​

stringifyValue: (value) => string

Stringify value to use as query parameter

Parameters ​

value ​

unknown

Returns ​

string

toParams ​

toParams: (obj, options?) => Record<string, string>

Prepare search params object

Parameters ​

obj ​

Record<string, any>

options? ​

StringifyOptions

Returns ​

Record<string, string>

Example ​

ts
// encode
qs.stringify({ page: 1, limit: 10 }); // 'page=1&limit=10'

// decode
const defaults = { page: 1, limit: 10 };
const params = qs.parse('page=5&limit=abc', defaults); // { page: 5, limit: 10 }

Replaces ​

Number(searchParams.get('page')) || 1, params.get('x') === 'true' — qs.parse(search, defaults) casts each key to the type of its default (number, boolean, Date, array, Set, Map, bigint) and falls back to the default when the value is missing or invalid. Caveats: numbers use parseFloat ('12abc' becomes 12), only 'true' is true, and keys missing from defaults are dropped.

Detect ​

(Number|parseInt|parseFloat)\(\s*[\w.]*(?i:params|query)\w*\.get\(

Detect ​

\.get\(\s*['"][^'"]+['"]\s*\)\s*===\s*['"]true['"]

Replaces ​

new URLSearchParams(Object.entries(obj).filter(([, v]) => v != null)) — qs.stringify(obj) skips null, undefined, '' and empty arrays or objects (keeps 0 and false), writes Dates as ISO strings and arrays as CSV; excludeDefaults also drops values equal to their defaults.

Detect ​

new URLSearchParams\(\s*Object\.entries\(

Released under the MIT License.