Keyword Arguments

Javascript's handling of input arguments is totally primitive. The best they've managed so far is default parameters, as long as you work from left to right and don't skip anything. In javascript you can call function(arg1, arg2) or function(arg1, arg2, arg3) but if you want to call function(arg1, arg3) you're screwed. The best you can do is function({arg1, arg3}), which requires you to write your code avoiding positional arguments entirely. And if you want to make some of those properties required in Typescript, you will want to kill yourself.

Typescript attempts to bring in method overloading, but similar to its implementation of required input object properties, the implementation is something out of nightmares. Arguably, it is even worse. It's understandable; there's only so much you can do when the underlying language is vehemently opposed to a feature.

This is a problem languages like Python solved with what they call "keyword" arguments. In Python, you can pass the first and third arguments like this: function(arg1, thirdInput = arg3). "thirdInput" is a "keyword." C# then stole that feature (C# always had method overloading, but I don't have a way to simulate that in JS ... maybe I'll think of something).

Link to the demo script
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 ({something} = Kwarg.parseArgs({something}). For "something" on the left side, copy and paste your function arguments. For "something" on the right side, paste the same thing, but delete any default values.

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.