or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-querystringify

Small, simple but powerful query string parser for JavaScript applications.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/querystringify@2.2.x

To install, run

npx @tessl/cli install tessl/npm-querystringify@2.2.0

index.mddocs/

querystringify

querystringify is a lightweight JavaScript library that provides a simple, JSON-compatible interface for parsing query strings into objects and converting objects back into query strings. It features built-in security protection against prototype pollution and graceful error handling for malformed input.

Package Information

  • Package Name: querystringify
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install querystringify

Core Imports

var qs = require('querystringify');

For ES modules (if available):

import { parse, stringify } from 'querystringify';

Basic Usage

var qs = require('querystringify');

// Parse query strings into objects
var result = qs.parse('?foo=bar&baz=qux');
// Result: { foo: 'bar', baz: 'qux' }

// Convert objects into query strings
var queryString = qs.stringify({ foo: 'bar', baz: 'qux' });
// Result: 'foo=bar&baz=qux'

// Add prefix to query string
var withPrefix = qs.stringify({ foo: 'bar' }, true);
// Result: '?foo=bar'

Capabilities

Query String Parsing

Parses query strings into JavaScript objects with automatic URL decoding and security protections.

/**
 * Parse a query string into an object
 * @param {string} query - The query string to parse (can include ?, #, or no prefix)
 * @returns {object} Object with key-value pairs from the query string
 */
function parse(query);

Features:

  • Handles query strings with or without ? or # prefixes
  • Parameters without values are set to empty strings
  • Automatic URL decoding (including plus signs as spaces)
  • Prevents prototype pollution by checking existing properties
  • Graceful error handling - invalid encoded values are omitted
  • First occurrence wins for duplicate keys

Usage Examples:

var qs = require('querystringify');

// Basic parsing
qs.parse('foo=bar&baz=qux');
// { foo: 'bar', baz: 'qux' }

// Works with prefixes
qs.parse('?foo=bar&baz=qux');
// { foo: 'bar', baz: 'qux' }

qs.parse('#foo=bar&baz=qux');
// { foo: 'bar', baz: 'qux' }

// Parameters without values
qs.parse('foo&bar=baz');
// { foo: '', bar: 'baz' }

// URL decoding including plus signs
qs.parse('name=John+Doe&city=New%20York');
// { name: 'John Doe', city: 'New York' }

// Duplicate keys - first wins
qs.parse('foo=first&foo=second');
// { foo: 'first' }

// Malformed input is handled gracefully
qs.parse('?%&validkey=validvalue');
// { validkey: 'validvalue' }

Query String Generation

Converts JavaScript objects into properly encoded query strings with optional prefix support.

/**
 * Convert an object into a query string
 * @param {object} obj - Object to convert to query string
 * @param {string|boolean} [prefix] - Optional prefix. If true, uses '?'. If string, uses that string. Default: no prefix
 * @returns {string} Query string representation, or empty string if no valid pairs
 */
function stringify(obj, prefix);

Features:

  • Automatic URL encoding of keys and values
  • Handles null, undefined, and NaN values by converting to empty strings
  • Optional prefix support (no prefix, ?, or custom string)
  • Empty objects return empty string regardless of prefix
  • Graceful error handling - invalid encoding attempts are omitted
  • Works with objects created with Object.create(null)

Usage Examples:

var qs = require('querystringify');

// Basic stringification
qs.stringify({ foo: 'bar', baz: 'qux' });
// 'foo=bar&baz=qux'

// With question mark prefix
qs.stringify({ foo: 'bar', baz: 'qux' }, true);
// '?foo=bar&baz=qux'

// With custom prefix
qs.stringify({ foo: 'bar', baz: 'qux' }, '#');
// '#foo=bar&baz=qux'

// Empty values
qs.stringify({ foo: '' });
// 'foo='

// Null, undefined, NaN become empty strings
qs.stringify({ foo: null, bar: undefined, baz: NaN });
// 'foo=&bar=&baz='

// URL encoding
qs.stringify({ name: 'John Doe', city: 'New York' });
// 'name=John%20Doe&city=New%20York'

// Empty objects
qs.stringify({});
// ''
qs.stringify({}, true);
// ''

// Works with nulled objects
var obj = Object.create(null);
obj.foo = 'bar';
qs.stringify(obj);
// 'foo=bar'

Error Handling

querystringify is designed to handle malformed input gracefully:

  • Invalid URL encoding: Characters that cannot be decoded are omitted from results
  • Prototype pollution protection: Built-in properties like toString and __proto__ cannot be overridden
  • Encoding failures: Keys or values that cannot be encoded are omitted from output
  • Edge cases: Empty strings, null values, and undefined values are handled consistently

Browser and Node.js Compatibility

querystringify works in both browser and Node.js environments without external dependencies. It uses standard JavaScript APIs (encodeURIComponent, decodeURIComponent) available in all modern environments.