forked from trickstival/the-pirate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumeric.js
executable file
·52 lines (42 loc) · 1.26 KB
/
numeric.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
export const uuidv4 = () =>
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const rand = (Math.random() * 16) | 0
const value = c === 'x' ? rand : (rand & 0x3) | 0x8
return value.toString(16)
})
import * as cnpj from './numeric-use/cnpj'
import * as cpf from './numeric-use/cpf'
export {
cpf, cnpj
}
export const normalizeLocalCurrency = (numInput) => {
const input = numInput + ''
const getSeparator = () => {
const chars = input.split('').slice(-3)
if (!chars.some(char => ['.', ','].includes(char))) {
return false
}
if (chars.includes(',')) {
return ','
}
return '.'
}
const separator = getSeparator()
// Integers
if (!separator) {
return parseFloat(input.replace(/[.|,]/g, ''))
}
// Comma decimal (ex: 1.000,00)
if (separator === ',') {
return parseFloat(input.replace(/\./g, '').replace(/,/g, '.'))
}
// Dot decimal (ex: 1,000.00)
return parseFloat(input.replace(/,/g, ''))
}
export const interval = (start = 0, end = 10, step = 1) => {
const interval = []
for (let current = start; current <= end; current += step) {
interval.push(current)
}
return interval
}