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

De Wikincat
Ir para navegação Ir para pesquisar
Sem resumo de edição
Sem resumo de edição
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 1: Linha 1:
// from: https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
// from: https://stackoverflow.com/questions/9083037
// To be used in Template:CardCatalog and other places as well
// To be used in Template:CardCatalog and other places as well

/* jshint esversion: 10 */


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


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


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


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

Edição atual tal como às 13h34min de 19 de agosto de 2024

// from: https://stackoverflow.com/questions/9083037
// To be used in Template:CardCatalog and other places as well

/* jshint esversion: 10 */

function mainFunc() {
  function romanize(num) {
    if (Number.isNaN(num)) return NaN;
    const digits = String(+num).split('');
    const 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 += 1)}. ${element.innerHTML}`;
  }

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

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

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