This repository was archived by the owner on Oct 1, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhandlebar-helpers.js
More file actions
153 lines (140 loc) · 4.1 KB
/
Copy pathhandlebar-helpers.js
File metadata and controls
153 lines (140 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const Handlebars = require('handlebars');
function normalizeAndFormat(string) {
return string
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.split(' ')
.map((word, index) => {
if (index > 0 && word) {
return word[0].toUpperCase() + word.slice(1);
}
return word;
})
.join('');
}
function formatCurrency(inputValue) {
try {
if (!inputValue) return '';
const number = parseFloat(inputValue);
return new Intl.NumberFormat('es-CL').format(number);
} catch (error) {
console.error(`ERROR: formatCurrency: ${error}`);
}
}
function formatCurrencyWithSymbol(inputValue) {
try {
const number = parseFloat(inputValue);
return new Intl.NumberFormat('es-CL', {
style: 'currency',
currency: 'CLP',
minimumFractionDigits: 0,
maximumFractionDigits: 2,
}).format(number);
} catch (error) {
console.error(`ERROR: formatCurrencyWithSymbol: ${error}`);
}
}
function omitTitleIfNil(originalTitle, options) {
try {
const context = options.data.root;
const formattedTitle = normalizeAndFormat(originalTitle);
if (!context[`${formattedTitle}`]) return '';
else return originalTitle;
} catch (error) {
console.error(`ERROR: omitTitleIfNil: ${error}`);
}
}
function omitValueIfNil(originalTitle, options) {
try {
const context = options.data.root;
const formattedTitle = normalizeAndFormat(originalTitle);
if (!context[`${formattedTitle}`]) return '';
else return context[`${formattedTitle}`];
} catch (error) {
console.error(`ERROR: omitValueIfNil: ${error}`);
}
}
function addSpaceIfNotNil(originalTitle, options) {
try {
const context = options.data.root;
const formattedTitle = normalizeAndFormat(originalTitle);
if (!context[`${formattedTitle}`]) return '';
else return new Handlebars.SafeString(' ');
} catch (error) {
console.error(`ERROR: addSpaceIfNotNil: ${error}`);
}
}
function dateFormatter(inputDateStr) {
try {
const months = ['ENE', 'FEB', 'MAR', 'ABR', 'MAY', 'JUN', 'JUL', 'AGO', 'SEP', 'OCT', 'NOV', 'DIC'];
const date = new Date(`${inputDateStr}T12:00:00.000Z`);
const day = date.getDate().toString().padStart(2, '0');
const monthIndex = date.getMonth();
const year = date.getFullYear();
return `${day}-${months[monthIndex]}-${year}`;
} catch (error) {
console.error(`ERROR: dateFormatter: ${error}`);
}
}
function subsidioPercentageFormatter(subsidio, options) {
try {
const context = options.data.root;
if (subsidio) {
return `(${context.subsidioPorcentaje}%)`;
} else return '';
} catch (error) {
console.error(`ERROR: subsidioPercentageFormatter: ${error}`);
}
}
function subsidioM3Formatter(subsidio, options) {
try {
const context = options.data.root;
if (subsidio) {
return `${context.subsidioM3}m³`;
} else return '';
} catch (error) {
console.error(`ERROR: subsidioM3Formatter: ${error}`);
}
}
function repactacionCuotaFormatter(repactacion, options) {
try {
// Access additional data directly from the options
const context = options.data.root;
if (repactacion) {
return `Cuota: ${context.cuotaActual}/${context.numeroTotalCuotas}`;
} else return '';
} catch (error) {
console.error(`ERROR: repactacionCuotaFormatter: ${error}`);
}
}
function repactacionDeudaFormatter(repactacion, options) {
try {
const context = options.data.root;
if (repactacion) {
const number = parseFloat(context.deudaTotal);
const formattedNumber = number.toLocaleString('es-CL', {
style: 'currency',
currency: 'CLP',
minimumFractionDigits: 0,
maximumFractionDigits: 2,
});
return `Deuda: ${formattedNumber}`;
} else return '';
} catch (error) {
console.error(`ERROR: repactacionDeudaFormatter: ${error}`);
}
}
module.exports = {
omitTitleIfNil,
omitValueIfNil,
addSpaceIfNotNil,
dateFormatter,
subsidioPercentageFormatter,
subsidioM3Formatter,
repactacionCuotaFormatter,
repactacionDeudaFormatter,
formatCurrency,
formatCurrencyWithSymbol,
};