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

@twilburn/scrapple

v1.0.0

Published

A simple, tiny hash router for news apps

Downloads

6

Readme

Scrapple

Scrapple is a simple, tiny hashbang router useful for news applications or other simple web apps. It allows you to register callbacks for URL patterns, as well as adding catch-all event handlers for all hits or misses on the routing list, in less than 2KB uncompressed.

Usage

To use Scrapple, first create an instance:

var router = new Scrapple();

Next, you'll want to register some routes. These can be simple constants--for example, you might want to have a view that automatically triggers for "yourpage.html#!/about":

router.add("/about", function() {
  console.log("You've triggered the about page");
});

Technically, Scrapple is not picky about how you prefix your internal URLs. You can visit "#!/about", "#/about", or "#about", and all of them will fire this route callback. Trailing slashes are also removed. Multiple routes can be registered in a single call, by passing in an array:

router.add(["/", "/home"], function() {
  console.log("Both routes will trigger this function");
});

However, routes do not have to be constant. They can also include parameters in the URL, which will automatically be extracted and passed through to your callback function as the params property of the event. The event object will also contain a url parameter, which is useful for logging.

router.add("/pets/:name", function(e) {
  console.log(`Hello, ${e.params.name}`);
});

router.add("/pets/:name/feed/:food", function(e) {
  var { name, food } = e.params;
  console.log(`${name} is grateful for the ${food}!`);
})

// visiting "/pets/Neko" => "Hello, Neko"
// visiting "/pets/Neko/feed/kibble" => "Neko is grateful for the kibble!"

Although Scrapple is very small and simple, it shines when creating template-driven data visualizations and applications, such as the Seattle Times' mayoral primary guide in 2017. Jumping to a new route is as simple as writing a link, with no need to manually add event listeners or mark up your HTML:

<a href="#!/go-to/route">This is a router link!</a>

Scrapple is an effort to go with the grain of the web platform: by working with hash events, it correctly handles the back button (because it leaves it up to the browser) and makes it easy for your users to bookmark pages or open them in new tabs.

If you want to be notified on any route, or when Scrapple can't find a route to match the current URL hash string, set functions to the router's onhit and onmiss properties, respectively:

router.add("/route", function() {
  console.log("Route callback");
});

router.onhit = function(e) {
  console.log(`Hit for ${e.url}`);
};

router.onmiss = function(url) {
  console.log(`Visited ${url} but no matching route!`);
};

// visit "/route" => "Hit for /route" and "Route callback"
// visit "/missing" => "Visited /missing but no matching route!"

Note that the onhit callback fires before the actual route--this can be useful if you need to do any cleanup for the current application state before proceeding to the next route.