I am using this bit of javascript code to create a string with css variables in it. It helps me to specifically set a size for each breakpoint on a webpage.
This is my code:
const sizes = {
xs: 0.9,
md: 2,
lg: 2.5,
xl: 4
};
function getCssVar(size) {
var varStr = "";
const defaultBPs = ["xs", "sm", "md", "lg", "xl"];
defaultBPs.forEach(bp => {
const bpi = defaultBPs.indexOf(bp);
var nextHigherIndex = defaultBPs.indexOf(Object.keys(size)[getNextHighestIndex(Object.keys(size).map(bp => (defaultBPs.indexOf(bp))), defaultBPs.indexOf(bp))]);
if (getNextHighestIndex(Object.keys(size).map(bp => (defaultBPs.indexOf(bp))), defaultBPs.indexOf(bp)) === Object.keys(size).length) {
nextHigherIndex = defaultBPs.length;
}
if (size[bp]) {
defaultBPs.forEach(bp2 => {
const bp2i = defaultBPs.indexOf(bp2);
const generateVar = " --size-" + bp2 + ":" + size[bp] + ";";
if (bp2i >= bpi && bp2i < nextHigherIndex) {
varStr += generateVar;
}
})
};
});
return varStr;
}
function getNextHighestIndex(arr, value) {
var i = arr.length;
while (arr[--i] > value);
return ++i;
}
console.log(getCssVar(sizes));
The Output is the following:
--size-xs:0.9; --size-sm:0.9; --size-md:2; --size-lg:2.5; --size-xl:4;
This is technically the correct answer and this code already works but I am still wondering if there is any way to improve my code.
I want it to be a shorter and more compact but with the same output.
Any help will be appreciated. Thanks.
I don't know if I get you right, but consider this logic as an option... Check inline comments