Have ideas to improve npm?Join in the discussion! »

hash-match

1.0.2 • Public • Published

hash-match

hash-match makes it easier to match window.location.hash with a router like wayfarer.

install

npm i --save hash-match

usage

var match = require('hash-match');
match('#weeee');
// returns "/weeee"
 
match('#/weeee');
// returns "/weeee"

So it's only really interesting if you use it like this:

match(window.location.hash);
// returns whatever the hash is

You can optionally set a prefix:

match(window.location.hash, '/hmm')

and if the hash looks like '#/hmm/whatever' or '#hmm/whatever' then you'll get '/whatever' in return.

ok but why

For feeding the output of hash-match into a router like wayfarer.

Here's an example:

var hashMatch = require('hash-match');
var router = require('wayfarer')({ default: '/' });
 
router.on('/', function () {
  console.log('root route')
})
 
router.on('/wat', function () {
  console.log('wat')
})
 
// Here's where hashMatch does its thing:
router.match(hashMatch(window.location.hash));

Now when you navigate to example.com/#/wat or example.com/#wat the /wat route will execute.

And if you want you can listen for the hashchange event to update the router:

window.addEventListener('hashchange', function (e) {
  router.match(hashMatch(window.location.hash));
});

Keywords

none

Install

npm i hash-match

DownloadsWeekly Downloads

52

Version

1.0.2

License

none

Last publish

Collaborators

  • avatar