args-pattern
v1.0.1
Published
A Nodejs lib that arranges the arguments passed to a function based on pattern string
Downloads
3
Maintainers
Readme
args-pattern
A Nodejs lib that arranges the arguments passed to a function based on pattern string
Description
This library intends to help the developers to maintain backwards compatibility in their functions, by making and maintaining optional parameters, and not worrying to write code to assign the correct values to the arguments
Installation
npm install parse-args -S
Typical Example of optional parameters
// params ([name], [email], age, [gender])
function testFn(name="vin", email="[email protected]", age, gender="female"){
if(!email) {
age = name;
name = "vin"
} else if (!age) {
age = email
email = "[email protected]"
}
}
testFn("john", 34)
Example after using this library
var parseArgs = require("args-pattern")
function testFn() {
let [name, email, age, gender] = parseArgs(arguments, "[name] [, email] , age [, gender]", {
args: ['vin', '[email protected]', 34, "female"]
})
// Output name === "john"
// Output email === "[email protected]"
// Output age === 35
// Output gender === "female"
}
testFn("john", 35)
Even though the real world examples can get a little more complicate, parse-args
library
will be there to rescue, so that you can avoid a lot of spaghetti code :thumbsup:
Test
npm install
mocha test.js
License
MIT