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

frampton-router

v0.1.1

Published

A module for navigating a SPA with frampton.js

Downloads

3

Readme

Frampton-Router

Parsers

Create a parser to match a path. A parser is a function that takes a path and returns a Maybe. If the parser matches the path the function returns a Just, otherwise a Nothing. When you create a parser you give it a string describing the format of the path to match. The parser matches most string exactly with strict equals. However, if you use the dynamic path segments of ':number' or ':string' the parser will match the dynamic segment and return it as the value of the Just. The value of the Just is always an array. If there are no dynamic segments the array is empty. If there are dynamic segments they are returned in the array in the order in which they occur in the path.

const parser = Frampton.Router.parser;

const postParser = parser('/post/:number');
const maybeMatch1 = postParser('/post/34');
const maybeMatch2 = postParser('/fake/route');
console.log(maybeMatch1); // -> 'Just(34)'
console.log(maybeMatch2); // -> 'Nothing'

To create a parser that matches on of many patterns use the oneOf helper.

const oneOf = Frampton.Router.oneOf;
const parser = Frampton.Router.parser;

const postListParser = parser('/post');
const postDetailParser = parser('post/:number');
const postEditParser = parser('post/:number/edit');
const postRouteParser = oneOf([ postListParser, postDetailParser, postEditParser ]);

const maybeMatch1 = postRouteParser('/post');
const maybeMatch2 = postRouteParser('/post/34');
const maybeMatch3 = postRouteParser('/post/34/edit');

console.log(maybeMatch1); // -> 'Just()'
console.log(maybeMatch2); // -> 'Just(34)'
console.log(maybeMatch3); // -> 'Just(34)'

That's all well and good, but how do we tell which of the parsers actually matched? We can use the map function to tag the matched route and format the return data.

const map = Frampton.Router.map;
const oneOf = Frampton.Router.oneOf;
const parser = Frampton.Router.parser;

const tagPostList = () => ({
    route: 'PostList'
  });

const tagPostDetail = (data) => ({
    route: 'PostDetail',
    id: data[0]
  });

const tagPostEdit = (data) => ({
    route: 'PostEdit',
    id: data[0]
  });

const postListParser = map(tagPostList, parser('/post'));
const postDetailParser = map(tagPostDetail, parser('post/:number'));
const postEditParser = map(tagPostEdit, parser('post/:number/edit'));
const postRouteParser = oneOf([ postListParser, postDetailParser, postEditParser ]);

const maybeMatch1 = postRouteParser('/post');
const maybeMatch2 = postRouteParser('/post/34');
const maybeMatch3 = postRouteParser('/post/34/edit');

console.log(maybeMatch1); // -> 'Just({ route: 'PostList' })'
console.log(maybeMatch2); // -> 'Just({ route: 'PostDetail', id: 34 })'
console.log(maybeMatch3); // -> 'Just({ route: 'PostEdit', id: 34 })'

App

An app handles a lot of the boiler plate wiring required when setting up a single page app. When the app starts up the app will call a urlParser function you provide to determine the correct route. The value returned by the parser function is expected to indicate to your app what to do and what to render. This return value is used as the parameter to the init function. The init function should return the initial application state and any tasks that need to be run at application start.

Anytime the url changes the urlParser function will be called again. This time the urlUpdate function will be called with two parameters, the return value of the urlParser function and the current application state. The urlUpdate function should then return a new application state and any tasks that need to be run as a result of the url changing.

const App = Frampton.Router.app;
const parser = Frampton.Router.parser;
const map = Frampton.Router.map;
const Never = Frampton.Data.Task;
const { div, section, text } = Frampton.DOM.Html;

const tagPostList = () => ({
    router: 'PostList'
  });

const tagPostDetail = (data) => ({
    router: 'PostDetail',
    id: data[0]
  });

const parsePostList = map(tagPostList, parser('/posts'));
const parsePostDetail = map(tagPostDetail, parser('/post/:number'));
const postRouteParser = oneOf([ parsePostList, parsePostDetail ]);

// urlParser will receive the window.location object
function urlParser(loc) {
  // The parser will return a Maybe. Fork will return the value of
  // the first callback for a Just or the value of the second callback
  // for a Nothing.
  return postRouteParser(loc.pathname).fork(
    (success) => success,
    () => ({ route: 'NotFound' })
  );
}

// The init function is called with the initial result of the urlParser. The
// return value of the init function is expected to be a tuple of the initial
// application state and any initial tasks to run.
function init(route) {
  const initialState = { route: route, username: 'Larry' };
  const initialTask = Never();
  return [ initialState, initialTask ];
}

// Update state based on messages
function update(msg, state) {
  switch(msg) {
    case 'click happened':
      const newState = { name : 'Bob' };
      return [ newState, Never() ];

    default:
      return [ state, Never() ];
  }
}

// After parsing the urlUpdate function is called with the value returned from
// parsing and the current state.
function urlUpdate(route, state) {
  const newState = {
    route: route,
    username: state.username
  };
  return [ newState, Never() ];
}

// Render view based on state
// Return values of event handlers are fed to update function as messages
function view(state) {

  switch (state.route) {

    case 'PostDetail':
      const clickHandler = (evt) => {
        return 'click happened';
      };

      return div({}, [
        h2('Post Detail Page'),
        section({ onClick : clickHandler }, [ text('click me') ])
      ]);
      break;

    case 'PostList':
      return div({}, [
        h2('Post List Page')
      ]);
      break;

    default:
      return div({}, [
        h2('404 Not Found')
      ]);
      break;
  }
}

// Create app
const app = App({
  init: init,
  update: update,
  urlUpdate: urlUpdate,
  urlParser: urlParser,
  view: view,
  rootElement: document.getElementById('app-root')
});

// The return value is a Signal of the current app state
app.value((currentState) => {
  console.log('The current app state: ', currentState);
});