2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-11-03 21:05:54 +00:00
|
|
|
|
2019-07-05 16:10:56 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
|
2009-11-03 21:05:54 +00:00
|
|
|
// parse:
|
|
|
|
// @params: caller-provided parameter object, or %null
|
2017-10-31 00:03:21 +00:00
|
|
|
// @defaults-provided defaults object
|
2009-11-03 21:05:54 +00: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 15:14:23 +00:00
|
|
|
// If @params is %null, this returns the values from @defaults.
|
2009-11-03 21:05:54 +00:00
|
|
|
//
|
2010-04-29 15:14:23 +00:00
|
|
|
// Return value: a new object, containing the merged parameters from
|
|
|
|
// @params and @defaults
|
2019-07-02 03:24:29 +00:00
|
|
|
function parse(params = {}, defaults, allowExtras) {
|
|
|
|
if (!allowExtras) {
|
|
|
|
for (let prop in params)
|
|
|
|
if (!(prop in defaults))
|
|
|
|
throw new Error(`Unrecognized parameter "${prop}"`);
|
2009-11-03 21:05:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 16:10:56 +00:00
|
|
|
let defaultsCopy = {};
|
|
|
|
Lang.copyProperties(defaults, defaultsCopy);
|
|
|
|
return Object.assign(defaultsCopy, params);
|
2019-07-02 03:24:29 +00:00
|
|
|
}
|