Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

broute

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

broute

procedural router

Source
npmnpm
Version
1.2.1
Version published
Maintainers
1
Created
Source

broute - brutal router

import {route, path, navigate} from 'broute'

route

route(f)

Declares the route function f which will be invoked each time the url changes. There can only be one such function. The url is "consumed" using nested scoped path functions.

:: (() ->) -> undefined

argdesc
fThe one and only route function.
retAlways undefined
route usage

The following usage shows how nested path declarations creates "scoped" functions that consumes part of the current url.

// the current url is: "/some/path/deep?panda=42"

route(() => {

    path('/some', (left, query) => {
        // at this point we "consumed" '/some'
        // and left is "/path/deep"
        // and query is {panda:42}
        path('/path/deep', (left, query) => {
          // left is "" and query is {panda:42}
        })
    })
    // at this point we haven't consumed anything
    path('/another', () => {
        // will not be invoked for the current url
    })
})
route example
isItem = (part, query) => part?.length > 1        // '' means list

route(() => {

    path('/news/', (slugId) => {                  // consume '/news/'
        action('selectarticle', slugid)           // fire action to fetch article
    }, () => {
        action('refreshnewslist')                 // else refresh news list
    })
    path('/aboutus', () => {                      // consume '/aboutus'
        action('showaboutus')
    })
})

path

path(p,f,fe)

As part of route declares a function f that is invoked if we "consume" url part p of the current (scoped) url.

argdesc
pThe string url part to match/consume.
fFunction to invoke when url part matches.
feOptional else function if url part doesn't match.
path example

See route usage and route example.

navigate

navigate(l) navigate(l, false)

Navigates to the location l using pushState and checks to see if the url changed in which case the route function is executed.

The function takes an optional second boolean argument that can be used to supress the execution of the route function.

This function is lazy when used inside route, only the last location will be used when the route function finishes.

:: string -> undefined :: string, boolean -> undefined

argdesc
lThe (string) location to navigate to. Can be relative.
tOptional boolean set to false to supress route function triggering.
retalways undefined
navigate example
// if browser is at "http://my.host/some/where"

navigate('other')   // changes url to "http://my.host/some/other"
navigate('/news')   // changes url to "http://my.host/news"


navigate('/didnt', false) // changes url to "http://my.host/didnt"
                          // without running the route function

License

The MIT License (MIT)

Copyright © 2016 Martin Algesten

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

router

FAQs

Package last updated on 24 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts