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:
parent
e95f3febd6
commit
46874eed05
@ -14,22 +14,12 @@
|
||||
//
|
||||
// Return value: a new object, containing the merged parameters from
|
||||
// @params and @defaults
|
||||
function parse(params, defaults, allowExtras) {
|
||||
let ret = {}, prop;
|
||||
|
||||
if (!params)
|
||||
params = {};
|
||||
|
||||
for (prop in params) {
|
||||
if (!(prop in defaults) && !allowExtras)
|
||||
throw new Error('Unrecognized parameter "' + prop + '"');
|
||||
ret[prop] = params[prop];
|
||||
function parse(params = {}, defaults, allowExtras) {
|
||||
if (!allowExtras) {
|
||||
for (let prop in params)
|
||||
if (!(prop in defaults))
|
||||
throw new Error(`Unrecognized parameter "${prop}"`);
|
||||
}
|
||||
|
||||
for (prop in defaults) {
|
||||
if (!(prop in params))
|
||||
ret[prop] = defaults[prop];
|
||||
}
|
||||
|
||||
return ret;
|
||||
return Object.assign(defaults, params);
|
||||
}
|
Loading…
Reference in New Issue
Block a user