url-to-action

1.1.1 • Public • Published

url-to-action

Library to run action by matched URL pathname route.

Installation

This is library available through npm registry.

npm install --save url-to-action

Routers

Set unique property keys to creating a route with add property rules. Optional property params will give a key to capture groups. And action will be run when URL matched with rules.

property default value
rules none RegEx
params [] Array
action function function
Example routes:
const routes = {
  home: {
    rules: /\/(home|$)/i,
    action: function () {
      console.log('home')
    }
  },
  user: {
    rules: /\/user\/(\w)/i,
    params: ['userId'],
    action: function (config) {
      console.log(`user id: ${config.params.userId}`)
    }
  }
}

Run action

const UrlToAction = require('url-to-action')

const routes = {
  home: {
    rules: /\/(home|$)/i,
    action: function () {
      console.log('home')
    }
  },
  user: {
    rules: /\/user\/(\w)/i,
    params: ['userId'],
    action: function (config) {
      console.log(`user id: ${config.params.userId}`)
    }
  }
}

const router = new UrlToAction({
  routes: routes,
  domain: window.location.protocol + '//' + window.location.host
})

router.action('/home') // home
router.action('/user/1') // user id: 1

Readme

Keywords

Package Sidebar

Install

npm i url-to-action

Weekly Downloads

2

Version

1.1.1

License

MIT

Unpacked Size

5.08 kB

Total Files

4

Last publish

Collaborators

  • musasutisna