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

blain

v0.0.3

Published

![Blain](./docs/blain.jpg 'Logo Title Text 1')

Downloads

3

Readme

Blain

Those functions are looking at you funny.

Blain: It's payback time!

NPM Version codecov Build Status DUB Greenkeeper badge Node Security

When testing APIs or functions generally you often need to test how they behave when unsupported values are passed in. It can often be tempting to pick a value you know doesn't work and just test that:

const f = s => {
  if (typeof s === `string`) {
    return `Its a String`
  }

  if (Array.isArray(s)) {
    return `Its an Array`
  }

  throw new Error(`Supplied value was invalid`)
}

const value = null
expect(() => f(value)).Throw(`Supplied value was invalid`)

However, there are often many other unsupported values, and just because the function throws correctly when encountering null doesn't mean it will throw correctly when encountering a RexExp object or NaN.

Blaine makes it easy to get your hands on a wide variety of values to test your functions. It offers a variety of groups out of the box and makes it easy to combine and edit those groups or add your own data.

So using the example above, and assuming the function only supports strings or arrays, we can do:

import blain, { keys } from 'blain'

const f = s => {
  if (typeof s === `string`) {
    return `Its a String`
  }

  if (Array.isArray(s)) {
    return `Its an Array`
  }

  throw new Error(`Supplied value was invalid`)
}

const values = blain({ baseGroup: [keys.ALL] }).exclude(
  keys.STRINGS,
  keys.ARRAYS
)

for (const value of values) {
  expect(() => f(value)).toThrow(`Supplied value was invalid`)
}

Overview

Using blain involves two steps.

  1. Call blain(), passing configuration (if required).
  2. Call one of the API methods on the returned object.

Config

Blain accepts a configuration object with two valid keys:

baseGroup (Array) defaults to []

baseGroup allows you to set up the default data returned from the API. You can then use exclude() to remove values from this data that you don't want.

It accepts an array of values. These values can either be arbitrary values, or group keys. Blain provides a large number of groups. Each group contains one or more values. Some groups contain only a single value, for example keys.NULL, and some groups contain multiple values, for example keys.NIL which combines the groups keys.NULL, keys.UNDEFINED and keys.NAN. There is a keys.All which contains all values provided by Blain.

groups (Object) defaults to {}

groups allows you to supply your own groups for use with the API. Values passed to the API methods are checked against existing groups and any groups you have supplied. For this reason it is important that you the keys for your groups are unique to avoid clashing with generic values that might be passed. Blain exposes a curried toUID function that you can use to create unique ids (see below). If you provide a group with an ID matching one of blain's own group IDs it will replace that group.

API

Blain has a very simple API. An object is returned from blain() containing two functions:

include (...args): Array

include allows you to add additional values to the baseGroup you defined during configuration. Blain will check each value against its map of groups. If the value matches a key, it will add the data from the matching group to the data already defined in baseGroup. Any values that don't key to a group will also be included. The returned array will therfore contain:

  • Data defined in baseGroup
  • Data from any groups matched by supplied values
  • Any remaining values that didn't match to groups

exclude (...args): Array

exclude allows you to remove values from the baseGroup you defined during configuration. Blain will check each value against its map of groups. If the value matches a key, it will remove the data from the matching group from the data already defined in baseGroup, if it exists. Any values that don't key to a group will also be removed. The returned array will therfore contain data defined in baseGroup with the following removed:

  • Data from any groups matched by supplied values
  • Any remaining values that didn't match to groups