farg

NodeJS function arguments arranger.

Usage no npm install needed!

<script type="module">
  import farg from 'https://cdn.skypack.dev/farg';
</script>

README

farg

Function arguments arranger.

Install

$ npm install farg

Usage

// user.js
var farg = require('farg');

function jsFunction(options, callback){
    var arg = farg({
            options: 'object',
            callback: 'function'
        });
    
    // process arg.options
    
    arg.callback();
}

// caller.js
jsFunction({}, function abx(){ ... });
    // arg.options set to {}
    // arg.callback set to abx function

jsFunction({})
    // arg.options set to {}
    // arg.callback set to undefined

jsFunction(1, 'a')
    // arg.options and arg.callback set to undefined

jsFunction(function abx(){ ... });
    // arg.options set to undefined
    // arg.callback set to abx function

Options

Each arguments options can be string type of the expected function argument, or it also can be object.

Known object type: string, number, object, and function.

var arg = farg({
        arg0: 'string',
        arg1: 'number',
        arg2: 'object',
        arg3: 'function'
    });

Or, it can be object:

var arg = farg({
    arg0: {
        format: 'string',
        options: ['a', 'b'], // List of allowed value
        otherwise: 'c'       // Set as this if it's not set.
    },
    arg1: {
        format: 'number',
        min: 0,             // minimun number allowed
        max: 9,             // maximum number allowed
        otherwise: 8        // Set as this if it's not set.
    },
    arg2: {
        format: 'object',   // no option for now
        otherwise: {}       // Set as this if it's not set.
    },
    arg3: {
        format: 'function',     // no option for now
        otherwise: function(){} // Set as this if it's not set.
    }
});