Git Product home page Git Product logo

hash-match's Introduction

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('wayfinder')({qs: false});

router.default('/');

router.route('/', function () {
  console.log('root route')
})

router.route('/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));
});

hash-match's People

Contributors

sethvincent avatar

Watchers

James Cloos avatar Yosh avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.