-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.js
53 lines (43 loc) · 908 Bytes
/
filters.js
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
const currencyFormatter = new Intl.NumberFormat(
'pt-BR', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
function cnpj (value) {
return value.length === 14
? value.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d)/, '$1.$2.$3/$4-$5')
: value
}
function capitalize (value) {
return value
.toLowerCase()
.split(' ')
.map(word =>
word.charAt(0).toUpperCase() + word.slice(1)
).join(' ')
}
function firstWord (value) {
return value.split(' ')[0]
}
function empty (value) {
if (!value) return '-'
return value
}
function hour (value) {
if (!value) return '-'
if (isNaN(value)) return value
return `${value}h`
}
function currency (number) {
const num = parseFloat(number) || 0
if (num === 0) return '-'
return currencyFormatter.format(num)
}
export const defaultFilters = {
cnpj,
capitalize,
firstWord,
empty,
hour,
currency
}