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-js

v1.0.0

Published

Javascript simple assertion library with no dependencies.

Downloads

766

Readme

AssertJS

Javascript, battle tested, simple assertion library with no dependencies.

Status: Build Status

Example:

/**
 * @param {HTMLElement} element
 */
function doSomethingWithHtmlElement(element)
{
    Assert.instanceOf(element, HTMLElement);

    // do your job
}

Now you are covered by the Assertion, and you don't need to be worried that someone might pass empty object {} to doSomethingWithHtmlElement. doSomethingWithHtmlElement function was designed to accept only HTMLElement, nothing more!

Usage

npm install assert-js --save
let Assert = require('assert-js')

Assert.true(true);
Assert.false(false);
Assert.instanceOf(new String("test"), String);
Assert.instanceOneOf(new String("test"), [String, Number]);
Assert.containsOnly([new String("test"), new String("test")],String);
Assert.containsOnlyString(["test", "test1"]);
Assert.containsOnlyInteger([1, 2]);
Assert.containsOnlyNumber([2, 10.25]);
Assert.integer(1);
Assert.number(0.5);
Assert.oddNumber(3);
Assert.evenNumber(4);
Assert.greaterThan(1, 10);
Assert.greaterThanOrEqual(1, 1);
Assert.lessThan(10, 5);
Assert.lessThanOrEqual(1, 1);
Assert.string("string");
Assert.boolean(true);
Assert.equal(1, 1);
Assert.objectEqual({"key":"value"}, {"key":"value"});
Assert.object({id: 1});
Assert.hasFunction("testFunction", {testFunction: () => { alert('test'); } });
Assert.hasProperty("test", {test: 'value'});
Assert.isFunction(() => { alert('test'); });
Assert.array([1, 2, 3, 4, 5]);
Assert.count(0, []);
Assert.notEmpty(0, [1, 2, 3]);
Assert.jsonString('{"key": "value"}');
Assert.email('[email protected]');
Assert.url('https://github.com/Tiliqua/assert-js');
Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');
Assert.hasElement('#div', window.document);
Assert.hasAttribute('data-test', window.document.querySelector('#test'));
Assert.hasAttributes(['data-test', 'id'], window.document.querySelector('#test'));
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Assert.true(true);

Asserts that expression or value is equal to true.

Example:

Assert.true(1 === 2); // this will throw an Error.
let falseValue = false;
Assert.true(falseValue); // this will throw an Error.

Assert.false(false);

Asserts that expression or value is equal to false.

Example:

Assert.false(1 !== 2); // this will throw an Error.
let falseValue = true;
Assert.false(falseValue); // this will throw an Error.

Assert.instanceOf(new String("test"), String);

Asserts that value is an instance of specific class.

Example:

let div = window.document.querySelector('#my-div');
Assert.instanceOf(element, HTMLDivElement);

Assert.instanceOneOf(new String("test"), [String, Number]);

Asserts that value is an instance of at least one specific class.

Example:

let div = window.document.querySelector('#my-div');
Assert.instanceOneOf(element, [HTMLDivElement, HTMLElement]);

Assert.containsOnly([new String("test"), new String("test")],String);

Asserts that array contains only instances of specific class.


Assert.containsOnlyString(["test", "test1"]);

Asserts that array contains only strings.


Assert.containsOnlyInteger([1, 2]);

Asserts that array contains only integers.


Assert.containsOnlyNumber([2, 10.25]);

Asserts that array contains only numbers.


Assert.integer(1);

Asserts that value is valid integer.


Assert.number(0.5);

Asserts that value is valid number (integer, float).


Assert.oddNumber(3);

Asserts that value is odd number.


Assert.evenNumber(4);

Asserts that value is event number.


Assert.greaterThan(1, 10)

Asserts that number is greater than.


Assert.greaterThanOrEqual(1, 1)

Asserts that number is greater than or equal.


Assert.lessThan(10, 5)

Asserts that number is less than.


Assert.lessThanOrEqual(1, 1)

Asserts that number is less than or equal.


Assert.string("string");

Assert that value is valid string.


Assert.boolean(true);

Asserts that value is valid boolean.


Assert.object(1, 1);

Asserts that value is equal to expected value.


Assert.objectEqual({"key":"value"}, {"key":"value"});

Asserts that object is equal to expected object.


Assert.object({id: 1});

Asserts that value is valid object.


Assert.hasFunction("testFunction", {testFunction: () => { alert('test'); }});

Asserts that object has function.


Assert.hasProperty("test", {test: 'value'});

Asserts that object has property (it can also be a function).


Assert.isFunction(() => { alert('test'); });

Asserts that value is valid function.


Assert.array([1, 2, 3, 4, 5]);

Asserts that value is valid array.


Assert.oneOf(4, [1, 2, 3, 4, 5]);

Asserts that value is one of expected values.


Assert.count(0, []);

Asserts that array have specific number of elements.


Assert.notEmpty(0, [1, 2, 3]);

Asserts that array is not empty.


Assert.jsonString('{"key": "value"}');

Asserts that value is valid json string.


Assert.email('[email protected]');

Asserts that string is valid email address.


Assert.url('https://github.com/Tiliqua/assert-js');

Asserts that string is valid url.


Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');

Asserts that string is valid UUID.


Assert.hasElement('#div', window.document);

Asserts that element has other element under selector.

Example:

let dom = new JSDOM(`<body><div id="div"></div></body>`);

Assert.hasElement('#div', dom.window.document);

Assert.hasAttribute('data-test', window.document.querySelector('#div'));

Asserts that element has expected attribute (it might be empty).

Example:

let dom = new JSDOM(`<body><div id="div" data-test></div></body>`);

Assert.hasAttribute('data-test', dom.window.document.querySelector('#div'));

Assert.hasAttributes(['data-test', 'foo'], window.document.querySelector('#div'));

Asserts that element has expected attributes (it might be empty).

Example:

let dom = new JSDOM(`<body><div id="div" data-test></div></body>`);

Assert.hasAttributes(['data-test','id'], dom.window.document.querySelector('#div'));

Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Asserts that function throws expected exception.

Example:

Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
Assert.throws(() => { throw 'expected message'; }, 'expected message');
Assert.throws(() => { throw new Error(); });
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));

Custom exception message

In order to customize error messages (for easier debugging) you can pass error message as a last parameter into all assertions.

Examples:

Assert.uuid('test', 'This value is not valid UUID.');

you can also use variables expected and received in your messages.

Assert.string(1234, 'Expected ${expected} but got ${received}'); // it throws Error("Expected string but got int[1234]")