smart-query-string
v1.0.3
Published
A lightweight TypeScript utility for parsing and manipulating query strings — no dependencies.
Maintainers
Readme
Smart Query String
A lightweight, zero-dependency TypeScript utility for parsing and manipulating URL query strings. This package provides a simple and type-safe API for working with URL query parameters in both browser and Node.js environments.
Features
- 🌐 First-class browser support with direct URL manipulation
- 🔍 Parse query strings into JavaScript objects
- 📝 Stringify objects into query strings
- 🔄 Support for arrays and nested objects
- 🛠️ Built-in TypeScript support
- 🚫 Zero dependencies
- 📦 Small bundle size
Installation
Using npm
npm install smart-query-stringUsing Yarn
yarn add smart-query-stringUsing pnpm
pnpm add smart-query-stringFramework Agnostic This package is designed to work seamlessly with any JavaScript framework including React, Vue, Angular, Svelte, and more. It has no framework dependencies and can be used in any JavaScript or TypeScript project.
Browser Usage
Get Current Query Parameters
import {queryString} from 'smart-query-string';
// Get all current URL query parameters as an object
const currentParams = queryString.get();
// If URL is https://example.com?page=1&sort=name
// Returns: { page: '1', sort: 'name' }Set Query Parameters
import {queryString} from 'smart-query-string';
// Set query parameters (replaces all existing ones)
queryString.set({page: 2, sort: 'name'});
// Updates URL to: https://example.com?page=2&sort=nameUpdate Specific Parameters
import {queryString} from 'smart-query-string';
// Update specific parameters while preserving others
queryString.update({page: 3});
// If current URL is https://example.com?page=2&sort=name
// Updates to: https://example.com?page=3&sort=nameRemove All Query Parameters
import {queryString} from 'smart-query-string';
// Remove all query parameters from the URL
queryString.remove();
// Updates URL to: https://example.comRemove Specific Parameters
import {queryString} from 'smart-query-string';
// Remove specific query parameters
queryString.removeKeys(['sort', 'filter']);
// If current URL is https://example.com?page=1&sort=name&filter=active
// Updates to: https://example.com?page=1General Usage
Parsing Query Strings
import {queryString} from 'smart-query-string';
// Parse a query string
const params = queryString.parse('foo=bar&baz=qux');
// Returns: { foo: 'bar', baz: 'qux' }
// Parse URL with query string
const {url, query} = queryString.parseUrl('https://example.com?foo=bar');
// url: 'https://example.com'
// query: { foo: 'bar' }Stringifying Objects
import {queryString} from 'smart-query-string';
// Stringify an object
const query = queryString.stringify({foo: 'bar', baz: 'qux'});
// Returns: 'foo=bar&baz=qux'
// Stringify with URL
const url = queryString.stringifyUrl({
url: 'https://example.com',
query: {foo: 'bar'}
});
// Returns: 'https://example.com?foo=bar'TypeScript Support
This package is written in TypeScript and includes type definitions out of the box.
License
MIT © Mohamed Zaki
