Git Product home page Git Product logo

qss's Introduction

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

qss's People

Contributors

id0sch avatar lukeed avatar maraisr avatar runjak avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.