this is my first time on this stack exchange :)
I created a little script to check for the latest version of specified API version available, but I am wondering if I am using the best way possible for checking regex and includes?
The var arr will be filled dynamically with version folders using fs.readdir, but for simplicity I wrote them in the array manually and the var pattern will come from req.params.v using nodejs express router middleware.
Just looking for an approval or if there is a better way, thank you!
var arr = ['v1.0.0', 'v5.2.4', 'v5.2.9', 'v5.20.4', 'v6.4.0'];
var pattern = 'v5.2';
const regex = /^v[0-9](\.[0-9]{1,2})?(\.[0-9])?$/;
if (regex.test(pattern) && (arr.filter(s => s.includes(pattern)) != 0)) {
if (pattern.match(/\./g) == null) {
console.log('major version');
console.log(pattern);
} else if (pattern.match(/\./g).length == 1) {
console.log('minor version');
console.log(pattern);
pattern = pattern + '.';
} else {
console.log('patch version');
console.log(pattern);
}
const matches = arr.filter(s => s.includes(pattern));
console.log(matches[matches.length - 1]);
} else {
console.log('Specify correct API version!');
}
Thank you in advance!