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

cv3-route

v0.0.3

Published

Dependency injection code.

Downloads

3

Readme

avatar

cv3-route

Simple routing for ES6.

cv3-route Travis (.org) Downloads Badge Size badge

Install

$ npm install cv3-route

Usage

Creating routers

Inject your routes into the Router class to produce a routable subclass.

Scalar values will be returned directly, methods will be evaluated & returned.

[false] will be called if no other routes match.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	index:     'Hello, world!'
	, other:   'Other route!'
	, [false]: 'Not found!'
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};

Routing

Pass a cv3-route/Path object to the route method on the Router object

import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const emptyPath = new Path('');
const indexPath = new Path('index');
const otherPath = new Path('other');
const errorPath = new Path('does-not-exist');

console.log(exampleRouter.route(emptyPath));
console.log(exampleRouter.route(indexPath));
console.log(exampleRouter.route(otherPath));
console.log(exampleRouter.route(errorPath));

Routing to methods

You can route to methods instead of directly returning strings. The router object will be passed as the first parameter, and the current path will be passed as the second parameter.

Internal methods may be called on the router.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	'do-something': (router, path) => {
		return 'did something.';
	}

	, 'do-something-else': (router, path) => {
		return router.internalMethod();
	}
};

export class ExampleRouter extends Inject(Router, {routes})
{
	internalMethod()
	{
		return 'did something.';
	}
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const somethingPath = new Path('do-something');
const somethingElsePath = new Path('do-something-else');

console.log(exampleRouter.route(somethingPath));
console.log(exampleRouter.route(somethingElsePath));

Regex Routing

Regex based routing can be achieved with computed property names.

The [square bracket] syntax allows us to maintain regex syntax highlighting in the IDE. Quotes may also be used. Any property starting with a / will be processed as a regex path.

If a method is supplied, it will be called with the match groups from the regex operation as the 3rd parameter.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	[/^regex-([-\w]+)$/]: (router, path, matchGroups) => {

		return matchGroups[1].split('-').join(',');
	}
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const onePath   = new Path('regex-one');
const twoPath   = new Path('regex-one-two');
const threePath = new Path('regex-one-two-three');

console.log(exampleRouter.route(onePath));
console.log(exampleRouter.route(twoPath));
console.log(exampleRouter.route(threePath));

Nested Routing

Path objects split their source string on /. This allows us to do nested routing.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const nestedRoutes = {
	index: 'Nested index.'
	, sub: 'Nested route.'
}

const routes = {
	nested: Inject(Router, {routes: nestedRoutes})
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const nestedIndexPath = new Path('nested');
const nestedRoutePath = new Path('nested/sub');

console.log(exampleRouter.route(nestedIndexPath));
console.log(exampleRouter.route(nestedRoutePath));

Routing with promises

Promises will be passed through, so promise-based routing is simple:

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	promise: new Promise((accept, reject) => {
		accept('fulfilled.');
	})
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;
const promisePath   = new Path('promise');

exampleRouter.route(promisePath).then((result) => {
	console.log(result);
}).catch((error) => {
	console.error(error);
});

License

cv3-route © Sean Morris 2019

All code in this package is relased under the Apache 2.0 licence.