InferJS - Live Example

Below is an example of the InferJS-Library, running on the client side, through the browser. An InferObject file, with pre-compiled JSDoc comments, is specified for runtime type checking and inferred expectations.

Source File: test1.js

import { InferFile } from "/js/infers/demo.infer.js";
import { InferJS } from "/js/inferjs-0.0.2.min.js";
const inferjs = new InferJS(InferFile);

/**
 * Test case scenario for JavaScript inferjs function.
 * @category tests
 * @function foo
 * @param {string} msg - The message to send through console.log().
 * @param {(number|string)} id - The id of the message.
 * @param {boolean} send - Whether to send your message.
 * @infer {string} msg {STRING-NOT-EMPTY} - Checks if string is not empty.
 * @infer {(number|string)} id {INT8} - Check if number.
 * @inferid foo
 * @return {(InferTypeError|InferExpectError|string)} - Returns an error or the msg.
 */
export function foo(msg, id, send) {

    // InferJS type check.
    const e = inferjs.check('foo', arguments, true);

    // Return error or message.
    if (e) {
        return e;
    } else {
        return msg;
    }
}

// Example Normal Call
console.log(foo('test', '-1234', true));

Type in the method parameters with anything you want calling foo(). Hit the run button to examine the Infer Exceptions. If you pass the correct expected method parameters, then the msg will be displayed. Notice the exceptions, contain plenty of useful debugging information.

Results:

Hit the RUN button
PACK VERSION: LIC: MIT
COPYRIGHT © 2022-2024 INFERJS