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

assertions

v2.3.4

Published

loads of useful assert functions in one package

Downloads

1,551

Readme

Assertions

assertions is a powerful assertion toolkit.

there are many useful assertion functions,
including a few very useful higher order assertion functions.
also, every function has a _curry form and these can be joined together to create reusable specs.

##here I'll explain:

assert that bar equals foo

var a = require('assertions')

a.equal(bar, foo)

create an assertion that anything equals foo

var equalFoo = a._equal(foo)

//apply it to some things...
equalFoo (bar)
equalFoo (baz)
equalFoo (zuk)

every function has a curry form with a leading "_",
it skips the first arg,
and returns an assertion function that you can pass the first arg to later!

okay, so why?

this starts to get super awesome when you have, example, the has assertion:

//assert that bar has a property letters: 3, and a name, which is a string.
a.has(bar, {
  letters: 3,
, name: function (actual) { a.isString(actual, 'make it a string!') }
})

has is kinda like deepEqual but only checks that the property matches if it's a primitive.
if the property is a function then has assumes that it is an assertion function,
and applys the function to the corisponding property on the actual object.

lets rewrite the above example using the curry form:

//assert that bar has a property letters: 3, and a name, which is a string.
var validTLA = a._has({
  letters: 3,
  name: a._isString('make it a string!')
}, 'must be a real Three Letter Acronym')

now we can check that every thing is a valid TLA, oh yeah, lets use the higher order assertion every

a.every([
  {name: 'WTF', letters: 3},
  {name: 'OMG', letters: 3},
  {name: 'BBQ', letters: 3},
  {name: 'TLA', letters: 3},
  {name: 'DSL', letters: 3}
], validTLA)

we can now use validTLA where ever we need to check that something is a TLA, not just in our tests.

Error Messages

one of the best things about assertions is that it creates very detailed error messages.

a.every([
  {name: 'WTF', letters: 3},
  {name: 'TLA', letters: 3},
  {name: 'IMHO', letters: 4}
  ], validTLA)

will give you a message like this, showing each step of where it went wrong!

equal: 4 == 3
has: ({ name: "IMHO", letters: 4 }).letters must match { letters: 3, name: isString }).letters
every: every[2] (== { name: "IMHO", letters: 4 }) must pass has, 
  (2 out of 3 have passed)
    at Object.equal (/home/dominic/source/dev/assertions/elementary.js:11:18)
    at Object.leaf (/home/dominic/source/dev/assertions/higher.js:175:16)
    ...

assertion(actual [, expected...], message)

that is the raw form of all assertions. this convention is borrowed from the nodejs assert module.

some times expected is not necessary, or is optional, or may take multiple args. see assertion docs.

if the assertion takes optional args, the last arg is always message if it is a string.

License

MIT