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

v0.2.3

Published

An enhanced Node.js assert module

Downloads

4

Readme

IMPORTANT: This module is an improvement from joyent/node-assert-plus module with added assertions registration methods.

Installation:

npm install assert-enhanced

For types definitions:

npm install AshLePoney/node-assert-enhanced-types --save-dev

Example:

const assert = require('assert-enhanced');

function example (stdout, chunk, next) {
  assert.writable(stdout, 'stdout');
  assert.object(chunk, 'chunk');
  assert.string(chunk.id, 'chunk.id');
  assert.buffer(chunk.buffer, 'chunk.buffer');
  assert.func(next, 'next');

  // ...

  stdout.write(chunk);
}

Or to add a new assertion set

const assert = require('assert-enhanced');

const definition = {
  custom: {
    check: (arg) => (arg instanceof Custom),
    operator: 'instanceof',
    actual: (arg) => Object.prototype.toString.call(arg).slice(8, -1)
  }
};

assert.register(null, definitions, { arrayOf: false, optionalArrayOf: false });

// Register to assert-enhanced:
// assert.custom
// assert.optionalCustom
// assert.arrayOfCustom         (generation disabled by options).
// assert.optionalArrayOfCustom (generation disabled by options).

API:

The added assertions methods take two arguments, the value tested first and then the name of the checked parameter.

assert.bool(myBoolArg, 'myBoolArg');
assert.optionalBool(myBoolArg, 'myBoolArg');
assert.arrayOfbool(myBoolArg, 'myBoolArg');
assert.optionalArrayOfBool(myBoolArg, 'myBoolArg');

On bad assertion it will throw an EnhancedAssertionError:

AssertionError [ERR_ASSERTION]: myBoolArg (bool) is required.
    at Object.<anonymous> (/home/user/projects/test-assert-enhanced/test.js:3:8)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Enhanced:

  • assert.EnhancedAssertionError
  • assert.bool
  • assert.number
  • assert.string
  • assert.symbol
  • assert.object
  • assert.func
  • assert.array
  • assert.asyncFunc
  • assert.promise
  • assert.date
  • assert.regexp
  • assert.buffer
  • assert.stream
  • assert.readable
  • assert.writable
  • assert.duplex
  • assert.optionalBool
  • assert.optionalNumber
  • assert.optionalString
  • assert.optionalSymbol
  • assert.optionalObject
  • assert.optionalFunc
  • assert.optionalArray
  • assert.optionalAsyncFunction
  • assert.optionalPromise
  • assert.optionalDate
  • assert.optionalRegexp
  • assert.optionalBuffer
  • assert.optionalStream
  • assert.optionalReadable
  • assert.optionalWritable
  • assert.optionalDuplex
  • assert.arrayOfBool
  • assert.arrayOfNumber
  • assert.arrayOfString
  • assert.arrayOfSymbol
  • assert.arrayOfObject
  • assert.arrayOfFunc
  • assert.arrayOfArray
  • assert.arrayOfAsyncFunc
  • assert.arrayOfPromise
  • assert.arrayOfDate
  • assert.arrayOfRegexp
  • assert.arrayOfBuffer
  • assert.arrayOfStream
  • assert.arrayOfReadable
  • assert.arrayOfWritable
  • assert.arrayOfDuplex
  • assert.optionalArrayOfBool
  • assert.optionalArrayOfNumber
  • assert.optionalArrayOfString
  • assert.optionalArrayOfSymbol
  • assert.optionalArrayOfObject
  • assert.optionalArrayOfFunc
  • assert.optionalArrayOfArray
  • assert.optionalArrayOfAsyncFunc
  • assert.optionalArrayOfPromise
  • assert.optionalArrayOfDate
  • assert.optionalArrayOfRegexp
  • assert.optionalArrayOfBuffer
  • assert.optionalArrayOfStream
  • assert.optionalArrayOfReadable
  • assert.optionalArrayOfWritable
  • assert.optionalArrayOfDuplex

Enhanced registration methods:

Node.js assert module:

See more on Node.js assert module: https://nodejs.org/api/assert.html

assert.register(target, definitions, options)->target

Register assertions sets.

| argument | type | details | |-------------|--------------------|-------------| | target | Object or Function | The target where to save the generated assertion methods. If it's null or undefined then registration is processed on the exported module. | | definitions | Object | The definitions object will wrap the definitions of the different assertions that need to be added. | | options | Object (optional) | The options object will store the states to know if we should cancel the generation of methods by types. Any registration type can be disabled using options ({ [type]: false }). |

Example:

const definitions = {
  custom: {
    check: (arg) => (arg instanceof Custom),
    operator: 'instanceof',
    actual: assert.getClassname
  }
};

assert.register(null, definitions, { optionalArrayOf: false });

// Register [standard, optional, arrayOf]

assert.registerStandardAssertion(target, name, definition)->target

Register standard assertion.

| argument | type | details | |--------------|--------------------|-------------| | target | Object or Function | The target where to save the generated assertion methods. If it's null or undefined then registration is processed on the exported module. | | name | String | The assertion method name. | | definition | Object | The definitions object. |

Example:

assert.registerStandardAssert(null, 'custom', {
  check: (arg) => (arg instanceof Custom),
  operator: 'instanceof',
  actual: assert.getClassname
});

assert.registerOptionalAssertion(target, name, definition)->target

Register optional assertion.

| argument | type | details | |--------------|--------------------|-------------| | target | Object or Function | The target where to save the generated assertion methods. If it's null or undefined then registration is processed on the exported module. | | name | String | The assertion method name. | | definition | Object | The definitions object. |

Example:

assert.registerOptionalAssertion(null, 'custom', {
  check: (arg) => (arg instanceof Custom),
  operator: 'instanceof',
  actual: assert.getClassname
});

assert.registerArrayOfAssertion(target, name, definition)->target

Register arrayOf assertion.

| argument | type | details | |--------------|--------------------|-------------| | target | Object or Function | The target where to save the generated assertion methods. If it's null or undefined then registration is processed on the exported module. | | name | String | The assertion method name. | | definition | Object | The definitions object. |

Example:

assert.registerArrayOfAssertion(null, 'custom', {
  check: (arg) => (arg instanceof Custom),
  operator: 'instanceof',
  actual: assert.getClassname
});

assert.registerOptionalArrayOfAssertion(target, name, definition)->target

Register optional arrayOf assertion.

| argument | type | details | |--------------|--------------------|-------------| | target | Object or Function | The target where to save the generated assertion methods. If it's null or undefined then registration is processed on the exported module. | | name | String | The assertion method name. | | definition | Object | The definitions object. |

Example:

assert.registerOptionalArrayOfAssertion(null, 'custom', {
  check: (arg) => (arg instanceof Custom),
  operator: 'instanceof',
  actual: assert.getClassname
});

assert.getClassname(arg)->string

Get an object classname, it's used to seed the actual property from the definitions.

| argument | type | details | |--------------|----------|-------------| | arg | any | |

assert.getTypeof(arg)->string

Get a typeof, it's used to seed the actual property from the definitions.

| argument | type | details | |--------------|----------|-------------| | arg | any | |