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

queryencoder

v1.1.2

Published

An npm module to encode an object into query params of an url

Downloads

6

Readme

Lint Build Test Coverage Status codecov Known Vulnerabilities dependencies Status Commitizen friendly License GitHub issues GitHub stars npm code style: prettier Types

queryencoder

An npm module to encode an object into query params of an url

Install

To install queryencoder, run:

$ npm install queryencoder

Usage

Simple

const { encode } = require('queryencoder');

const object = {
    str: 'string',
    n: 23,
    truthy: true
};

// The result is '?str=string&n=23&truthy=true'
const queryUrl = encode(object);

With nested object

const { encode } = require('queryencoder');

const object = {
    str: 'string',
    nested: {
        a: 'ciao'
    }
};

// The result is '?str=string&nested=true&nested.a=ciao'
const queryUrl = encode(object);

With some options

const { encode } = require('queryencoder');

const object = {
    str: 'string',
    shown: undefined,
    nested: {
        a: 'ciao'
    },
    vero: true
};

// The result is 'str=string&nested.a=ciao&vero'
const queryUrl = encode(object, {
    preserveUndefined: true,
    addQuestionMark: false,
    flagNestedParents: false,
    shortBoolean: true
});

With some dates

const { encode } = require('queryencoder');

const object = {
    date: new Date('1999-04-23')
};

// The result is 'date=1999-04-23'
const queryUrl = encode(object, {
    dateParser: date => date.toISOString().slice(0, 10)
});

API

The documentation site is: queryencoder documentation

The documentation for development site is: queryencoder dev documentation

encode

The function to encode an object into an url query param string.

Syntax:

const queryString = encode(object, options);

Parameters:

  • object: It is the object describing the parameters that should be encoded. It is an object that can be nested ad have values of type: string, number, boolean and Date.
  • options: Optional. It is the object containing the options.

Options parameters:

  • addQuestionMark: Optional. A boolean that says if the ? will be added to the begin of the result. Default value: true.
  • shortBoolean: Optional. If a value is of boolean type, it will be just declared if true while omitted if false. (e.g. &val) Default value: false.
  • flagNestedParents: Optional. A boolean that says if in case there is a nested object, a parameter with value true for each path to the parents will be added. Default value: true.
  • preserveNull: Optional. A boolean that says if all the null values will be kept and parsed as 'null'. Default value: true.
  • preserveUndefined: Optional. A boolean that says if all the undefined values will be kept and parsed as 'undefined'. Default value: false.
  • dateParser: Optional. The function used to parse the dates. Default value: value => value.toISOString().

Development

To build the module make sure you have the dev dependencies installed.

The project is written in Typescript, bundled with Webpack and linted with ESLint.

Lint

In order to lint the code:

$ npm run lint

In order to lint and fix the code:

$ npm run lint:fix

There are also the :source and :test suffix after lint in order to lint only the source code or the test code.

Transpile

To transpile both the source and the test code:

$ npm run transpile

The source and the test folders will be transpiled in the dist folder. Also the type declarations will be generated.

To transpile only the source code:

$ npm run transpile:source

The source folder will be transpiled in the dist folder. Also the type declarations will be generated.

Test

After having transpiled the code, run:

$ npm test

in order to run the tests with mocha.

If a coverage report is to be generated, run:

$ npm run nyc

Bundle

$ npm run bundle

The source folder will be compiled in the bundled folder. It will contain the bundled index.js and index.d.ts files.