Feature | Example | Detail |
---|---|---|
Kwarg.parseArgs(args: {}, allowUnknownKeyword: boolean = false) |
function parrot(voltage, state = 'a stiff', action = 'voom', type = 'Norwegian Blue') {
({voltage, state = 'a stiff', action = 'voom', type = 'Norwegian Blue} = Kwarg.parseArgs({voltage, state, action, type})); }
|
To add support for keyword arguments, you must add a call to Kwarg.parseArgs in your method, with an object on the left side and the right, in parentheses, using the pattern Yes, this is annoying AF. This is something that's not supported by JS, so whatever we do is going to have to require some code change. But at least it's down to three copy/paste operations, which is as minimal as I could get it. |
kw({name: value}) kw('name', value) kw(['name', value]) |
parrot('a thousand', kw(['state', 'pushing up the daisies']))
|
Once support has been added to a method, you can use a keyword argument by using the kw() method, which can take either two arguments, a one-key object, or a two-element array. The kw() method can be inserted anywhere in the argument list, allowing you to mix keyword and positional arguments. It is, however, standard to only include keyword arguments after positional arguments (but there's no way to validate it). |
$$kw$$ |
({$$kw$$} = Kwarg.parseArgs({pos1, pos2}))
|
$$kw$$ is loaded with an object containing any keyword arguments used by the caller that aren't a formal parameter. So for example, if your function took pos1 and pos2 and the caller specified kw['pos3', true], $$kw$$ would be { pos3: true }; |
$rest$ |
({$rest$} = Kwarg.parseArgs({pos1, pos2, ...rest}))
|
$rest$ is an array loaded with any rest arguments. Note that rest arguments must appear last in javascript. |
Kwarg.unpack(args: {}) |
parrot(...Kwarg.unpack({ action: "VOOM", voltage: "four million", state: "bleedin' demised" }));
|
The Kwarg.unpack() method transforms an object into an array of kw objects, which can be passed into a function. |