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

duck

v0.1.12

Published

Rich matchers inspired by Hamcrest. Useful for generating helpful assertion failure messages in tests.

Downloads

1,483,327

Readme

duck.js -- rich matchers with helpful messages on match failure

duck.js allows you to perform assertions on complex objects. When those assertions fail, duck.js will try to produce helpful error messages. For instance, suppose you want to assert the same property on an array of objects:

var duck = require("duck");
var isArray = duck.isArray;
var hasProperties = duck.hasProperties;

var users = fetchUsers();
duck.assertThat(users, isArray([
    hasProperties({name: "Bob"}),
    hasProperties({name: "Jim"}),
]));

which might produce an error message like:

Expected [object with properties {
    name: 'Bob'
}, object with properties {
    name: 'Jim'
}]
but element at index 0 didn't match:
    value of property "name" didn't match:
        was 'Jim'
        expected 'Bob'
    expected object with properties {
        name: 'Bob'
    }
element at index 1 didn't match:
    value of property "name" didn't match:
        was 'Bob'
        expected 'Jim'
    expected object with properties {
        name: 'Jim'
    }

API

The below is a quick reference to the API. For more examples, take a look at the tests.

duck.assertThat(value, matcher)

Assert that value satifies matcher.

If value satifies matcher, return normally, otherwise throw an AssertionError describing the mismatch.

duck.is(value)

If value is a matcher, return that matcher, otherwise return duck.equalTo(value).

duck.equalTo(value)

Matcher for deep equality on value.

duck.isObject(matcherObj)

An object obj matches duck.isObject(matcherObj) if:

  • obj matches duck.hasProperties(matcherObj), and
  • there is no key that is present in obj but not in matcherObj

Sample usage:

duck.isObject({
    name: "Bob",
    address: duck.isObject({
        city: "Cambridge",
        county: "UK"
    })
})

duck.is is called on each value of the matcher object, meaning that the above is equivalent to:

duck.isObject({
    name: duck.is("Bob"),
    address: duck.isObject({
        city: duck.is("Cambridge"),
        county: duck.is("UK")
    })
})

duck.hasProperties(matcherProperties)

An object obj matches duck.hasProperties(matcherProperties) if, for each key in matcherProperties, matcherProperties[key].matches(obj[key])

Sample usage:

duck.hasProperties({
    name: "Bob",
    address: duck.hasProperties({
        city: "Cambridge",
        county: "UK"
    })
})

duck.is is called on each value of the matcher object, meaning that the above is equivalent to:

duck.hasProperties({
    name: duck.is("Bob"),
    address: duck.hasProperties({
        city: duck.is("Cambridge"),
        county: duck.is("UK")
    })
})

duck.isArray(matcherArray)

An array blah matches duck.isArray(matcherArray) if:

  • blah.length == matcherArray.length, and
  • For 0 <= i < array.length, matcherArray[i].matches(blah[i])

Sample usage:

duck.isArray([
    duck.hasProperties({name: "Bob"}),
    duck.hasProperties({name: "Jim"}),
]))

duck.is is called on each element of the matcher array, meaning that the following are equivalent:

duck.isArray(["apple", "banana"])

duck.isArray([duck.is("apple"), duck.is("banana")])

Matcher

Each matcher has the following methods:

matcher.matches(value)

Return true if value satifies this matcher, false otherwise.

matcher.describeMismatch(value)

Generate a string describing why value doesn't satisfy this matcher. Behaviour is undefined if value actually satisifies the matcher.

matcher.matchesWithDescription(value)

Equivalent to:

var isMatch = this.matches(value);
return {
    matches: isMatch,
    description: isMatch ? "" : this.describeMismatch(value)
};

Useful if you're likely to want both the boolean and the mismatch description.

matcher.describeSelf()

Generate a string describing the matcher.

Thanks

Thanks to Hamcrest for inspiration.