I have the following block of code:
data.split('\n').map(l => {
if (/^videoW/.exec(l)) {
vidConf.videoW = l.split('=')[1]
}
if (/^videoH/.exec(l)) {
vidConf.videoH = l.split('=')[1]
}
if (/^videoT/.exec(l)) {
vidConf.videoT = l.split('=')[1]
}
if (/^videoL/.exec(l)) {
vidConf.videoL = l.split('=')[1]
}
if (/^time=/.exec(l)) {
vidConf.timeImg = l.split('=')[1]
}
if (/^auto=/.exec(l)) {
vidConf.videoA = l.split('=')[1]
}
if (/^typeP=/.exec(l)) {
vidConf.typeP = l.split('=')[1]
}
})
I cut the data array by line breaks ( \n
), because I extract it from an external file and perform an map
envelope to check the existence of some variables in it through regular expressions and in this way pass the data to the vidConf variable .
How could I refactor this code avoiding so many statements if
, and achieving the same result?
You could create an array with the list of "cases" (
eg: ifs
) and then for each line evaluate them one by one.We can use
some
to avoid control all cases.Example: