More convenience with condition chaining
This commit is contained in:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user