npa

named-positional-args

support API args as both named and positional

Showing:

Popularity

Downloads/wk

8

GitHub Stars

2

Maintenance

Last Commit

1yr ago

Contributors

1

Package

Dependencies

1

Size (min+gzip)

1.6KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Travis

Run on Repl.it

named-positional-args

API support for arguments as both named and positional.

Depends on named-parameters lib for implementing the very helpful default, coerce, and require features to manage API arguments data types and values.

Install

npm install --save named-positional-args

Usage

Call your function API either of two ways, example:
makeIntoGold({c:3, a:1});      //1. named

makeIntoGold(1, undefined, 3); //2. positional
Implementation, example:
var namedPositionalArgs = require('named-positional-args');

function makeIntoGold(a, b, c) {
  arguments = namedPositionalArgs.apply(makeIntoGold, arguments).args();
  a = arguments[0]; b = arguments[1]; c = arguments[2];
 
  //rest of code...
}
Even better/simpler with ES6 destructuring:
function makeIntoGold(a, b, c) {
  [a, b, c] = 
    namedPositionalArgs.apply(makeIntoGold, arguments).args();

  //rest of code...
}
function makeIntoGold(a, b, c) {
  [a, b, c] = 
    namedPositionalArgs
    .apply(makeIntoGold, arguments)
    .default('a', 999)
    .coerce('b', 'boolean')
    .require('c', 'positive integer')
    .args();

  //rest of code...
}

Note: This is obviously silly to use for functions which only take a single {} object param anyways! ;)

Warning: If you compress/mangle your code, this way might break it!! (since the function arg names might no longer align internally as expected). To avoid this limitation, you can instead use a csvArgs string:

function makeIntoGold(a, b, c) { // these can get mangled now!
  [a, b, c] =
    namedPositionalArgs
    .apply('a, b, c', arguments) // these are the cannonical arg names
    .args();

  //rest of code...
}

API

  • .apply(funcName:Function, arguments) : Starts the argument parsing chain.

-or-

  • .apply(csvArgs:String, arguments) : Starts the argument parsing chain.

.default() : see default

.coerce() : see coerce

.require() : see require (alias: .demand())

.args() : Returns an Array akin to arguments.

.opts() : Returns an Object with arguments as name: value pairs.

Test

npm test

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100