3.2.0 • Published 3 years ago

es6-router v3.2.0

Weekly downloads
29
License
MIT
Repository
github
Last release
3 years ago

ES6 Router

Licence Tests

A simple client side router built in ES6 with 0 dependencies and TypeScript definitions.

Usage

const router = new Router({ ... })
  .add(() => {
    // getPage('/');
  })
  .add(/about/, () => {
    // getPage('about');
  })
  .add('contact', () => {
    // getPage('contact');
  });

router.remove('contact');
router.navigate('about');

API

constuctor

ParamTypeDescription
optionsObjectOptions object
options.debugboolean (false)Enable debugging
options.contextObject (window)Context to add event listener to
options.startListeningboolean (true)Start listening when router is created

currentRoute ⇒ string

Name of the current route

Returns: string - Current route

add(route, handler) ⇒ Router

Add a new route

ParamTypeDescription
restring | RegExpName of route to match
handlerfunctionMethod to execute when route matches

Returns: Router - This router instance

remove(route, handler) ⇒ Router

Remove a route from the routerc

ParamTypeDescription
restring | RegExpName of route to remove
handlerfunctionFunction handler to remove

Returns: Router - This router instance

reload() ⇒ Router

Reload the current route

Returns: Router - This router instance

listen(instance) ⇒ Router

Start listening for hash changes on the window

ParamTypeDefaultDescription
instanceanyWindowContext to start listening on

Returns: Router - This router instance

stopListen(instance) ⇒ Router

Stop listening for hash changes on the window

ParamTypeDefaultDescription
instanceanyWindowContext to stop listening on

Returns: Router - This router instance

navigate(path) ⇒ Router

Navigate router to path

ParamTypeDescription
pathstringPath to navigate the router to

Returns: Router - This router instance

navigateError(hash) ⇒ Router

Navigate to the error page

ParamType
hashstring

Returns: Router - This router instance

cleanPath(path) ⇒ string

Strip the path of slashes and hashes

ParamTypeDescription
pathstringPath to clean of hashes

Returns: string - Cleaned path

parseRoute(path) ⇒ string

Parse a route URL to get all parts

ParamTypeDescription
pathstringRoute to split into parts

Returns: string[] - Parts of the url

License

MIT

3.2.0

3 years ago

3.1.6

6 years ago

3.1.5

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.1

6 years ago

1.0.0

7 years ago