Skip to content

Commit d804987

Browse files
committed
Refctor: Updates to formatNumber to match the rest of the scripts
1 parent 0b731d4 commit d804987

1 file changed

Lines changed: 71 additions & 61 deletions

File tree

Dist/Functional/FormatNumbers.js

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,85 @@
1-
'use strict'
1+
'use strict';
22

3-
let FormatNo = (element, locales, number, options) => {
4-
let final = Intl.NumberFormat(locales, options).format(number);
5-
if (!final) return;
6-
element.innerHTML = final;
7-
}
3+
class NumberFormatter {
4+
constructor(_element) {
5+
try {
6+
this.element = _element;
7+
this.locales = _element.getAttribute("wt-formatnumber-locales");
8+
this.style = _element.getAttribute("wt-formatnumber-style");
9+
this.currency = _element.getAttribute("wt-formatnumber-currency");
10+
this.unit = _element.getAttribute("wt-formatnumber-unit");
11+
this.value = _element.textContent;
812

9-
let GetFormatStyle = (_style, _currency, _unit) => {
10-
let _options = null;
13+
if (!this.value) throw new Error("Number value is missing in the element.");
1114

12-
if (_style) {
13-
switch (_style) {
14-
case 'currency':
15-
if (!_currency) return;
16-
_options = {
17-
style: _style,
18-
currency: _currency
19-
}
20-
break;
21-
case 'decimal':
22-
_options = {
23-
style: _style
24-
};
25-
break;
26-
case 'percent':
27-
_options = {
28-
style: _style
29-
};
30-
break;
31-
case 'unit':
32-
_options = {
33-
style: _style,
34-
unit: _unit
35-
};
36-
break;
37-
default:
38-
return null;
15+
this.options = this.getFormatStyle(this.style, this.currency, this.unit);
16+
if (!this.options) throw new Error("Invalid format options.");
17+
18+
this.formatNumber();
19+
} catch (err) {
20+
console.error(`Error initializing NumberFormatter: ${err.message}`);
3921
}
40-
return _options
4122
}
42-
}
43-
44-
const InitializeFormatNumbers = () => {
45-
46-
let numbersToFormat = document.querySelectorAll('[wt-formatnumber-element="number"]');
47-
48-
if(!numbersToFormat || numbersToFormat.length === 0) return;
49-
50-
for(let numberContainer of numbersToFormat) {
51-
let locales = numberContainer.getAttribute("wt-formatnumber-locales");
52-
let style = numberContainer.getAttribute("wt-formatnumber-style");
53-
let currency = numberContainer.getAttribute("wt-formatnumber-currency");
54-
let unit = numberContainer.getAttribute("wt-formatnumber-unit");
55-
let options = GetFormatStyle(style, currency, unit);
56-
57-
if(!options) return;
5823

59-
let value = numberContainer.textContent;
24+
getFormatStyle(_style, _currency, _unit) {
25+
let _options = null;
26+
if (_style) {
27+
switch (_style) {
28+
case 'currency':
29+
if (!_currency) return null;
30+
_options = {
31+
style: _style,
32+
currency: _currency
33+
};
34+
break;
35+
case 'decimal':
36+
case 'percent':
37+
_options = { style: _style };
38+
break;
39+
case 'unit':
40+
if (!_unit) return null;
41+
_options = {
42+
style: _style,
43+
unit: _unit
44+
};
45+
break;
46+
default:
47+
return null;
48+
}
49+
return _options;
50+
}
51+
}
6052

61-
if (!value) return;
62-
53+
formatNumber() {
6354
try {
64-
FormatNo(numberContainer, locales, value, options);
65-
} catch (error) {
66-
console.error(`there was an error processing the format, ${error}`);
55+
const number = parseFloat(this.value.replace(/,/g, ''));
56+
if (isNaN(number)) throw new Error("Invalid number value.");
57+
58+
const formattedNumber = new Intl.NumberFormat(this.locales, this.options).format(number);
59+
if (!formattedNumber) throw new Error("Formatting failed.");
60+
61+
this.element.innerHTML = formattedNumber;
62+
} catch (err) {
63+
console.error(`Error formatting number: ${err.message}`);
6764
}
6865
}
6966
}
7067

68+
const InitializeFormatNumbers = () => {
69+
try {
70+
const numbersToFormat = document.querySelectorAll('[wt-formatnumber-element="number"]');
71+
if (!numbersToFormat || numbersToFormat.length === 0) throw new Error("No elements found to format.");
72+
73+
numbersToFormat.forEach((numberContainer) => {
74+
new NumberFormatter(numberContainer);
75+
});
76+
} catch (err) {
77+
console.error(`Format Number found an Error while initializing: ${err.message}`);
78+
}
79+
};
80+
7181
if (/complete|interactive|loaded/.test(document.readyState)) {
7282
InitializeFormatNumbers();
73-
} else {
74-
window.addEventListener('DOMContentLoaded', InitializeFormatNumbers)
75-
}
83+
} else {
84+
window.addEventListener('DOMContentLoaded', InitializeFormatNumbers);
85+
}

0 commit comments

Comments
 (0)