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

@shedevro/assert

v1.3.0

Published

Assertion typescript library for better, comfortable and safe coding. See readme!

Downloads

342

Readme

GitHub Workflow Status Coveralls github npm (scoped) npm GitHub stars

Assert

Library provides assertion utility for better and safe coding.

Features:

  • Right assertion return types:

    Assert.someFn(value): asserts value is string or Assert.is.someFn(value): value is string etc.

  • Assert functions can be chained with operators (is, all, nullOr, not)

  • throws WebUtilsAssertionError (extends from Error). Provides right stacktrace (especialy helpfull then coding with Angular to see links to .ts files)

Installation

npm i @shedevro/assert --save

Usage

First you should import the library

import { Assert } from '@shedevro/assert';

Custom error messages

Each function can be customized with you own error message.

Error message supports params injecting of assert functions arguments like input or expected values etc.

Most often the order of params matches the function arguments order.

For example:

const someValue = 3;
Assert.greaterThan(someValue, 10, 'This value should be greater than {2}, but got {1}');
// throw error with message 'This value should be greater than 10, but got 3';

Functions

Then you can choice needed function.

Operators

Almost all functions can be chained with different operators.

Moreover, you can combine them each other.

  • is – modifies assert functions to return true or false as a result
  • all – applies an assert function to a provided array
  • nullOr – runs assertion only if input value is defined
  • not – inverts assertion action

For example:

// is
const someNumber = 10;
if (Assert.is.range(someNumber, 10, 15)) {
  ...
}


// all
class User {
  id: number;
  name: string;
}

const someArray = [new User(), new User];
Assert.all.instanceOf(someArray, User);

// ...futher actions


// nullOr
const someBoolean = null; // or undefined
Assert.nullOr.boolean(someBoolean);
// ...futher actions


// not
const someFunction = () => 123;
Assert.not.throws(someFunction, TypeError);


// combinations
Assert.is.nullOr.not.emptyString('some string');
Assert.not.endsWith('str', 'some suffix');
Assert.nullOr.all.greaterThan([10, 11, 12, 13], 5);
// ...and so on

String Assertions

| Method | Description | |--------------------------------------------------------|---------------------------------------------------------------| | string(value, message?: string) | Ensures that value is a string | | emptyString(value, message?: string) | Ensures that value is an empty string | | contains(value, subString: string, message?: string) | Ensures that value contains substring | | startsWith(value, prefix: string, message?: string) | Ensures that value starts with some prefix | | endsWith(value, suffix: string, message?: string) | Ensures that value ends with some suffix | | oneOf(value, values, message?: string | Ensures that value is one of values (string/number` array) |

Number Assertions

| Method | Description | |--------------------------------------------------------------|-------------------------------------------------| | number(value, message?: string) | Ensures that value is a number | | natural(value, message?: string) | Ensures that value is a natural number | | greaterThan(value, limit: number, message?: string) | Ensures that value is greater than limit | | greaterThanOrEqual(value, limit: number, message?: string) | Ensures that value is greater or equal to limit | | lessThan(value, limit: number, message?: string) | Ensures that value is less than limit | | lessThanOrEqual(value, limit: number, message?: string) | Ensures that value is less or equal to limit | | range(value, min: number, max: number, message?: string) | Ensures that value is in range of min and max |

Boolean Assertions

| Method | Description | |------------------------------------|---------------------------------| | boolean(value, message?: string) | Ensures that value is a boolean | | true(value, message?: string) | Ensures that value is true | | false(value, message?: string) | Ensures that value is false |

Object Assertions

| Method | Description | |-----------------------------------|---------------------------------------------------------------| | object(value, message?: string) | Ensures that value is an object ({}, not Array or null) |

Function Assertions

| Method | Description | |-------------------------------------|----------------------------------| | function(value, message?: string) | Ensures that value is a function |

Array Assertions

| Method | Description | |-------------------------------------------------------------------------|-----------------------------------------------------| | array(value, message?: string) | Ensures that value is an array | | arrayLength(value, expectedLength: number, message?: string) | Ensures that array length is equal to number | | arrayMinLength(value, limit: number, message?: string) | Ensures that array length is not less than limit | | arrayMaxLength(value, limit: number, message?: string) | Ensures that array length is not greater than limit | | arrayLengthBetween(value, min: number, max: number, message?: string) | Ensures that array length is inside a min and max |

Instance Assertions

| Method | Description | |-----------------------------------------------------------|-------------------------------------------------| | instanceOf(value, instanceClass, message?: string) | Ensures that value is an instance of some class | | instanceOfAny(value, instanceClasses, message?: string) | Ensures that value is an instance of any class |

RexExp Assertions

| Method | Description | |--------------------------------------------------|--------------------------------------------------| | match(value, regExp: RegExp, message?: string) | Ensures that value is match a regular expression |

Other Assertions

| Method | Description | |----------------------------------------------------------------|----------------------------------------------------------------------| | defined(value, message?: string) | Ensures that value is defined (not null, undefined or NaN) | | equal(value, expect, message?: string) | Ensures that value is equal to expec (value === expect) | | throws(expression: () => any, errorClass?, message?: string) | Ensures that expression throws some error |

Authors

Shedevro

All credits for great idea to @webmozart with his webmozart/assert for PHP!

Contribute

If you have ideas on how to improve this library, you are welcome!

Please read readme of root project directory.

License

The content of this package is licensed under the MIT license.