MediaWiki:NumerAlpha.js: mudanças entre as edições

De Wikincat
Ir para navegação Ir para pesquisar
(Criou página com '// from: https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript // To be used in Template:CardCatalog and other places as well functi...')
 
Sem resumo de edição
Linha 2: Linha 2:
// To be used in Template:CardCatalog and other places as well
// To be used in Template:CardCatalog and other places as well


function romanize(num) {
function mainFunc() {
if (isNaN(num)) return NaN;
function romanize(num) {
if (isNaN(num)) return NaN;
const digits = String(+num).split(""),
const digits = String(+num).split(""),
key = [
"",
key = [
"C",
"",
"CC",
"C",
"CCC",
"CC",
"CD",
"CCC",
"D",
"CD",
"DC",
"D",
"DCC",
"DC",
"DCCC",
"DCC",
"CM",
"DCCC",
"",
"CM",
"X",
"",
"XX",
"X",
"XXX",
"XX",
"XL",
"XXX",
"L",
"XL",
"LX",
"L",
"LXX",
"LX",
"LXXX",
"LXX",
"XC",
"LXXX",
"",
"XC",
"I",
"",
"II",
"I",
"III",
"II",
"IV",
"III",
"V",
"IV",
"VI",
"V",
"VII",
"VI",
"VIII",
"VII",
"IX",
"VIII",
];
"IX",
let roman = "";
];
let i = 3;
let roman = "";
let i = 3;
while (i--) roman = (key[+digits.pop() + i * 10] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
while (i--) roman = (key[+digits.pop() + i * 10] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
}
}


const incrementElemsNumeral = document.querySelectorAll(
const incrementElemsNumeral = document.querySelectorAll(
".increment-numeralpha-numeral"
".increment-numeralpha-numeral"
);
);


for (let [i, element] of incrementElemsNumeral.entries()) {
for (let [i, element] of incrementElemsNumeral.entries()) {
element.innerHTML = `${++i}. ${element.innerHTML}`;
element.innerHTML = `${++i}. ${element.innerHTML}`;
}
}


const incrementElemsRoman = document.querySelectorAll(
const incrementElemsRoman = document.querySelectorAll(
".increment-numeralpha-roman"
".increment-numeralpha-roman"
);
);

for (let [i, element] of incrementElemsRoman.entries()) {
element.innerHTML = `${romanize(++i)}. ${element.innerHTML}`;
}
}


if (document.readyState === "complete" || document.readyState !== "loading") {
for (let [i, element] of incrementElemsRoman.entries()) {
mainFunc();
element.innerHTML = `${romanize(++i)}. ${element.innerHTML}`;
} else {
document.addEventListener("DOMContentLoaded", mainFunc);
}
}

Edição das 11h33min de 1 de fevereiro de 2023

// from: https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
// To be used in Template:CardCatalog and other places as well

function mainFunc() {
  function romanize(num) {
    if (isNaN(num)) return NaN;
    const digits = String(+num).split(""),
      key = [
        "",
        "C",
        "CC",
        "CCC",
        "CD",
        "D",
        "DC",
        "DCC",
        "DCCC",
        "CM",
        "",
        "X",
        "XX",
        "XXX",
        "XL",
        "L",
        "LX",
        "LXX",
        "LXXX",
        "XC",
        "",
        "I",
        "II",
        "III",
        "IV",
        "V",
        "VI",
        "VII",
        "VIII",
        "IX",
      ];
    let roman = "";
    let i = 3;
    while (i--) roman = (key[+digits.pop() + i * 10] || "") + roman;
    return Array(+digits.join("") + 1).join("M") + roman;
  }

  const incrementElemsNumeral = document.querySelectorAll(
    ".increment-numeralpha-numeral"
  );

  for (let [i, element] of incrementElemsNumeral.entries()) {
    element.innerHTML = `${++i}. ${element.innerHTML}`;
  }

  const incrementElemsRoman = document.querySelectorAll(
    ".increment-numeralpha-roman"
  );

  for (let [i, element] of incrementElemsRoman.entries()) {
    element.innerHTML = `${romanize(++i)}. ${element.innerHTML}`;
  }
}

if (document.readyState === "complete" || document.readyState !== "loading") {
  mainFunc();
} else {
  document.addEventListener("DOMContentLoaded", mainFunc);
}