2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported parse */
|
2009-11-03 16:05:54 -05:00
|
|
|
|
2023-07-30 08:56:59 -04:00
|
|
|
/**
|
|
|
|
* parse:
|
|
|
|
*
|
|
|
|
* @param {*} params caller-provided parameter object, or %null
|
|
|
|
* @param {*} defaults provided defaults object
|
|
|
|
* @param {boolean} [allowExtras] whether or not to allow properties not in `default`
|
|
|
|
*
|
|
|
|
* @summary Examines `params` and fills in default values from `defaults` for
|
|
|
|
* any properties in `default` 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`.
|
|
|
|
*
|
|
|
|
* If `params` is %null, this returns the values from `defaults`.
|
|
|
|
*
|
|
|
|
* @returns a new object, containing the merged parameters from
|
|
|
|
* `params` and `defaults`
|
|
|
|
*/
|
2019-07-01 23:24:29 -04:00
|
|
|
function parse(params = {}, defaults, allowExtras) {
|
|
|
|
if (!allowExtras) {
|
2019-08-19 20:51:42 -04:00
|
|
|
for (let prop in params) {
|
2019-07-01 23:24:29 -04:00
|
|
|
if (!(prop in defaults))
|
|
|
|
throw new Error(`Unrecognized parameter "${prop}"`);
|
2019-08-19 20:51:42 -04:00
|
|
|
}
|
2009-11-03 16:05:54 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 17:36:30 -04:00
|
|
|
let defaultsCopy = Object.assign({}, defaults);
|
2019-07-05 12:10:56 -04:00
|
|
|
return Object.assign(defaultsCopy, params);
|
2019-07-01 23:24:29 -04:00
|
|
|
}
|