@slimio/arg-checker
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

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

Package Sidebar

Install

npm i @slimio/arg-checker

Weekly Downloads

0

Version

0.2.1

License

MIT

Unpacked Size

8.54 kB

Total Files

6

Last publish

Collaborators

  • fraxken
  • alexandre.malaj