Created
June 24, 2020 06:04
-
-
Save elpddev/905bf06b3a28c5b597d2f0b74c94ef22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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