Skip to content

Instantly share code, notes, and snippets.

@qnkhuat
Last active May 23, 2023 09:43
Show Gist options
  • Save qnkhuat/a87119475a132b1e4b2d68aa08195ded to your computer and use it in GitHub Desktop.
Save qnkhuat/a87119475a132b1e4b2d68aa08195ded to your computer and use it in GitHub Desktop.
Metabase migrate from 18 to 24 logic
const mapping = [
{
start: 0,
end: 0,
},
{
start: 1,
end: 2,
},
{
start: 3,
end: 3,
},
{
start: 4,
end: 4,
},
{
start: 5,
end: 6,
},
{
start: 7,
end: 7,
},
{
start: 8,
end: 8,
},
{
start: 9,
end: 10,
},
{
start: 11,
end: 11,
},
{
start: 12,
end: 12,
},
{
start: 13,
end: 14,
},
{
start: 15,
end: 15,
},
{
start: 16,
end: 16,
},
{
start: 17,
end: 18,
},
{
start: 19,
end: 19,
},
{
start: 20,
end: 20,
},
{
start: 21,
end: 22,
},
{
start: 23,
end: 23,
},
];
const migrate18to24 = (startX, width) => {
const endX = startX + width - 1;
const newStartX = mapping[startX].start;
const newEndX = mapping[endX].end;
const newWidth = newEndX - newStartX + 1;
return [newStartX, newWidth];
}
const range = (start, end) => {
return Array.apply(0, Array(end - 1))
.map((_element, index) => index + start)
};
const migrate18to24_ngoc_version = (startX, width) => {
const scale_18_to_24 = (x) => (parseFloat(x) / 18) * 24;
const endX = startX + width;
const newStartX = Math.floor(scale_18_to_24(startX));
const newEndX = Math.ceil(scale_18_to_24(endX));
const newWidth = newEndX - newStartX;
return [newStartX, newWidth];
}
for (let w of range(1, 5)) {
for (let s of range(0, 20 - w)) {
console.log("__________________________________________")
console.log("(s, w): ", s, w)
const original_result = migrate18to24(s, w);
const simplified_result = migrate18to24_ngoc_version(s, w);
const is_matched = (original_result[0] == simplified_result[0] && original_result[1] == simplified_result[1]);
console.log("ORIGINAL:", original_result);
console.log("NGOC: ", simplified_result);
console.log("MATCHED? ", is_matched ? "YESSSSSSSSSSS" : "NOOOOOOOOO");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment