npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ways

v0.5.0

Published

Fluid router specially designed for complex page transitions and granular UI animations

Downloads

9

Readme

Build Status Coverage Status Code Climate Dependency Status

Sauce Test Status

Ways

Fluid router specially designed for complex page transitions and granular UI animations.

But not only that.

Installation

# node
npm install ways # --save, --save-dev

# bower
bower install ways # --save, --save-dev

# meteor
meteor add arboleya:ways # <- Ways is exported with a **capital** W!

Basics

Basic signature is ways(pattern, handler).

In Meteor, consider Ways is exported with a capital W.

var ways = require('ways');

// simple route
ways('/pages', function(req){
  // req.pattern, req.url, req.params
});

// named params
ways('/pages/:id', handler);

// splat params
ways('/pages/:id/tags/*tags', handler);

// optional params
ways('/pages/:id?', handler);
ways('/pages/:id?/tags/*tags?', handler);

// match-all
ways('*', handler);

// initialize with current url
ways.go(ways.pathname());

AddressBar

By default Ways doesn't offers support for /pushState and #hash, there's no browser dependency whatsoever. Therefore you can use it wherever you want to, even on the server. Or keep in the client, but without affecting urls.

However, sometimes you'll want to activate addressbar support, like this:

// activate addressbar support
ways.use(ways.addressbar);

And you're done.

Go

Redirects app.

// ways.go(url, [title, [state]]);
ways.go('/pages');
ways.go('/pages', 'Page Title');
ways.go('/pages', 'Page Title', {foo: 'bar'});

Go Silent

Same as ways.go, but in silent mode, without triggering any route.

// ways.go.silent(url, [title, [state]]);
ways.go.silent('/pages');
ways.go.silent('/pages', 'Page Title');
ways.go.silent('/pages', 'Page Title', {foo: 'bar'});

Think about go() = pushState, go.silent() = replaceState

Pathname

Gets current pathname.

// ways.pathname();
ways.go(ways.pathname());

Flow mode

Connect your routes altogheter, creating a dependency graph between them.

Lets say you have three routes:

ways('/a', function (req) { /* ... */ });
ways('/b', function (req) { /* ... */ });
ways('/c', function (req) { /* ... */ });

Now lets assume that /c depends on /b that depends on /a.

So when we call /c, we really want to execute:

  1. First /a
  2. Then /b
  3. And finally /c

That's what flow based mode would do for you.

And more:

  • Routes' execution occurs asynchronously and sequentially
  • Dependency chain is computed automatically, no more routes' hell
  • Pack your projects with granular UI animations and complex page transitions
  • Forget the Layout <- View paradigm, embrace the View <-> View reality

TODO: Maybe explain wtf is View <-> View

Activation

The passed mode tell the order things should run.

// ways.flow(mode);
ways.flow('destroy+run'); // destroy first, run after
ways.flow('run+destroy'); // run before, destroy after

Don't panic, continue reading.

Signature changes

In flow mode, the routes can be run or destroyed and signature changes a little. You must pass two handlers instead of one: a runner and a destroyer.

Optionally, you may also (most probably) pass a dependency.

// ways(pattern, run, destroy, [dependency])

var ways = require('ways');

ways.flow('destroy+run');

function run(req, done){
  console.log('rendering', req);
  done();
}

function destroy(req, done){
  console.log('destroying', req);
  done();
}

ways('/', run, destroy);
ways('/pages/:id', run, destroy, '/'); // [1]
ways('/pages/:id/edit', run, destroy, '/pages/:id'); // [2]

// [1] 'pages/:id' depends on '/'
// [2] '/pages/:id/edit' depends on '/pages/:id'

Both handlers (run and destroy) will receive two params when called:

  • req - infos about the request
  • done- callback to be called when route finishes running or destroying

Example

Lets take a look at a full example:

var ways = require('ways');

ways.flow('destroy+run');

var running = '+ RUN url=%s, pattern=%s, params='
var destroying = '- DESTROY url=%s, pattern=%s, params='

var run = function(req, done) {
  console.log(running, req.url, req.pattern, req.params);
  done();
};

var destroy = function(req, done) {
  console.log(destroying, req.url, req.pattern, req.params);
  done();
};

ways('/', run, destroy);
ways('/pages', run, destroy, '/');
ways('/pages/:id', run, destroy, '/pages');
ways('/pages/:id/edit', run, destroy, '/pages/:id');
ways('*', run, destroy); // <- this is a catch all

Ok, now lets start our navigation:

Step 1

// pretend our firt and current url is '/pages/33/edit',
// we'll use `ways.pathname()` to get it

ways.go(ways.pathname());

This will produce the following output:

+ RUN url='/', pattern='/', params= Object {}
+ RUN url='/pages', pattern='/pages', params= Object {}
+ RUN url='/pages/33', pattern='/pages/:id', params= Object {id: "33"}
+ RUN url='/pages/33/edit', pattern='/pages/:id/edit', params= Object {id: "33"} 

At the beggining there's no route to be destroyed, so the dependency chain is computed and every route gets executed, one after another, asynchronously.

Step 2

ways.go('/pages/22/edit');

This will produce the following output:

- DESTROY url='/pages/33/edit', pattern='/pages/:id/edit', params= Object {id: "33"}
- DESTROY url='/pages/33', pattern='/pages/:id', params= Object {id: "33"}
+ RUN url='/pages/22', pattern='/pages/:id', params= Object {id: "22"}
+ RUN url='/pages/22/edit', pattern='/pages/:id/edit', params= Object {id: "22"}

Here we have two routes being destroyed before running the new ones, this is computed again based on the dependency chain. In this case, useless routes are destroyed before running the new ones, the opposite is achieved by passing the mode run+destroy.

Step 3

ways.go('/any/route/here');

This will produce the following output:

- DESTROY url='/pages/22/edit', pattern='/pages/:id/edit', params= Object {id: "22"}
- DESTROY url='/pages/22', pattern='/pages/:id', params= Object {id: "22"}
- DESTROY url='/pages', pattern='/pages', params= Object {}
- DESTROY url='/', pattern='/', params= Object {}
+ RUN url='/any/thing/here', pattern='*', params= Object {} 

As the route in question here has no dependencies, note that every other route needs to be destroyed before it runs.

Events

There's only one global event you can listen to.

ways.on('url:change', function(url){
  console.log('current url is', url);
});

Restricted urls

A simple way to have restricted urls would be like:

function auth(done){
  // your logic here
  done(true);
}

function restrict(action) {
  return function(req, done) {
    auth(function(authorized) {
      if(!authorized)
        ways.go('/login');
      else
        action(req, done);
    });
  }
}

ways('/pages/secret', restrict(run), destroy)

License

The MIT License (MIT)

Copyright (c) 2013 Anderson Arboleya

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.