params: Simplify code

Standard javascript now has Object.assign() which is very similar to
Params.parse(), except that the latter by default disallows "extra"
parameters. We can still leverage the standard API by simply
implementing the error check, and then call out to Object.assign()
for the actual parameter merging.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/612
This commit is contained in:
Florian Müllner 2019-07-02 05:24:29 +02:00 committed by Florian Müllner
parent e95f3febd6
commit 46874eed05

View File

@ -14,22 +14,12 @@
// //
// Return value: a new object, containing the merged parameters from // Return value: a new object, containing the merged parameters from
// @params and @defaults // @params and @defaults
function parse(params, defaults, allowExtras) { function parse(params = {}, defaults, allowExtras) {
let ret = {}, prop; if (!allowExtras) {
for (let prop in params)
if (!params) if (!(prop in defaults))
params = {}; throw new Error(`Unrecognized parameter "${prop}"`);
for (prop in params) {
if (!(prop in defaults) && !allowExtras)
throw new Error('Unrecognized parameter "' + prop + '"');
ret[prop] = params[prop];
} }
for (prop in defaults) { return Object.assign(defaults, params);
if (!(prop in params)) }
ret[prop] = defaults[prop];
}
return ret;
}