@slimio/arg-checker

SlimIO Argument Checker

Usage no npm install needed!

<script type="module">
  import slimioArgChecker from 'https://cdn.skypack.dev/@slimio/arg-checker';
</script>

README

arg-checker

version Maintenance MIT

SlimIO Argument Checker. This package is inspired by ow to retrieve the callsites but doesn't include any built-in predicates.

Requirements

Why ?

  • No built-in predicates/validation
  • No browser support (only Node.js).

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/arg-checker
# or
$ yarn add @slimio/arg-checker

Usage example

const argc = require("@slimio/arg-checker");
const is = require("@slimio/is");

function sayHello(name, age) {
    argc(name, [is.string, is.symbol]); // <-- Array stand for "OR" condition
    argc(age, is.number);
}

sayHello("fraxken", 24);
// => No error
sayHello(true);
// => ArgumentError: 'name' doesn't match his predicate(s)

API

argc(arg: any, ...predicates: (argc.predicate | argc.predicate[])[]): void

Check if arg match his predicates.

const logValidInput = (input) => {
    argc(input, is.number, (num) => num > 10 && num < 20);
    // ...predicates are executing as && condition.
    console.log(input);
}

logValidInput(15);
// => No Error

logValidInput(50);
// => ArgumentError: 'input' doesn't match his predicate(s)

Roadmap

  • Custom Error message ?
  • Improve predicates detection ?

Dependencies

Name Refactoring Security Risk Usage
callsites ⚠️Major Low Get V8 call stack traces
clean-stack Minor Low Clean the stack trace generated by Errors

License

MIT