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

@butility/router

v1.0.0

Published

Router for the browser with many features from @butility

Downloads

2

Readme

@butility/router

A lightweight and flexible client-side router for building single-page applications (SPAs) with TypeScript. This router allows developers to define routes dynamically and supports HTML files, view classes, and functions for rendering content.

Features

  • Dynamic Route Matching: Easily match routes with parameters (e.g., /user/:id).
  • Multiple Route Handlers: Use HTML file paths, view classes, or functions to render content.
  • Custom 404 Handling: Configure a custom 404 page to handle not found routes.
  • Meta Tags Management: Set metadata for each route for better SEO.
  • Relative Path Calculation: Automatically adjusts paths based on the current route.

Installation

To install the package, you can use npm or CDN:

npm install @butility/router
<!-- To use all the functions and methods -->
<script src="https://unpkg.com/@butility/router@latest/router.js" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/@butility/router@latest/router.js" type="module"></script>

Usage

Basic Setup

  1. Import the Router and View:

WE used typescript for example but for JavaScript just remove the types, other is okay!

import Router, { View } from "@butility/router";
import type { Routes } from "@butility/router";
  1. Define a View Class (optional):
class UserProfileView extends View {
    public render(params: Record<string, string>) {
        const userId = params.id; // Accessing the :id param
        document.body.innerHTML = `
            <h1>User Profile</h1>
            <p>Welcome, user #${userId}!</p>
        `;
    }
}
  1. Define Your Routes:
const routes: Routes = {
    "/": "./index.html",  // HTML file path for the root route
    "/user/:id": new UserProfileView(),  // Using a View class
    "/login": "./login.html" // Another HTML file
};
  1. Create an Instance of the Router:
const router = new Router(routes);
  1. Set a Custom 404 Route:
router.set404("./404.html");
  1. Navigate to Routes Programmatically:
router.navigate("/user/123");  // Navigates to user profile with id 123

Example

Here’s a complete example that uses a function as a route handler:

function UserProfileFunction(params: Record<string, string>) {
    const userId = params.id; // Accessing the :id param
    const div = document.createElement("div");
    div.innerHTML = `
        <h1>User Profile</h1>
        <p>Welcome, user #${userId}!</p>
    `;
    return div;
}

const routes: Routes = {
    "/home": "./home.html",
    "/user/:id": UserProfileFunction,
};

const router = new Router(routes);
router.set404("./404.html");
router.navigate("/user/456"); // Navigates to user profile with id 456

API

For @butility/router documentation refer to the docs folder.

Contributing

Feel free to contribute by submitting issues or pull requests to improve this library.

License

This project is licensed under the MIT License.