Is there a JavaScript module that makes it easy to map arguments to parameters? Here is how I envision it working:
var arguments = assignArguments(arguments, 'p1', 'p2', [{'p3': 0}, 'p4'], {'p5': 'unknown'});
Within a function, you would call this to generate an object that associated a parameter with an argument. Parameters defined within an array or inline object would be considered optional, where inline objects would permit assigning default values. All other parameters are considered "required".
Here are some sample inputs/outpus:
foo(1): { p1: 1, p3: 0, p5: 'unknown' } // no p2 (aka undefined)
foo(1, 2): { p1: 1, p2: 2, p3: 0, p5: 'unknown' }
foo(1, 2, 3): { p1: 1, p2: 2, p3: 0, p4: 3, p5: 'unknown' }
foo(1, 2, 3, 4): { p1: 1, p2: 2, p3: 3, p4: 4, p5: 'unknown' }
foo(1, 2, 3, 4, 5): { p1: 1, p2: 2, p3: 3, p4: 4, p5: 5 }
I am hoping a library like this already exists. This logic gets repeated a lot and I want to eliminate it if possible.
Is anyone aware of a library like this? If not, can someone send me down the right path for implementing one?