58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
export function numberToWords(num) {
|
|
if (num === 0)
|
|
return 'zero';
|
|
const belowTwenty = [
|
|
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
|
|
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
|
|
];
|
|
const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
|
|
const thousands = ['', 'thousand', 'million', 'billion'];
|
|
function helper(n) {
|
|
if (n === 0)
|
|
return '';
|
|
else if (n < 20)
|
|
return belowTwenty[n] + ' ';
|
|
else if (n < 100)
|
|
return tens[Math.floor(n / 10)] + ' ' + helper(n % 10);
|
|
return belowTwenty[Math.floor(n / 100)] + ' hundred ' + helper(n % 100);
|
|
}
|
|
let word = '';
|
|
let i = 0;
|
|
while (num > 0) {
|
|
if (num % 1000 !== 0) {
|
|
word = helper(num % 1000) + thousands[i] + ' ' + word;
|
|
}
|
|
num = Math.floor(num / 1000);
|
|
i++;
|
|
}
|
|
return word.trim();
|
|
}
|
|
export function toPascalCase(strings) {
|
|
if (!Array.isArray(strings))
|
|
return '';
|
|
function capitalize(word) {
|
|
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
}
|
|
return strings.map(capitalize).join('');
|
|
}
|
|
export function convertNumberToPascalCase(num) {
|
|
return toPascalCase(numberToWords(num).split(' '));
|
|
}
|
|
export function injectCss(href) {
|
|
if (document.querySelector(`link[href^='${href}']`)) {
|
|
return Promise.resolve();
|
|
}
|
|
return new Promise((resolve) => {
|
|
const link = document.createElement('link');
|
|
link.setAttribute('rel', 'stylesheet');
|
|
link.setAttribute('type', 'text/css');
|
|
const timeout = setTimeout(resolve, 1000);
|
|
link.addEventListener('load', (_e) => {
|
|
clearInterval(timeout);
|
|
resolve();
|
|
});
|
|
link.href = href;
|
|
document.head.appendChild(link);
|
|
});
|
|
}
|