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

@resourge/history-store

v1.3.3

Published

`@resourge/history-store` is a lightweight JavaScript utility for managing and subscribing to navigation events using the browser's History API. It simplifies tracking URL changes and enables seamless state management for single-page applications (SPAs).

Downloads

141

Readme

@resourge/history-store

@resourge/history-store is a lightweight JavaScript utility for managing and subscribing to navigation events using the browser's History API. It simplifies tracking URL changes and enables seamless state management for single-page applications (SPAs).

Features

  • Unified Navigation State Management: Consistent API for both web and React Native environments.
  • Subscription-Based Updates: Subscribe to URL changes and respond in real-time.
  • Custom Navigation Actions: Specify custom navigation actions or replace the current history state.

Installation

Install using Yarn:

Browser

yarn add @resourge/history-store

or NPM:

npm install @resourge/history-store --save

react-native

yarn add @resourge/history-store react-native-url-polyfill

or NPM:

npm install @resourge/history-store react-native-url-polyfill --save

Basic usage

To start using @resourge/history-store, simply import and interact with the HistoryStore instance:

// Browser
import { HistoryStore } from '@resourge/history-store';
// react-native
// import { HistoryStore } from '@resourge/history-store/mobile';

// Subscribe to URL changes
const unsubscribe = HistoryStore.subscribe(() => {
  const [currentUrl, action, previous] = HistoryStore.getValue();
  console.log('Current URL:', currentUrl.href);
  console.log('Action:', action);
  if (previous) {
    console.log('Previous URL:', previous[0].href);
  }
});

// Navigate to a new URL
HistoryStore.navigate(new URL('/new-page', window.location.href));

// Unsubscribe when done
unsubscribe();

Methods

subscribe(notification: () => void): () => void

Adds a callback function that will be triggered on URL change. Returns an unsubscribe function to remove the subscription.

const unsubscribe = HistoryStore.subscribe(() => {
  console.log('URL changed!');
});

getValue(): StoreValue

Returns the current navigation state, which is a tuple containing:

  • url: The current URL.
  • action: The type of action that triggered the change.
  • previousValue: (Optional) A tuple containing the previous URL and action.
const [url, action, previous] = HistoryStore.getValue();

navigate(url: URL, options?: NavigateOptions): void

Navigates to a specified URL. The optional NavigateOptions can be used to define the navigation action or whether to replace the current history state.

  • Web:

    • url: The current URL.
    • options.action: The navigation action type (e.g., push, replace).
    • options.replace: If true, replaces the current navigation state instead of pushing a new one.
  • react-native:

    • url: The current URL.
    • options.action: The navigation action type (e.g., push, replace).
    • options.replace: If true, replaces the current navigation state instead of pushing a new one.
    • config.stack: If true, clears all navigation entries after the current URL before adding a new entry.
HistoryStore.navigate(new URL('/new-page', window.location.href), { replace: true });

Utility functions

createNewUrlWithSearch(url: URL, newSearch: string, hash?: boolean): URL

Creates a new URL instance with an updated search string. Optionally updates the hash if specified.

  • Parameters:
    • url: The original URL.
    • newSearch: The new search string to be set.
    • hash: If true, updates the URL hash with the new search string.
import { createNewUrlWithSearch } from '@resourge/history-store/utils';
const newUrl = createNewUrlWithSearch(new URL('/path', window.location.origin), 'foo=bar', true);

parseParams<T extends Record<string, any>>(paramValues: T): string

Converts parameters into a search string.

  • Parameters:
    • paramValues: An object representing the parameters.
import { parseParams } from '@resourge/history-store/utils';
const searchString = parseParams({ foo: 'bar', nested: { key: 'value' } });

parseSearchParams<T extends Record<string, any>>(searchParams: URLSearchParams, defaultParams?: T): T

Converts search parameters into an object with primitive values.

  • Parameters:
    • searchParams: An instance of URLSearchParams.
    • defaultParams: Optional default parameters to merge with the parsed result.
import { parseSearchParams } from '@resourge/history-store/utils';
const params = parseSearchParams(new URLSearchParams('foo=bar&nested[key]=value'));

parseSearch<T extends Record<string, any>>(search: string, defaultParams?: T): T

Converts a search string into an object with primitive values.

  • Parameters:
    • search: The search string (e.g., ?foo=bar&nested[key]=value).
    • defaultParams: Optional default parameters to merge with the parsed result.
import { parseSearch } from '@resourge/history-store/utils';
const params = parseSearch('?foo=bar&nested[key]=value');

License

MIT Licensed.