Skip to content

Instantly share code, notes, and snippets.

@elpddev
Created June 24, 2020 06:04
Show Gist options
  • Save elpddev/905bf06b3a28c5b597d2f0b74c94ef22 to your computer and use it in GitHub Desktop.
Save elpddev/905bf06b3a28c5b597d2f0b74c94ef22 to your computer and use it in GitHub Desktop.
const actions = ()=>{
const functionA = ()=>{/*do sth*/}
const functionB = ()=>{/*do sth*/}
const functionC = ()=>{/*send log*/}
return new Map([
[/^guest_[1-4]$/,functionA],
[/^guest_5$/,functionB],
[/^guest_.*$/,functionC],
//...
])
}
const onButtonClick = (identity, status)=>{
patternOnEach(actions(), `${identity}_${status}`);
}
const patternOnEach = (allActions, pattern) => {
const actions = [...allActions].filter(([key, value]) => (key.test(pattern)));
actions.forEach(([key,value]) => value.call(this))
}
const patternOnFirst = (allActions, pattern) => {
const action = [...allActions].find(([key, value])=>(key.test(pattern)));
if (!action) { return; }
action[1].call(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment