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

wtc-history

v0.0.1

Published

A simple wrapper for the HTML History API

Downloads

5

Readme

wtc-history 0.0.1

A simple wrapper for the HTML History API

src/wtc-history.js

new History()

Class representing an abstraction of the history API.

Install

$ npm install wtc-history

Usage

Import it in your project.

import History from 'wtc-history';

The history class is a static class, so we call its methods directly on the class itself rather than instanciate it. First, however, we need to intialise the class:

History.init();

Then we can push history states to it:

History.push('/home'); // The URL is now http://domain.com/home
History.push('/about'); // The URL is now http://domain.com/about
History.push('/about/team'); // The URL is now http://domain.com/about/team

We can also push histroy states that contain a title

History.push('/work', 'Our Work'); // The URL is now http://domain.com/work and the page title is 'Our Work'

Finally, we can also push state data into the history stack. This session data is available using the property History.currentState

History.push('/admin', 'Admin section', {sessionID: 1234567}); // The URL is now http://domain.com/work and the page title is 'Our Work'

If you want to listen to the pop and push states of the History wrapper, just subscribe to WTCHistory-pop and WTCHistory-push on the document. For example:

document.addEventListener('WTCHistory-push', (e) => {
 console.log(e.detail);
});
WTCHistory.push('/home', 'This is the Homepage', {testing: false});
// This will log:
// {
//   historyStateURL: '/home',
//   historyStateTitle: 'This is the Homepage',
//   testing: false
// }

The History object will work with native browser history interactions as well as with programmatic interactions, these can be easily triggered using History.back() and History.forward()

Returns
  • Void

History.emitEvent(eventID, data)

Emits an event from the history object

Parameters

| Name | Type | Description | | | ---- | ---- | ----------- | -------- | | eventID | string | the event ID to emit |   | | data | object | = {} the data to include with the event |   |

Examples
let listener = function(e) {
  console.log(e.detail)
  e.target.removeEventListener(e.type, arguments.callee);
}
document.addEventListener("ajax-get-addedToDom", listener);
Returns
  • Void

History.init(devmode)

Initialises the History class. Nothing should be able to operate here unless this has run with a support = true.

Parameters

| Name | Type | Description | | | ---- | ---- | ----------- | -------- | | devmode | boolean | Indicated whether the object is running in dev mode (will log outputs to console) |   |

Returns
  • boolean Returns whether init ran or not

History.push(URL, title, stateObj)

Construct and push a URL state

Parameters

| Name | Type | Description | | | ---- | ---- | ----------- | -------- | | URL | string | The URL to push, can be relative, absolute or full |   | | title | string | The title to push. |   | | stateObj | object | A state to push to the stack. This will be popped when naviagting back |   |

Examples
AJAXObj.push('/dev/foo/bar', 'The title for the history object')
Returns
  • boolean Indicates whether the push succeeded

History.back()

Takes the user back to the previous state. Simply wraps the history object's back method.

Returns
  • Void

History.forward()

Takes the user forward to the next state. Simply wraps the history object's forward method.

Returns
  • Void

History._fixURL(URL, includeDocRoot, includeTrails) private method

Takes a provided URL and returns the version that is usable

Parameters

| Name | Type | Description | | | ---- | ---- | ----------- | -------- | | URL | string | The URL to be passed |   | | includeDocRoot | boolean | = true Whether to include the docroot on the passed URL |   | | includeTrails | boolean | = true Whether to include found hashes and params |   |

Returns
  • string The passed and formatted URL

URLRegex()

URL Regex works like this:

        ^
        ([^:]+://           # HTTP(S) etc.
            ([^/]+)         # The URL (if available)
        )?
        (#{@documentRoot})? # The document root, which we want to get rid of
        (/)?                # check for the presence of a leading slash
        ([^\#\?]*)          # The URI - this is what we care about. Check for everything except for # and ?
        (\?[^\#]*)?         # any additional URL parameters (optional)
        (\#\!?.+)?          # Any hashbang trailers (optional)
Returns
  • Void

_popstate(e) private method

Listener for the popstate method

Parameters

| Name | Type | Description | | | ---- | ---- | ----------- | -------- | | e | object | the passed event object |   |

Returns
  • void

docrootRegex()

docrootRegex works like this:

         ^
         ([^:]+://       # HTTP(S) etc.
             ([^/]+)     # The hostname (if available)
         )?
         /?
         (.*(?=/))?      # the URI to use as the docroot less any available trailing slash
Returns
  • Void

Documentation generated with doxdox.