Hi, I’d like to ask you how to optimize the below code (both regex and function). It should check whether “img.file”, “img[x2]” and “img[x3]” exist and then return appropriate srcset. PS the script is written for Handlebars but it’s almost pure js. Regex should return only last part of the digits (example: 1.biwbvib55-3124-444.jpg => 444)
injectSrcSet: (img) => {
// const regex = /\b(?:\d+)/g;
const regex = /\b[^-](?:\d+)\b/g;
let small, med, big, result;
img.file ? small = `/images/${img.file}` + " " + `${img.file.match(regex)}` + "w" : null;
img["2x"] ? med = `/images/${img["2x"]}` + " " + `${img["2x"].match(regex)}` + "w" : null;
img["3x"] ? big = `/images/${img["3x"]}` + " " + `${img["3x"].match(regex)}` + "w" : null;
if (small) {
result = small
} if (med) {
result = result + ", " + med
} if (big) {
result = result + ", " + big
}
return `scrset="${result}"`
},