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

assert-utils

v2.0.3

Published

Assertion tool that deeply compares value to its definition.

Downloads

28

Readme

assert-utils

Assertion tool that deeply compares value to its definition.

Install

$ yarn add assert-utils

Usage

const
	assertUtils = require('assert-utils'),
	{assert} = assertUtils;

function checkIsString(myText) {
	assert.type(myText, 'string');
}

checkIsString('this is string'); // valid, outputs nothing
checkIsString(2); // invalid, throws an error with message 'Assertion error: expected "string" but got "number"'

API

assert.value(value, expectation, strict)

Checks whether the value equals to the expectation. Throws error if not.

value | any

expectation | any

strict | boolean optional
Default is false.

strict | boolean optional Default is false.

assert.value('1' + 0, '10'); // valid
assert.value('1' + 0, 10); // valid
assert.value('1' + 0, 10, true); // invalid
assert.value('1' + 0, 1); // invalid

assert.type(value, definition)

Deeply checks whether the whole value equals to its definition. If the definition is an object, it recursively checks all its properties.

value | any

definition | any
definition basically accepts the same value as returned by built in typeof operator. In addition, there are some shortcuts:

  • '[]' for array
  • '{}' or {} for object

Example

let
	value = {
		number: 200,
		string: 'this is string',
		function: function() {},
		object: {
			stringProperty: 'also string'
		}
	};

assert.type(value, {
	number: 'number',
	string: 'string',
	function: 'function',
	object: {
		stringProperty: 'string'
	}
});

Optional properties

If you want to mark any definition optional, just add a ? before the first letter.

assert.type('this is string', '?string'); // valid
assert.type(undefined, '?string'); // also valid
assert.type(undefined, 'string'); // invalid

In case of optional object definition you can use special property _required: true. Default is true.

// valid
assert.type({
	string: 'this is string'
}, {
	string: 'string'
})

// also valid
assert.type(undefined, {
	_required: false,
	string: 'string'
})

// invalid
assert.type(undefined, {
	string: 'string'
})

Multiple possible types

In case you expect multiple type, just divide them by |.

// valid
assert.type({
	stringOrNumber: 2
}, {
	stringOrNumber: 'string|number'
})

// also valid
assert.type({
	stringOrNumber: 'this is string'
}, {
	stringOrNumber: '?string|number'
})

// invalid - "?" has to be first
assert.type({
	stringOrNumber: 'this is string'
}, {
	stringOrNumber: 'string|?number'
})

Equal properties

By default only the properties included in the definition object are being checked. If the tested object has some additional properties, it just passes them as valid. If you want to check there are no additional properties beside those in the definition, use a special property _strict: true. Default is false:

// valid
assert.type({
	string: 'this is string'
}, {
	string: 'string'
})

// also valid
assert.type({
	string: 'this is string',
	number: 2
}, {
	string: 'string'
})

// invalid
assert.type({
	string: 'this is string',
	number: 2
}, {
	_strict: true,
	string: 'string'
})

Specific value instead of type

In case you want to mix up type and value checks, use a special property _value. Of cource you can use it along with _strict property:

// valid
assert.type(1, {
	_value: 1
})

// valid
assert.type(1, {
	_value: '1'
})

// invalid
assert.type(1, {
	_value: 'number'
})

// invalid
assert.type(1, {
	_value: '1',
	_strict: true
})

Testing

Tests are using AVA library

$ yarn test                              // run all tests
$ yarn test -- -m 'Test title RegExp'    // run test with matching title