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

@elijahcode/router

v2.1.1

Published

[![Coverage Status](https://coveralls.io/repos/github/ElijahCode/Router/badge.svg?branch=development)](https://coveralls.io/github/ElijahCode/Router?branch=development)

Downloads

16

Readme

Coverage Status

elijahCode/Router

Description

In this package realised API that work with url in your browser. It' based on Hash (HashRouter) and History (HistoryRouter) API's.

Also that package support work next hook's: onEnter, onLeave, onBeforeEnter.

Installation

You can installing package with using CLI. For it run next command in terminal:

              npm install @elijahCode/Router

Usage

Adding to module

For adding packge to module, put next code in this:

import { createRouter } from "@elijahcode/router";

Create new router

You can create a new router with using create router function:

const hashRouter = createRouter("hash");
// or
const historyRouter = createRouter("history");

Argument of this function is literall "hash" or "history". Return value is object that have HashRouter or HistoryRouter parent's classes.

If you using JavaScript, you can also pass in function any data. In this case function return null.

HashRouter.go and HistoryRouter.go

This methods let you go to url, that you define as first argument. In second argument you can pass a parameters for hooks, that run on this page. If you want to learn more about hooks, read section "HashRouter.on and HistoryRouter.on".

This method is asyncronic.

Arguments must satisfy next type's:

type firstArgumet = string;
interface secondArgument {
  onEnter?: any[];
  onLeave?: any[];
  onBeforeEnter?: any[];
}

This method call similar for routers, that you create with using HashRouter and HistoryRouter.

Example:

// without using arguments for hooks
await someRouter.go("/main/about");
// with using arguments for hooks
await someRouter.go("/main/contacts", {
  onEnter: [argument1, argument2, argument3],
});

HashRouter.on and HistoryRouter.on

This method let you to set hooks, that will be run if conditions is satisfied.

Also as HashRouter.go and HistoryRouter.go, this methods call similar for all router.

Methods have next signatire:

someRouter.on(firstArgument, secondArgument);

Arguments have next type's:

type FirstArgument = string | RegExp | UrlFunction
interface UrlFunction = {
  (url?: string): boolean;
}

interface SecondArgument = {
  onEnter?: Hook | AsyncHook;
  onLeave?: Hook | AsyncHook;
  onBeforeEnter?: Hook | AsyncHook;
}
interface Hook {
  (...args: any[]): () => void;
}
interface AsyncHook {
  (...args: any[]): Promise<() => void> | Promise<void>;
}

How you can see, as addres of page you pass string, regular expression or function and as hooks yoy can pass synhronic or asynhronic function.

Now describe when every begin work:

  1. onEnter - it's works then trunsition occurs on url, that pass as first argument;
  2. onLeave - it's works then trunsition occurs from url, that pass as first argument;
  3. onBeforeEnter - it's works before trunsition occurs to url, that pass as first argument;

Example:

someRouter.on("/main/contacts", {
  onEnter: () => console.log("It go to /main/contacts"),
  onLeave: () => console.log("It leave /main/contacts"),
  onBeforeEnter: () => console.log("It prepare go to /main/contacts"),
});

await someRouter.go("/main/contacts");
// -> It prepare go to /main/contacts
// -> It go to /main/contacts

await someRouter.go("/main/about");
// -> It leave /main/contacts

If you want watch more examples of using this package, you can go to github repository of this package and watch tests

ChangeLog

2.1.0

  1. Now then you initialize new router it doesn't automatically go in root path of your page.
  2. Minor code changes.

2.0.1

  1. Minor code changes.

2.0.0

  1. Now then you initialize new router it doesn't need root path.
  2. Add createRouter function.
  3. Add new types.
  4. Add urlFilter function for optimization code

1.0.0 Release