@lfsoftwares/native-util 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/index.ts +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/src/DateUtils/index.js +2 -0
- package/lib/src/DateUtils/index.js.map +1 -0
- package/lib/src/DateUtils/util.dates.js +293 -0
- package/lib/src/DateUtils/util.dates.js.map +1 -0
- package/lib/src/FormatUtils/index.js +2 -0
- package/lib/src/FormatUtils/index.js.map +1 -0
- package/lib/src/FormatUtils/util.format.js +118 -0
- package/lib/src/FormatUtils/util.format.js.map +1 -0
- package/lib/src/GeneralConsts/consts.general.js +135 -0
- package/lib/src/GeneralConsts/consts.general.js.map +1 -0
- package/lib/src/GeneralConsts/index.js +2 -0
- package/lib/src/GeneralConsts/index.js.map +1 -0
- package/lib/src/GeneralUtils/index.js +2 -0
- package/lib/src/GeneralUtils/index.js.map +1 -0
- package/lib/src/GeneralUtils/util.general.js +71 -0
- package/lib/src/GeneralUtils/util.general.js.map +1 -0
- package/lib/src/MaskUtils/index.js +2 -0
- package/lib/src/MaskUtils/index.js.map +1 -0
- package/lib/src/MaskUtils/util.masks.js +58 -0
- package/lib/src/MaskUtils/util.masks.js.map +1 -0
- package/lib/src/NetworkUtils/index.js +2 -0
- package/lib/src/NetworkUtils/index.js.map +1 -0
- package/lib/src/NetworkUtils/util.network.js +11 -0
- package/lib/src/NetworkUtils/util.network.js.map +1 -0
- package/lib/src/NumberUtils/index.js +2 -0
- package/lib/src/NumberUtils/index.js.map +1 -0
- package/lib/src/NumberUtils/util.numbers.js +212 -0
- package/lib/src/NumberUtils/util.numbers.js.map +1 -0
- package/lib/src/RegexConsts/consts.regex.js +8 -0
- package/lib/src/RegexConsts/consts.regex.js.map +1 -0
- package/lib/src/RegexConsts/index.js +2 -0
- package/lib/src/RegexConsts/index.js.map +1 -0
- package/lib/src/ValidationUtils/index.js +2 -0
- package/lib/src/ValidationUtils/index.js.map +1 -0
- package/lib/src/ValidationUtils/util.validation.js +175 -0
- package/lib/src/ValidationUtils/util.validation.js.map +1 -0
- package/lib/src/index.js +10 -0
- package/lib/src/index.js.map +1 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/src/DateUtils/index.d.ts +2 -0
- package/lib/types/src/DateUtils/index.d.ts.map +1 -0
- package/lib/types/src/DateUtils/util.dates.d.ts +19 -0
- package/lib/types/src/DateUtils/util.dates.d.ts.map +1 -0
- package/lib/types/src/FormatUtils/index.d.ts +2 -0
- package/lib/types/src/FormatUtils/index.d.ts.map +1 -0
- package/lib/types/src/FormatUtils/util.format.d.ts +10 -0
- package/lib/types/src/FormatUtils/util.format.d.ts.map +1 -0
- package/lib/types/src/GeneralConsts/consts.general.d.ts +11 -0
- package/lib/types/src/GeneralConsts/consts.general.d.ts.map +1 -0
- package/lib/types/src/GeneralConsts/index.d.ts +2 -0
- package/lib/types/src/GeneralConsts/index.d.ts.map +1 -0
- package/lib/types/src/GeneralUtils/index.d.ts +2 -0
- package/lib/types/src/GeneralUtils/index.d.ts.map +1 -0
- package/lib/types/src/GeneralUtils/util.general.d.ts +9 -0
- package/lib/types/src/GeneralUtils/util.general.d.ts.map +1 -0
- package/lib/types/src/MaskUtils/index.d.ts +2 -0
- package/lib/types/src/MaskUtils/index.d.ts.map +1 -0
- package/lib/types/src/MaskUtils/util.masks.d.ts +11 -0
- package/lib/types/src/MaskUtils/util.masks.d.ts.map +1 -0
- package/lib/types/src/NetworkUtils/index.d.ts +2 -0
- package/lib/types/src/NetworkUtils/index.d.ts.map +1 -0
- package/lib/types/src/NetworkUtils/util.network.d.ts +5 -0
- package/lib/types/src/NetworkUtils/util.network.d.ts.map +1 -0
- package/lib/types/src/NumberUtils/index.d.ts +2 -0
- package/lib/types/src/NumberUtils/index.d.ts.map +1 -0
- package/lib/types/src/NumberUtils/util.numbers.d.ts +15 -0
- package/lib/types/src/NumberUtils/util.numbers.d.ts.map +1 -0
- package/lib/types/src/RegexConsts/consts.regex.d.ts +8 -0
- package/lib/types/src/RegexConsts/consts.regex.d.ts.map +1 -0
- package/lib/types/src/RegexConsts/index.d.ts +2 -0
- package/lib/types/src/RegexConsts/index.d.ts.map +1 -0
- package/lib/types/src/ValidationUtils/index.d.ts +2 -0
- package/lib/types/src/ValidationUtils/index.d.ts.map +1 -0
- package/lib/types/src/ValidationUtils/util.validation.d.ts +8 -0
- package/lib/types/src/ValidationUtils/util.validation.d.ts.map +1 -0
- package/lib/types/src/index.d.ts +10 -0
- package/lib/types/src/index.d.ts.map +1 -0
- package/package.json +56 -0
- package/src/DateUtils/index.ts +1 -0
- package/src/DateUtils/util.dates.ts +337 -0
- package/src/FormatUtils/index.ts +1 -0
- package/src/FormatUtils/util.format.ts +151 -0
- package/src/GeneralConsts/consts.general.ts +138 -0
- package/src/GeneralConsts/index.ts +1 -0
- package/src/GeneralUtils/index.ts +1 -0
- package/src/GeneralUtils/util.general.ts +76 -0
- package/src/MaskUtils/index.ts +1 -0
- package/src/MaskUtils/util.masks.ts +78 -0
- package/src/NetworkUtils/index.ts +1 -0
- package/src/NetworkUtils/util.network.ts +15 -0
- package/src/NumberUtils/index.ts +1 -0
- package/src/NumberUtils/util.numbers.ts +250 -0
- package/src/RegexConsts/consts.regex.ts +10 -0
- package/src/RegexConsts/index.ts +1 -0
- package/src/ValidationUtils/index.ts +1 -0
- package/src/ValidationUtils/util.validation.ts +215 -0
- package/src/index.ts +9 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { Platform } from 'react-native'
|
|
2
|
+
import { formatCurrency } from 'react-native-format-currency'
|
|
3
|
+
|
|
4
|
+
class NumberUtils {
|
|
5
|
+
static stringToFloat(textValue?: string) {
|
|
6
|
+
if (textValue) {
|
|
7
|
+
textValue = textValue
|
|
8
|
+
.replace('R$', '')
|
|
9
|
+
.replace('%', '')
|
|
10
|
+
.replace('.', '')
|
|
11
|
+
.replace(',', '.')
|
|
12
|
+
.trim()
|
|
13
|
+
|
|
14
|
+
if (textValue.includes('(')) {
|
|
15
|
+
textValue = textValue.split('(')[0].trim()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return parseFloat(textValue)
|
|
19
|
+
} else {
|
|
20
|
+
return NaN
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static editFloatString(textValue?: string) {
|
|
25
|
+
if (textValue) {
|
|
26
|
+
textValue = textValue
|
|
27
|
+
.replace('R$', '')
|
|
28
|
+
.replace('%', '')
|
|
29
|
+
.replace('.', '')
|
|
30
|
+
.trim()
|
|
31
|
+
|
|
32
|
+
if (textValue.includes('(')) {
|
|
33
|
+
textValue = textValue.split('(')[0].trim()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return textValue
|
|
37
|
+
} else {
|
|
38
|
+
return ''
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static currencyFormat(
|
|
43
|
+
currencyValue: number | string | undefined,
|
|
44
|
+
putSymbol = false,
|
|
45
|
+
putPercent = false,
|
|
46
|
+
outMultiple = 0,
|
|
47
|
+
decimals = 2,
|
|
48
|
+
) {
|
|
49
|
+
try {
|
|
50
|
+
let currencyNumber: number
|
|
51
|
+
|
|
52
|
+
if (!currencyValue) {
|
|
53
|
+
currencyValue = 0
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (typeof currencyValue === 'string') {
|
|
57
|
+
currencyNumber = parseFloat(
|
|
58
|
+
currencyValue
|
|
59
|
+
.replace('R$', '')
|
|
60
|
+
.replace('%', '')
|
|
61
|
+
.replace('.', '')
|
|
62
|
+
.replace(',', '.')
|
|
63
|
+
.trim(),
|
|
64
|
+
)
|
|
65
|
+
} else {
|
|
66
|
+
currencyNumber = currencyValue
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isNaN(currencyNumber)) {
|
|
70
|
+
currencyNumber = 0
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (Platform.OS === 'ios') {
|
|
74
|
+
let currencyText = new Intl.NumberFormat('pt-BR', {
|
|
75
|
+
style: 'currency',
|
|
76
|
+
currency: 'BRL',
|
|
77
|
+
maximumFractionDigits: decimals,
|
|
78
|
+
}).format(currencyNumber)
|
|
79
|
+
|
|
80
|
+
if (putPercent) {
|
|
81
|
+
currencyText = currencyText.replace('R$', '').trim() + '%'
|
|
82
|
+
} else if (!putSymbol) {
|
|
83
|
+
currencyText = currencyText.replace('R$', '').trim()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (outMultiple > 0) {
|
|
87
|
+
if (currencyText.includes(',')) {
|
|
88
|
+
if (outMultiple === 1 || parseInt(currencyText.split(',')[1]) === 0)
|
|
89
|
+
currencyText = currencyText.split(',')[0]
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return currencyText
|
|
94
|
+
} else {
|
|
95
|
+
currencyNumber = parseFloat(currencyNumber.toFixed(decimals))
|
|
96
|
+
|
|
97
|
+
const [currencyTextWithSymbol, currencyTextWithoutSymbol] =
|
|
98
|
+
formatCurrency({
|
|
99
|
+
amount: currencyNumber,
|
|
100
|
+
code: 'BRL',
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const currencyParts = putSymbol
|
|
104
|
+
? currencyTextWithSymbol.split(',')
|
|
105
|
+
: String(currencyTextWithoutSymbol).split(',')
|
|
106
|
+
|
|
107
|
+
let decimal = ',' + '0'.repeat(decimals)
|
|
108
|
+
|
|
109
|
+
if (currencyParts[1]) {
|
|
110
|
+
decimal = ',' + currencyParts[1].padEnd(decimals, '0')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (
|
|
114
|
+
outMultiple === 1 ||
|
|
115
|
+
(outMultiple > 0 && parseInt(decimal.replace(',', '')) === 0)
|
|
116
|
+
) {
|
|
117
|
+
return currencyParts[0]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!putSymbol && putPercent) {
|
|
121
|
+
decimal += '%'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return currencyParts[0] + decimal
|
|
125
|
+
}
|
|
126
|
+
} catch {
|
|
127
|
+
return String(currencyValue)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static extractCurrencyValue(currencyText: string) {
|
|
132
|
+
if (currencyText.length > 0) {
|
|
133
|
+
const currencyValue = parseFloat(
|
|
134
|
+
currencyText
|
|
135
|
+
.replace('R$', '')
|
|
136
|
+
.replace('%', '')
|
|
137
|
+
.replace('.', '')
|
|
138
|
+
.replace(',', '.')
|
|
139
|
+
.trim(),
|
|
140
|
+
)
|
|
141
|
+
return isNaN(currencyValue) ? 0 : currencyValue
|
|
142
|
+
} else {
|
|
143
|
+
return 0
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static extractNumbers(text: string, includeComma?: boolean) {
|
|
148
|
+
const characters = '0123456789' + (includeComma ? ',' : '')
|
|
149
|
+
|
|
150
|
+
function check(x: string) {
|
|
151
|
+
return !!characters.includes(x)
|
|
152
|
+
}
|
|
153
|
+
return [...text].reduce((x, y) => (check(y) ? x + y : x), '')
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static numbersAndDots(text: string) {
|
|
157
|
+
const characters = '0123456789.'
|
|
158
|
+
|
|
159
|
+
function check(x: string) {
|
|
160
|
+
return !!characters.includes(x)
|
|
161
|
+
}
|
|
162
|
+
return [...text].reduce((x, y) => (check(y) ? x + y : x), '')
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static stringToInteger(text?: string) {
|
|
166
|
+
if (!text) {
|
|
167
|
+
return 0
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
text = this.extractNumbers(text)
|
|
172
|
+
|
|
173
|
+
if (!isNaN(parseInt(text))) {
|
|
174
|
+
return parseInt(text)
|
|
175
|
+
} else {
|
|
176
|
+
return 0
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
return 0
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static formatDecimalValue(decimalText: string, putSymbol: boolean) {
|
|
184
|
+
const formatedText = decimalText.replace('.', '').replace(',', '')
|
|
185
|
+
|
|
186
|
+
if (formatedText.length === 1) {
|
|
187
|
+
return putSymbol ? 'R$ 0,0' + formatedText : '0,0' + formatedText
|
|
188
|
+
}
|
|
189
|
+
if (formatedText.length === 2) {
|
|
190
|
+
return putSymbol ? 'R$ 0,' + formatedText : '0,' + formatedText
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let valueText = ''
|
|
194
|
+
let digits = 0
|
|
195
|
+
|
|
196
|
+
for (let i = formatedText.length - 1; i >= 0; i--) {
|
|
197
|
+
if (valueText.length === 2) {
|
|
198
|
+
valueText = formatedText[i] + ',' + valueText
|
|
199
|
+
digits += 1
|
|
200
|
+
} else if (valueText.length > 2) {
|
|
201
|
+
if (digits === 3) {
|
|
202
|
+
digits = 1
|
|
203
|
+
valueText = formatedText[i] + '.' + valueText
|
|
204
|
+
} else {
|
|
205
|
+
valueText = formatedText[i] + valueText
|
|
206
|
+
digits += 1
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
valueText = formatedText[i] + valueText
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return putSymbol ? 'R$ ' + valueText : valueText
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static truncValue(value: number) {
|
|
217
|
+
if (value > 0) {
|
|
218
|
+
const textValue = String(value)
|
|
219
|
+
if (textValue.includes('.')) {
|
|
220
|
+
const numberParts = textValue.split('.')
|
|
221
|
+
const integerPart = numberParts[0]
|
|
222
|
+
const decimalPart = numberParts[1].substring(0, 2).padEnd(2, '0')
|
|
223
|
+
|
|
224
|
+
const trucatedNumber = parseFloat(integerPart + '.' + decimalPart)
|
|
225
|
+
|
|
226
|
+
if (!isNaN(trucatedNumber)) {
|
|
227
|
+
return trucatedNumber
|
|
228
|
+
} else {
|
|
229
|
+
return value
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
return value
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
return 0
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static roundValue(value: number, decimalPlaces: number) {
|
|
240
|
+
return parseFloat(value.toFixed(decimalPlaces))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
static roundValueStr(value: number, decimalPlaces: number) {
|
|
244
|
+
const roundedValue = this.roundValue(value, decimalPlaces)
|
|
245
|
+
|
|
246
|
+
return roundedValue.toFixed(decimalPlaces)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export default NumberUtils
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const RNRegex = {
|
|
2
|
+
REGEX_IP: /^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?!$)|$)){4}$/,
|
|
3
|
+
REGEX_URL: /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})(\/[\w]*)*$/,
|
|
4
|
+
REGEX_EMAIL:
|
|
5
|
+
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
6
|
+
REGEX_PHONE:
|
|
7
|
+
/^(\+55)?[\s]?\(?(\d{2})?\)?[\s-]?(9?\d{4}[\s-]?\d{4})$/
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default RNRegex
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './consts.regex'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './util.validation'
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import GeneralConsts from "../GeneralConsts"
|
|
2
|
+
import NumberUtils from "../NumberUtils"
|
|
3
|
+
import RegexConsts from "../RegexConsts"
|
|
4
|
+
|
|
5
|
+
class ValidationUtils {
|
|
6
|
+
static validateCNPJ(cnpj?: string) {
|
|
7
|
+
if (!cnpj) {
|
|
8
|
+
return ''
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
cnpj = cnpj.replace(/[^\d]+/g, '')
|
|
12
|
+
|
|
13
|
+
if (cnpj.length !== 14) return ''
|
|
14
|
+
|
|
15
|
+
if (
|
|
16
|
+
cnpj === '00000000000000' ||
|
|
17
|
+
cnpj === '11111111111111' ||
|
|
18
|
+
cnpj === '22222222222222' ||
|
|
19
|
+
cnpj === '33333333333333' ||
|
|
20
|
+
cnpj === '44444444444444' ||
|
|
21
|
+
cnpj === '55555555555555' ||
|
|
22
|
+
cnpj === '66666666666666' ||
|
|
23
|
+
cnpj === '77777777777777' ||
|
|
24
|
+
cnpj === '88888888888888' ||
|
|
25
|
+
cnpj === '99999999999999'
|
|
26
|
+
)
|
|
27
|
+
return ''
|
|
28
|
+
|
|
29
|
+
let tamanho: number = cnpj.length - 2
|
|
30
|
+
let numeros: string = cnpj.substring(0, tamanho)
|
|
31
|
+
const digitos: string = cnpj.substring(tamanho)
|
|
32
|
+
let soma = 0
|
|
33
|
+
let pos = tamanho - 7
|
|
34
|
+
|
|
35
|
+
for (let i = tamanho; i >= 1; i--) {
|
|
36
|
+
soma += parseInt(numeros.charAt(tamanho - i)) * pos--
|
|
37
|
+
if (pos < 2) pos = 9
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let resultado: number = soma % 11 < 2 ? 0 : 11 - (soma % 11)
|
|
41
|
+
|
|
42
|
+
if (resultado !== parseInt(digitos.charAt(0))) return ''
|
|
43
|
+
|
|
44
|
+
tamanho += 1
|
|
45
|
+
numeros = cnpj.substring(0, tamanho)
|
|
46
|
+
soma = 0
|
|
47
|
+
pos = tamanho - 7
|
|
48
|
+
|
|
49
|
+
for (let i = tamanho; i >= 1; i--) {
|
|
50
|
+
soma += parseInt(numeros.charAt(tamanho - i)) * pos--
|
|
51
|
+
if (pos < 2) {
|
|
52
|
+
pos = 9
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11)
|
|
57
|
+
|
|
58
|
+
if (resultado !== parseInt(digitos.charAt(1))) return ''
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
cnpj.substring(0, 2) +
|
|
62
|
+
'.' +
|
|
63
|
+
cnpj.substring(2, 5) +
|
|
64
|
+
'.' +
|
|
65
|
+
cnpj.substring(5, 8) +
|
|
66
|
+
'/' +
|
|
67
|
+
cnpj.substring(8, 12) +
|
|
68
|
+
'-' +
|
|
69
|
+
cnpj.substring(12, 14)
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static validateCPF(cpf?: string) {
|
|
74
|
+
if (!cpf) {
|
|
75
|
+
return ''
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
cpf = cpf.replace(/[^\d]+/g, '')
|
|
79
|
+
|
|
80
|
+
if (cpf.length !== 11) return ''
|
|
81
|
+
|
|
82
|
+
if (
|
|
83
|
+
cpf === '00000000000' ||
|
|
84
|
+
cpf === '11111111111' ||
|
|
85
|
+
cpf === '22222222222' ||
|
|
86
|
+
cpf === '33333333333' ||
|
|
87
|
+
cpf === '44444444444' ||
|
|
88
|
+
cpf === '55555555555' ||
|
|
89
|
+
cpf === '66666666666' ||
|
|
90
|
+
cpf === '77777777777' ||
|
|
91
|
+
cpf === '88888888888' ||
|
|
92
|
+
cpf === '99999999999'
|
|
93
|
+
)
|
|
94
|
+
return ''
|
|
95
|
+
|
|
96
|
+
let soma = 0
|
|
97
|
+
let resto
|
|
98
|
+
|
|
99
|
+
for (let i = 1; i <= 9; i++)
|
|
100
|
+
soma = soma + parseInt(cpf.substring(i - 1, i)) * (11 - i)
|
|
101
|
+
resto = (soma * 10) % 11
|
|
102
|
+
|
|
103
|
+
if (resto === 10 || resto === 11) resto = 0
|
|
104
|
+
if (resto !== parseInt(cpf.substring(9, 10))) return ''
|
|
105
|
+
|
|
106
|
+
soma = 0
|
|
107
|
+
for (let i = 1; i <= 10; i++)
|
|
108
|
+
soma = soma + parseInt(cpf.substring(i - 1, i)) * (12 - i)
|
|
109
|
+
resto = (soma * 10) % 11
|
|
110
|
+
|
|
111
|
+
if (resto === 10 || resto === 11) resto = 0
|
|
112
|
+
if (resto !== parseInt(cpf.substring(10, 11))) return ''
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
cpf.substring(0, 3) +
|
|
116
|
+
'.' +
|
|
117
|
+
cpf.substring(3, 6) +
|
|
118
|
+
'.' +
|
|
119
|
+
cpf.substring(6, 9) +
|
|
120
|
+
'-' +
|
|
121
|
+
cpf.substring(9, 11)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static validateEmail(email?: string) {
|
|
126
|
+
if (!email) {
|
|
127
|
+
return ''
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return RegexConsts.REGEX_EMAIL.test(email)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static validatePhoneNumber(
|
|
134
|
+
phoneNumber?: string,
|
|
135
|
+
forceNineDigits?: boolean,
|
|
136
|
+
) {
|
|
137
|
+
if (!phoneNumber) {
|
|
138
|
+
return ''
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let phone = NumberUtils.extractNumbers(phoneNumber)
|
|
142
|
+
|
|
143
|
+
if (forceNineDigits && phone.length === 12) {
|
|
144
|
+
phone = phone.slice(0, 4) + '9' + phone.slice(4)
|
|
145
|
+
} else if (forceNineDigits && phone.length === 10) {
|
|
146
|
+
phone = phone.slice(0, 2) + '9' + phone.slice(2)
|
|
147
|
+
} else if (forceNineDigits && phone.length === 8) {
|
|
148
|
+
phone = '9' + phone
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (phone.length === 13) {
|
|
152
|
+
return (
|
|
153
|
+
'(' +
|
|
154
|
+
phone.substring(2, 4) +
|
|
155
|
+
') ' +
|
|
156
|
+
phone.substring(4, 9) +
|
|
157
|
+
'-' +
|
|
158
|
+
phone.substring(9)
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (phone.length < 8 || phone.length > 11) {
|
|
163
|
+
return ''
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let numbers = ''
|
|
167
|
+
|
|
168
|
+
if (phone.length > 9) {
|
|
169
|
+
const ddd = phone.substring(0, 2)
|
|
170
|
+
|
|
171
|
+
if (!GeneralConsts.DDD_BRASIL.includes(ddd)) {
|
|
172
|
+
return ''
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
numbers = phone.substring(2)
|
|
176
|
+
} else {
|
|
177
|
+
numbers = phone
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (numbers === '00000000' || numbers === '000000000') return ''
|
|
181
|
+
|
|
182
|
+
const isValid = RegexConsts.REGEX_PHONE.test(phone)
|
|
183
|
+
|
|
184
|
+
if (isValid) {
|
|
185
|
+
switch (phone.length) {
|
|
186
|
+
case 8:
|
|
187
|
+
return phone.substring(0, 4) + '-' + phone.substring(4)
|
|
188
|
+
case 9:
|
|
189
|
+
return phone.substring(0, 5) + '-' + phone.substring(5)
|
|
190
|
+
case 10:
|
|
191
|
+
return (
|
|
192
|
+
'(' +
|
|
193
|
+
phone.substring(0, 2) +
|
|
194
|
+
')' +
|
|
195
|
+
phone.substring(2, 6) +
|
|
196
|
+
'-' +
|
|
197
|
+
phone.substring(6)
|
|
198
|
+
)
|
|
199
|
+
default:
|
|
200
|
+
return (
|
|
201
|
+
'(' +
|
|
202
|
+
phone.substring(0, 2) +
|
|
203
|
+
')' +
|
|
204
|
+
phone.substring(2, 7) +
|
|
205
|
+
'-' +
|
|
206
|
+
phone.substring(7)
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
return ''
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export default ValidationUtils
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as RNGeneralConsts } from './GeneralConsts'
|
|
2
|
+
export { default as RNRegexConsts } from './RegexConsts'
|
|
3
|
+
export { default as RNDateUtils } from './DateUtils'
|
|
4
|
+
export { default as RNFormatUtils } from './FormatUtils'
|
|
5
|
+
export { default as RNGeneralUtils } from './GeneralUtils'
|
|
6
|
+
export { default as RNMaskUtils } from './MaskUtils'
|
|
7
|
+
export { default as RNNetworkUtils } from './NetworkUtils'
|
|
8
|
+
export { default as RNNumberUtils } from './NumberUtils'
|
|
9
|
+
export { default as RNValidationUtils } from './ValidationUtils'
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"jsx": "react-native",
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationDir": "lib/types",
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "lib",
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"lib": ["DOM", "ESNext"],
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"allowJs": true,
|
|
15
|
+
"declarationMap": true,
|
|
16
|
+
"noEmit": false,
|
|
17
|
+
"esModuleInterop": true,
|
|
18
|
+
"forceConsistentCasingInFileNames": true,
|
|
19
|
+
"strict": true,
|
|
20
|
+
"skipLibCheck": true
|
|
21
|
+
},
|
|
22
|
+
"exclude": ["lib", "node_modules", "babel.config.js", "metro.config.js"]
|
|
23
|
+
}
|