hm
hash-match
npm i hash-match
hm

hash-match

match location.hash to routes

by Seth Vincent

1.0.2 (see all)TypeScript:Not Found
npm i hash-match
Readme

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));
});
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate