npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

querystring-serializer

v0.1.2

Published

QuerystringSerializer is a TypeScript class that provides serialization and deserialization of objects to and from URL-encoded query strings. It supports handling nested objects and arrays within the serialization and deserialization processes.

Downloads

12

Readme

QuerystringSerializer is a TypeScript class that provides serialization and deserialization of objects to and from URL-encoded query strings. It supports handling nested objects and arrays within the serialization and deserialization processes.

It's inspired by popular package qs (https://www.npmjs.com/package/qs)

Features

  • Supports serialization of simple objects into URL-encoded query strings.
  • Handles nested objects and arrays within the serialization and deserialization processes.
  • Properly encodes query string values using encodeURIComponent and decodes them using decodeURIComponent.
  • Allows developers to serialize objects as human-readable for search queries.
  • Provides flexible serialization parameters for more unusual scenarios.
  • Maintains the correct order of array elements during serialization.
  • Follows the Google TypeScript Style Guide for code structure and naming conventions.

Installation

You can install the QuerystringSerializer package using either npm or yarn.

npm

npm install querystring-serializer

yarn

yarn add querystring-serializer

Usage

Serialization

import QuerystringSerializer from "querystring-serializer";

const data = {
    a: 'b', 
    c: {
      d: 1, 
      e: true
    },
};

const queryString = QuerystringSerializer.serialize(data);

queryString will be:

a=b&c.d=1&c.e=true'

Deserialization

import QuerystringSerializer from "querystring-serializer";

const queryString = 'a=b&c.d=1&c.e=true';
const data = QuerystringSerializer.parse(queryString);

data will be:

{
    a: 'b', 
    c: {
      d: 1, 
      e: true
    },
};

Nested Objects in Arrays

import QuerystringSerializer from "querystring-serializer";

const data = {
    a: 'b', 
    c: [
        {
            f: ["g", "h"]
        },
    ]
};

const queryString = QuerystringSerializer.serialize(data);

queryString will be:

a=b&c[0].f[0]=g&c[0].f[1]=h

Same way, QuerystringSerializer.serialize("a=b&c[0].f[0]=g&c[0].f[1]=h'"); will produce:

{
    a: 'b', 
    c: [
        {
            f: ["g", "h"]
        },
    ]
}

Encoded Results

import QuerystringSerializer from "querystring-serializer";

const data = {
  products: [
    {
      name: 'iPhone 13',
      price: 999,
      features: ['Face ID', 'A15 Bionic Chip', '5G Support'],
    },
    {
      name: 'Samsung Galaxy S21',
      price: 899,
      features: ['Dynamic AMOLED Display', 'Snapdragon 888', '108MP Camera'],
    },
  ],
};

const queryString = QuerystringSerializer.serialize(data);

queryString will be:

products[0].name=iPhone%2013&products[0].price=999&products[0].features[0]=Face%20ID&products[0].features[1]=A15%20Bionic%20Chip&products[0].features[2]=5G%20Support&products[1].name=Samsung%20Galaxy%20S21&products[1].price=899&products[1].features[0]=Dynamic%20AMOLED%20Display&products[1].features[1]=Snapdragon%20888&products[1].features[2]=108MP%20Camera

URL Friendly, Human readible serialization

The main motivation behind that package was to make URL search parameters more human-readable. However, the default results of the serialize function produce special characters like "[" and "]", which transform the following beautiful[0]=querystring into beautiful%5B0%5D%3Dquerystring, making it hard to read.

After version 0.1.0, I have changed fixed characters(like [,],=,.) from serialization and make them parametric:

  • delimiter - The delimiter used to separate key-value pairs in the query string. (Default: '&')
  • arrayStart - The string that indicates the start of an array in the object. (Default: '[')
  • arrayEnd - The string that indicates the end of an array in the object. (Default: ']')
  • equalityChar - The character used to assign values to keys in the query string. (Default: '=')
  • nestDelimiter - The delimiter used to represent nested objects in the query string. (Default: '.')

So, you will be able to modify your results like that:

import QuerystringSerializer from "querystring-serializer";

const data = {
    beatiful: ['querystring'],
};

const queryString = QuerystringSerializer.serialize(
    data, 
    {
        ...QuerystringSerializer.defaultParameters,
        arrayStart: "#",
        arrayEnd: ""
    }
);

queryString will be:

beatiful#0=querystring

To parse that string you need to follow same method in parse function

import QuerystringSerializer from "querystring-serializer";

const queryString = 'beautiful#0=querystring';
const data = QuerystringSerializer.parse(
    queryString,
    {
        ...QuerystringSerializer.defaultParameters,
        arrayStart: "#",
        arrayEnd: ""
    }
);

data will be:

{
  beautiful: ['querystring'],
};

If you want to use result for querystring parameter values, which is very common developer attitude:

import QuerystringSerializer from "querystring-serializer";

const data = {
    my: ['data', "parameter"],
};

const queryString = QuerystringSerializer.serialize(
    {
        simple: 'value',
        array: [
            {
                internal: ["val1", "val2"]
            },
        ]
    },
    {
        ...QuerystringSerializer.defaultParameters,
        arrayStart: "@",
        arrayEnd: "",
        equalityChar: ":",
        delimiter: "#"
    }
);

queryString will be:

simple:value#[email protected]@0:val1#[email protected]@1:val2

Fun part of it

For those parameters you can use keywords too. To explain this more clearly, lets check our last test on the src/__test__/QuerySerializer.test.tswhich will make your day

import QuerystringSerializer from "querystring-serializer";

const funParameters = {...QuerystringSerializer.defaultParameters,
    nestDelimiter: " make ",
    equalityChar: " some "
}

const wut = {
    should: {make: "fun"},
}

const serialized = QuerystringSerializer.serialize(
    wut,
    funParameters
)

const marooned = QuerystringSerializer.parse(serialized, funParameters)

expect(wut).toEqual(marooned);

guess serialized what can be:

should make some fun

License

This project is licensed under the MIT License - see the LICENSE file for details.