More convenience with condition chaining

This commit is contained in:
Andreas Hocevar
2020-06-10 13:05:19 +02:00
parent aa5de5db60
commit 3de2ea0624
5 changed files with 105 additions and 35 deletions
+23
View File
@@ -13,6 +13,29 @@ import {assert} from '../asserts.js';
* @typedef {function(this: ?, import("../MapBrowserEvent.js").default): boolean} Condition
*/
/**
* Creates a condition function that is only fulfilled when a chain of conditions pass.
* @param {...Condition} var_args Conditions to check.
* @return {Condition} Condition function that checks a chain of conditions.
*/
export function chain(var_args) {
const conditions = arguments;
/**
* @param {import("../MapBrowserEvent.js").default} event Event.
* @return {boolean} All conditions passed.
*/
return function (event) {
let pass = true;
for (let i = 0, ii = conditions.length; i < ii; ++i) {
pass = pass && conditions[i](event);
if (!pass) {
break;
}
}
return pass;
};
}
/**
* Return `true` if only the alt-key is pressed, `false` otherwise (e.g. when
* additionally the shift-key is pressed).