const greet = function () {
const params = ducky.params("foo", arguments, {
hello: { def: "Hello", valid: "string" },
what: { pos: 0, def: "World", valid: "string" },
more: { pos: "...", def: [], valid: "[ number* ]" },
});
let result = params.hello + " " + params.what;
params.more.forEach(function (num) {
result += ", " + num;
});
return result;
};
greet() // → "Hello World"
greet("Hackers") // → "Hello Hackers"
greet({ what: "Hackers" }) // → "Hello Hackers"
greet("Hackers", 1, 2, 3) // → "Hello Hackers, 1, 2, 3"
greet({ what: "Hackers", more: [ 1, 2, 3 ] })
// → "Hello Hackers, 1, 2, 3"
greet(42) // → throw Error
greet("Foo", "Bar") // → throw Error
Let's define a function which should greet.
With the help of Ducky its parameter handling
is both concise and strict.
The parameter handling provided by Ducky support
positional and named parameters at the same time.
Additionally, it supports optional parameters (those
with default values and not in the middle of a list
of positional parameters), a powerful value
validation based on
ducky.validate()
specifications
and even the special scenario of rest parameters.