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

help-my-views

v0.1.1

Published

A collection of helpful view helpers.

Downloads

2

Readme

help-my-views

A collection of Node.js view helpers

Installation

npm install help-my-views --save

Usage

Typically you would include this in your req.locals so you have access to it within your views. An example init might be something like this:

var express = require('express');
var app = express();

//Attach the data that is needed for every request
app.use(function(req, res, next) {
  req.locals = {};
  req.locals.helpers = require('help-my-views');
});

app.get('/', function (req, res) {
  res.render('views/home', req.locals);
});

app.listen(3000);

Now you have access to all the view helpers in your view, as shown here for an example Jade template:

html
  head
    title My Awesome Site

  body
    - var myCats = ['Fluffy', 'Mr. Snuggles', 'Ms. Fancy Feet']

    h1 #{helpers.titleCase('I love my ' + myCats.length + ' ' + helpers.pluralize('cat', myCats)}

    - var nav = helpers.linkify('/kitties?cat=', myCats)
    
    h4 Pick a Kitty!
    ul
      for link in nav
        li
          != link

The Helpers

escape(html)

Removes the HTML from a string. Very helpful for user generated content.

> helpers.escape('Hello, <em>World!</em>');
'Hello, &lt;em&gt;World!&lt;/em&gt;'

unescape(text)

Does the opposite of escape.

> helpers.unescape('I &lt;3 burritos!');
'I <3 burritos!'

previewText(html, length)

This takes a string, stripes out any HTML, and returns the first # of characters specified in length followed by an ellipsis. If it would cut off the string in the middle of a word, it "rounds down" to the previous word. This is helpful for showing preview descriptions of blog posts, product descriptions, etc.

> helpers.previewText("When in the Course of human events,<br>it becomes necessary for one people<br>to dissolve the political bands which have connected ...", 100); 
'When in the Course of human events, it becomes necessary for one people to dissolve the political . . .'

previewImage(html, index, origin)

This finds the first (or a specified 0-based index) image in HTML content. Useful for the same things you'd use previewText() for. If you supply an origin URL it will attempt to convert relative URLs to absolute.

> helpers.previewImage("<p>Here's my favorite kitty!!!!</p><img src='/img/mrfluffy.jpg', alt='OMG SO CUTE'>");
{ src: '/img/mrfluffy.jpg', alt: 'OMG SO CUTE' }

> helpers.previewImage("<img src='/img/sadFace.jpg', alt=':('><p>This one is just okay</p><img src='/img/mrbitey.jpg', alt='kinda mean'>", 1, "https://mysite.com/");
{ src: 'https://mysite.com/img/mrbitey.jpg', alt: 'kinda mean' }

limit(text, length)

A very simple function that limits the length of a string to a set number of characters. It adds an ellipsis if needed. This is nice to use when your design allows for a fixed number of characters and you don't know exactly what the input will be. It's similar to previewText() but it doesn't remove HTML and it doesn't "round down" to the previous word.

> helpers.limit('This_is_my_really_annoyingly_long_username', 15);
'This_is_my_r...'

titleCase(text)

It transforms your string into Title Case where the first character of each word is capitalized and the rest is lower case. Note that each word is capitalized, even the little ones. So you'd get, "To Kill A Mocking Bird" and not "To Kill a Mocking Bird".

> helpers.titleCase('MAKE mE SoMe title caSE');
'Make Me Some Title Case'

fractions(text)

It takes a string and transforms some of the decimal numbers into pretty fractions using <sup>'s and <sub>'s. It will transform every number that it can in your string. Currently it supports: halves, thirds, fourths, fifths, sixths, eighths and tenths since these are likely the most common, but it's easy to add more if needed. For numbers with repeating decimals (i.e. 1.3333333 ...), it checks the first 3 decimal digits.

This helper is useful if you'd like to display pretty fractions for a recipe site, or maybe if you sell something that can be divided up.

> helpers.fractions('Add 1.5t salt to 1.333T butter and stir');
'Add 1 <sup>1</sup>/<sub>2</sub>t salt to 1 <sup>1</sup>/<sub>3</sub>T butter and stir'

linkify(prepend, list)

This takes an array of strings and converts it into an array of links. It uses the string for the anchor text, and builds the URL by prepending the prepend to each string. Useful for navs or turning blog post tags or categories into links.

> helpers.linkify('/blog/tags/', [ 'cats', 'dogs', 'pizza' ]);
[ '<a href="/blog/tags/cats">cats</a>',
  '<a href="/blog/tags/dogs">dogs</a>',
  '<a href="/blog/tags/pizza">pizza</a>' ]

pluralize(word, check, custom)

It takes the singular form of the supplied word, and makes it plural based on the check. The check turns the word into a plural with the behavior based on type: a number (not 1), an object (not one property), an array (not one item) or a boolean (not false). It forms the plural based on standard English grammar rules, and has a lookup table for irregular words. Since there's no possible way that I've captured every irregular word, you can supply your own custom dictionary to override the built in rules.

This is helpful when you want your text to read more like proper English when you're generating it.

> helpers.pluralize('cat', [ 'Lil Finch', 'Meow Radley' ]);
'cats'

> helpers.pluralize('dog', [ 'Spoticus' ]);
'dog'

> helpers.pluralize('loaf', 6);
'loaves'

> helpers.pluralize('fish', true, { fish: 'fishies' });
'fishies'

Tests

npm test

Contributing

Have a cool and helpful view helper you'd like to add? Perfect! Fork and send a pull request. Make sure to test and jshint your code.

Release History

  • 0.1.0 Initial release

License

MIT