2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-11-03 16:05:54 -05:00
|
|
|
|
|
|
|
// parse:
|
|
|
|
// @params: caller-provided parameter object, or %null
|
2010-04-29 11:14:23 -04:00
|
|
|
// @defaults: function-provided defaults object
|
2009-11-03 16:05:54 -05:00
|
|
|
// @allowExtras: whether or not to allow properties not in @default
|
|
|
|
//
|
|
|
|
// Examines @params and fills in default values from @defaults for
|
|
|
|
// any properties in @defaults that don't appear in @params. If
|
|
|
|
// @allowExtras is not %true, it will throw an error if @params
|
|
|
|
// contains any properties that aren't in @defaults.
|
|
|
|
//
|
2010-04-29 11:14:23 -04:00
|
|
|
// If @params is %null, this returns the values from @defaults.
|
2009-11-03 16:05:54 -05:00
|
|
|
//
|
2010-04-29 11:14:23 -04:00
|
|
|
// Return value: a new object, containing the merged parameters from
|
|
|
|
// @params and @defaults
|
2009-11-03 16:05:54 -05:00
|
|
|
function parse(params, defaults, allowExtras) {
|
2010-04-29 11:14:23 -04:00
|
|
|
let ret = {}, prop;
|
|
|
|
|
2009-11-03 16:05:54 -05:00
|
|
|
if (!params)
|
2010-04-29 11:14:23 -04:00
|
|
|
params = {};
|
2009-11-03 16:05:54 -05:00
|
|
|
|
2010-04-29 11:14:23 -04:00
|
|
|
for (prop in params) {
|
|
|
|
if (!(prop in defaults) && !allowExtras)
|
|
|
|
throw new Error('Unrecognized parameter "' + prop + '"');
|
|
|
|
ret[prop] = params[prop];
|
2009-11-03 16:05:54 -05:00
|
|
|
}
|
|
|
|
|
2010-04-29 11:14:23 -04:00
|
|
|
for (prop in defaults) {
|
2010-02-02 10:31:38 -05:00
|
|
|
if (!(prop in params))
|
2010-04-29 11:14:23 -04:00
|
|
|
ret[prop] = defaults[prop];
|
2009-11-03 16:05:54 -05:00
|
|
|
}
|
|
|
|
|
2010-04-29 11:14:23 -04:00
|
|
|
return ret;
|
2009-11-03 16:05:54 -05:00
|
|
|
}
|