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

whit

v0.0.3

Published

A minimal programmatic view library with zero dependencies.

Downloads

1

Readme

Note: This is an alpha release of whit with incomplete documentation.

import html, { body } from "whit-html";
import form, {textField, select, option, checkbox, clearValue} from "whit-form";

body.append(
  form("http://myserver.com/users", "post",
    textField("Name:", "user[name]", "Your name here.")
      .clearValueOnFocus,

    textField("Email:", "user[email]", "Your email here.")
      .clearValueOnFocus,

    select("Favorite Color:", "user[favorite_color]",
      option("Red").default,
      option("Orange"),
      option("Yellow"),
      option("Green"),
      option("Blue"),
      option("Violet")
    ),

    checkbox("Join our Mailing List?", "user[mailing_list]"),

    submit("Submit to Enter Contest")
  ).on("submit", (formView, event) => {
    event.preventDefault();
    view.
  })
);

Features

  • Zero Dependencies
  • No templates. Markup is generated automatically.
  • 100% Vanilla-JS
  • 6.1Kb Uncompressed
  • 100% Test Coverage
  • High-Quality Source Code & Test Files that are simple to read, understand, and extend.
    • Cyclomatic Complexity under 3
    • Halstead Time under 36 minutes
    • Maintainability Index over 100
    • 36 or less logical lines of code for all files

Introduction

There are two primary ways to use Whit depending on the syntax you want to work with.

Factory Syntax

  • Create factory functions that instantiate views and return them for you.
  • Lets you create a custom view DSL using vanilla-js.
  • Simplifies creation and combination of views.

./app.js :

import { orderedList, listItem, body } from "./views.js";

body.append(
    orderedList(
        listItem("One"),
        listItem("Two"),
        listItem("Three")
    )
);

./views.js :

import View from "whit";

export function orderedList(...children) {
    return new class OrderedList extends View {
        initialize(options) {
            options.tag = "ol",
            options.children = children;
        }
    };
}

export function listItem(labelText) {
    return new class ListItem extends View {
        initialize(options) {
            options.tag = "li"
        }
    }
}

const body = new class Body extends View {
    initialize(options) {
        // Get an existing element instead of creating a new one.
        options.element = document.getElementByTagName("body")[0]
    }
};

export { body };

Class Syntax

  • See exactly what's going with nothing masked.
  • Less complex than creating a factory wrapper for each view.

./app.js :

import { OrderedList, ListItem, Body } from "./views.js";

const body = new Body();

const orderedList = new OrderedList({
    children: [
        new ListItem({ contents: "One" }),
        new ListItem({ contents: "Two" })
        new ListItem({ contents: "Three" })
    ]
});

body.append(orderedList);

./views.js :

import View from "whit";

export class OrderedList extends View {
    initialize(options) {
        options.tag = "ol"
    }
}

export class ListItem extends View {
    initialize(options) {
        options.tag = "li"
    }
}

export class Body extends View {
    initialize(options) {
        // Get an existing element instead of creating a new one.
        options.element = document.getElementByTagName("body")[0]
    }
}

Events

Attach event handlers to views with .on, then trigger them with .trigger.

import View from "whit";

const myView = new View();

myView.on("some-event", (view, event) => alert("Yep. It works."))

myView.trigger("some-event");

Whit will trigger the mount event when a view is mounted to the DOM:

import View from "whit";

class Body extends View {
    initialize(options) {
        options.element = document.getElementByTagName("body")[0]
    }
}

const bodyView = new Body();
const myView = new View();

myView.on("mount", (view, event) => alert("I'm now mounted in the DOM!"))

bodyView.append(myView);

Attributes

Change attributes easily

import View from "whit";

const myView = new View();

myView.on("some-event", (view, event) => alert("Yep. It works."))

myView.trigger("some-event");

Whit will trigger the mount event when a view is mounted to the DOM:

import View from "whit";

class Body extends View {
    initialize(options) {
        options.element = document.getElementByTagName("body")[0]
    }
}

const bodyView = new Body();
const myView = new View();

myView.on("mount", (view, event) => alert("I'm now mounted in the DOM!"))

bodyView.append(myView);

Getting Started

Installation

npm install whit --save

Creating & Displaying Views

  1. Import the View class from whit.
  2. Extend the View class for each of your application's views.
  3. Use the .initialize method ( not .constructor ) to customize each view's options.
  4. Instantiate & combine views as required.
  5. Append the view to your document.
import View from "whit";

class OrderedList extends View {
    initialize(options) {
        options.tag = "ol"
    }
}

class ListItem extends View {
    initialize(options) {
        options.tag = "li"
    }
}

class Body extends View {
    initialize(options) {
        // Get an existing element instead of creating a new one.
        options.element = document.getElementByTagName("body")[0]
    }
}

const orderedList = new OrderedList({
    children: [
        new ListItem({ contents: "One" }),
        new ListItem({ contents: "Two" })
        new ListItem({ contents: "Three" })
    ]
});

const body = new Body();

body.append(orderedList);

Cleaner Syntax w/ Factories

Add an ID

import View from "whit";

const view = new View();

view.toString(); // <div></div>

Render to String

<html>
  <body>
    <form action="http://myserver.com/users" method="post">
      <ol>
        <li class="text">
          <label for="user_name">Name:</label>
          <input id="user_name" type="text" name="user[name]" value="Your name here." />
        </li>
        <li class="text">
          <label for="user_email">Email:</label>
          <input id="user_email" type="text" name="user[email]" value="Your email here." />
        </li>
        <li class="select">
          <label for="user_favorite_color">Favorite Color:</label>
          <select id="user_favorite_color" type="text" name="user[favorite_color]">
            <option value="Red">Red</option>
            <option value="Orange">Orange</option>
            <option value="Yellow">Yellow</option>
            <option value="Green">Green</option>
            <option value="Blue">Blue</option>
            <option value="Violet">Violet</option>
          </select>
        </li>
        <li class="checkbox">
          <label for="user_mailing_list">Join our Mailing List?</label>
          <input id="user_mailing_list" type="checkbox" name="user[mailing_list]" />
        </li>
        <li class="submit">
          <input type="submit" value="Submit to Enter Contest" />
        </li>
      </ol>
    </form>
  </body>
</html>