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

musthave

v1.0.2

Published

A minimal utility module to throw errors when an object does not contain "must have" keys.

Downloads

45

Readme

Build Status

musthave

This module provides a simple and standard way of checking for object attributes. The inspiration for this project was checking for the existence of environment variables.

Installation: npm install musthave

This module contains minimal dependencies and helps keep npm fit.

Use: HasAll

If your application requires the presence of a couple of envrionment variables, it's a good idea to make sure they exist as a simple sanity check when your application launches. The following code will log a warning to the console and throw an error if IMPORTANT_VAR_1 and IMPORTANT_VAR_2 aren't in the environment variables.

var MustHave = require('musthave');
var mh = new MustHave();

mh.hasAll(process.env, 'IMPORTANT_VAR_1', 'IMPORTANT_VAR_2'); // returns boolean

If the hasAll method fails, the missing attributes are available in mh.missing (returned as an array of strings).

Use: HasAny

If you need to make sure an object has at least one attribute of a specific name, use the hasAny() method.

var MustHave = require('musthave');
var mh = new MustHave();

mh.hasAny(process.env, 'IMPORTANT_VAR_1', 'IMPORTANT_VAR_2'); // returns boolean

The code above will throw an error only if process.env does NOT have IMPORTANT_VAR_1 or IMPORTANT_VAR_2.

Use: HasExactly

If you need to make sure an object has only a specific set of named attributes, use the hasExactly() method.

var MustHave = require('musthave');
var mh = new MustHave();

mh.hasExactly(process.env, 'IMPORTANT_VAR_1', 'IMPORTANT_VAR_2'); // returns boolean

The code above will not throw and error only if process.env has IMPORTANT_VAR_1 and IMPORTANT_VAR_2. If process.env has both of these and another attribute like NODE_ENV, this method will throw an error because the object is not exactly as defined. You probably don't want to use this on process.env because it can change from system to system, but it can be useful on other kinds of objects like data models.

Handling Errors & Suppressing Warnings

If you want to suppress warning messages, the MustHave module can be configured to do so. It can also be configured to not throw errors so your app can handle them as it sees fit. To accomodate these use cases, there are two configuration options that can be passed to the MustHave() object. Not providing a configuration is the same as:

var MustHave = require('musthave');
var mh = new MustHave({
  throwOnError: true,      // Set this to false if you want to handle errors on our own
  suppressWarnings: false  // Set this to true if you don't want console output for warnings.
});