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 🙏

© 2026 – Pkg Stats / Ryan Hefner

url-query-toolkit

v1.0.1

Published

Lightweight utilities for managing URL query parameters and navigation in the browser

Readme

url-query-toolkit

🔧 Lightweight utilities for managing URL query parameters and navigation in the browser.

Simple, intuitive API for working with query strings without reloading the page.

Installation

npm install url-query-toolkit

Why This Package?

Working with URL query parameters shouldn't be complicated. This toolkit provides:

Simple API - Intuitive methods for common operations
No page reloads - Updates URL using History API
Zero dependencies - Pure JavaScript, tiny bundle size
TypeScript-friendly - Full JSDoc annotations
Framework agnostic - Works with vanilla JS, React, Vue, etc.

Quick Start

import { useQuery } from 'url-query-toolkit'

const query = useQuery()

// Get a parameter
const userId = query.getParam('user') // "123"

// Set a parameter
query.setParam('page', '2') // URL: ?page=2

// Get all parameters
const params = query.getAllParams() // { page: '2', user: '123' }

// Delete a parameter
query.deleteParam('user') // URL: ?page=2

// Clear everything
query.clearAllParams() // URL: (no query string)

API Reference

useQuery()

Returns an object with methods for managing URL parameters.

const query = useQuery()

getParam(key)

Get a single query parameter value.

// URL: ?name=John&age=30
const name = query.getParam('name') // "John"
const missing = query.getParam('missing') // null

Parameters:

  • key (string) - Parameter name

Returns: string | null


getAllParams()

Get all query parameters as an object.

// URL: ?name=John&age=30&city=NYC
const params = query.getAllParams()
// { name: "John", age: "30", city: "NYC" }

Returns: Object<string, string>


setParam(key, value)

Set or update a query parameter. Replaces existing value.

// URL: ?page=1
query.setParam('page', '2')
// URL: ?page=2

query.setParam('sort', 'date')
// URL: ?page=2&sort=date

Parameters:

  • key (string) - Parameter name
  • value (string) - Parameter value

addParam(key, value)

Append a query parameter without replacing existing values.

// URL: ?tag=javascript
query.addParam('tag', 'nodejs')
// URL: ?tag=javascript&tag=nodejs

Parameters:

  • key (string) - Parameter name
  • value (string) - Parameter value

deleteParam(key)

Remove a query parameter from the URL.

// URL: ?name=John&age=30
query.deleteParam('age')
// URL: ?name=John

Parameters:

  • key (string) - Parameter name to remove

clearAllParams()

Remove all query parameters from the URL.

// URL: ?name=John&age=30&city=NYC
query.clearAllParams()
// URL: (clean, no query string)

goToPage(url, params)

Navigate to a new page with query parameters.

query.goToPage('/search?', {
  q: 'javascript',
  sort: 'relevance',
  page: '1'
})
// Navigates to: /search?q=javascript&sort=relevance&page=1

Parameters:

  • url (string) - Base URL
  • params (Object) - Query parameters to append

getSlug()

Get the last segment of the URL pathname.

// URL: https://example.com/blog/my-post-title
const slug = query.getSlug() // "my-post-title"

// URL: https://example.com/products/123
const id = query.getSlug() // "123"

Returns: string


Real-World Examples

Pagination

const query = useQuery()

// Get current page
const currentPage = query.getParam('page') || '1'

// Go to next page
function nextPage() {
  const page = parseInt(currentPage) + 1
  query.setParam('page', page.toString())
}

// Go to previous page
function prevPage() {
  const page = Math.max(1, parseInt(currentPage) - 1)
  query.setParam('page', page.toString())
}

Filters and Sorting

const query = useQuery()

// Apply filters
function applyFilters(filters) {
  Object.entries(filters).forEach(([key, value]) => {
    if (value) {
      query.setParam(key, value)
    } else {
      query.deleteParam(key)
    }
  })
}

// Example usage
applyFilters({
  category: 'electronics',
  price_min: '100',
  price_max: '500',
  sort: 'price_asc'
})
// URL: ?category=electronics&price_min=100&price_max=500&sort=price_asc

Search with History

const query = useQuery()

function search(searchTerm) {
  if (searchTerm) {
    query.setParam('q', searchTerm)
  } else {
    query.deleteParam('q')
  }
}

// Restore search on page load
const savedSearch = query.getParam('q')
if (savedSearch) {
  document.getElementById('search-input').value = savedSearch
  performSearch(savedSearch)
}

Multi-Select Filters

const query = useQuery()

// Add multiple tags
function addTag(tag) {
  query.addParam('tag', tag)
}

// Get all selected tags
function getSelectedTags() {
  const params = new URLSearchParams(window.location.search)
  return params.getAll('tag') // ["javascript", "nodejs", "react"]
}

Tab Navigation

const query = useQuery()

function switchTab(tabName) {
  query.setParam('tab', tabName)
}

// Restore active tab on page load
const activeTab = query.getParam('tab') || 'overview'
showTab(activeTab)

Dynamic Product Pages

const query = useQuery()

// Get product ID from URL
const productId = query.getSlug()
// URL: /products/abc-123 → productId: "abc-123"

// Load product details
fetchProduct(productId)

// Handle variant selection
function selectVariant(color, size) {
  query.setParam('color', color)
  query.setParam('size', size)
}
// URL: /products/abc-123?color=blue&size=large

Clear Filters Button

const query = useQuery()

function clearAllFilters() {
  query.clearAllParams()
  // Optionally reload data
  loadProducts()
}

Use Cases

🔍 Search pages - Preserve search queries in URL
📄 Pagination - Share links to specific pages
🎛️ Filters & sorting - Bookmarkable filter states
🗂️ Tabs - Deep linking to specific tabs
🛒 E-commerce - Product variants, filters, sorting
📊 Dashboards - Date ranges, view modes
🎮 Game states - Level selection, difficulty

Browser Support

Works in all modern browsers that support:

  • URLSearchParams
  • History API

Supported:

  • Chrome 51+
  • Firefox 44+
  • Safari 10.1+
  • Edge 14+

Framework Integration

Vanilla JavaScript

import { useQuery } from 'url-query-toolkit'
const query = useQuery()
query.setParam('page', '2')

React

import { useQuery } from 'url-query-toolkit'

function SearchPage() {
  const query = useQuery()
  
  const handleSearch = (term) => {
    query.setParam('q', term)
    // Trigger re-render or refetch
  }
  
  return <input onChange={(e) => handleSearch(e.target.value)} />
}

Vue

import { useQuery } from 'url-query-toolkit'

export default {
  setup() {
    const query = useQuery()
    
    const updateFilter = (key, value) => {
      query.setParam(key, value)
    }
    
    return { updateFilter }
  }
}

TypeScript Support

Full JSDoc annotations are included. For TypeScript projects, types are inferred automatically:

import { useQuery } from 'url-query-toolkit'

const query = useQuery()
const page: string | null = query.getParam('page')

License

MIT

Contributing

Issues and pull requests are welcome on GitHub.


Made with ❤️ for better URL management