runjak / qss

A tiny (294b) browser utility for encoding & decoding a querystring.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

qss Build Status

A tiny (305B) browser utility for stringifying a query Object.

You should only consider using this within a browser context since Node's built-in querystring.stringify is much faster and should be used in a Node environment! An ideal use case is serializing a query object before an API request is sent.

This module exposes three module definitions:

  • ES Module: dist/qss.mjs
  • CommonJS: dist/qss.js
  • UMD: dist/qss.min.js

Install

$ npm install --save qss

Usage

import { encode, decode } from 'qss';

encode({ foo:'hello', bar:[1,2,3], baz:true });
//=> 'foo=hello&bar=1&bar=2&bar=3&baz=true'

encode({ foo:123 }, '?');
//=> '?foo=123'

encode({ bar:'world' }, 'foo=hello&');
//=> 'foo=hello&bar=world'

decode('foo=hello&bar=1&bar=2&bar=3&baz=true');
//=> { foo:'hello', bar:[1,2,3], baz:true };

API

qss.encode(params, prefix)

Returns: String

Returns the formatted querystring.

params

Type: Object

The object that contains all query parameter keys & their values.

prefix

Type: String
Default: ''

An optional prefix. The stringified params will be appended to this value, so it must end with your desired joiner; eg ?.

Important: No checks or validations will run on your prefix. Similarly, no character is used to "glue" the query string to your prefix string.

qss.decode(query)

Returns: Object

Returns an Object with decoded keys and values.

Repetitive keys will form an Array of its values. Also, qss will attempt to typecast Boolean and Number values.

query

Type: String

The query string, with or without its leading ? character.

qss.decode(
  location.search.substring(1) // removes the "?"
);

Benchmarks

Running Node v14.5.0

Encode

qss             x 1,151,789 ops/sec ±0.66% (89 runs sampled)
native          x 4,432,282 ops/sec ±1.47% (90 runs sampled)
querystringify  x   644,980 ops/sec ±0.70% (93 runs sampled)
query-string    x   209,326 ops/sec ±0.63% (94 runs sampled)
qs              x   514,285 ops/sec ±0.68% (89 runs sampled)

Decode

qss             x   504,873 ops/sec ±3.95% (87 runs sampled)
native          x 1,148,267 ops/sec ±0.68% (94 runs sampled)
querystringify  x   193,010 ops/sec ±0.94% (91 runs sampled)
query-string    x   138,808 ops/sec ±1.15% (90 runs sampled)
qs              x   137,275 ops/sec ±0.77% (94 runs sampled)

License

MIT © Luke Edwards

About

A tiny (294b) browser utility for encoding & decoding a querystring.

License:MIT License


Languages

Language:JavaScript 100.0%