-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
211 lines (179 loc) · 5.18 KB
/
index.js
File metadata and controls
211 lines (179 loc) · 5.18 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
// Valid rut formats
const STANDARD = 'standard' // 11.222.333-4
const DASH_ONLY = 'dashOnly' // 11222333-4
const NO_SYMBOLS = 'noSymbols' // 112223334
/**
*
* @param {string|number} input
* @param {string} [formatType='any']
* @returns A detailed representation of the given input.
*/
function toJSON(input, formatType = STANDARD) {
const { digit, serial } = split(removeInvalidChars(input))
return {
digit,
formattedValue: format(input, formatType),
isFormatValid: isFormatValid(input, formatType),
isValid: isValid(input),
serial,
}
}
/**
*
* @param {string|number} input - Any string or number
* @param {'standard'|'dashOnly'|'noSymbols'} [formatType="standard"] - One of the listed strings. Defaults to ``standard``
* @returns {string} String formatted according to the ``formatType`` param.
* Removes any invalid character before applying the format.
*
* @example
* format('108646292') // => '10.864.629-2'
* format('108646292', 'dashOnly') // => '10864629-2'
* format('10.864.629-2', 'noSymbols') // => '108646292'
*/
function format(input, formatType = STANDARD) {
const FORMATTERS = {
[STANDARD]: standardFormatter,
[DASH_ONLY]: dashOnlyFormatter,
[NO_SYMBOLS]: noSymbolsFormatter,
get: function (key) {
return this[key] || this[STANDARD]
},
}
input = removeInvalidChars(input)
return hasMinLength(input) ? FORMATTERS.get(formatType)(split(input)) : input
}
/**
*
* @param {string|number} input - Any string or number
* @param {'any'|'standard'|'dashOnly'|'noSymbols'} [formatType="any"] - One of the listed strings. Defaults to ``'any'``.
* @returns {boolean} Returns true when ``input`` matches the given ``formatType`` or any of them if ``formatType='any'`` is given, otherwise false.
* @example
* isFormatValid('108646292', 'standard') // => false
* isFormatValid('108646292', 'any') // => true
* isFormatValid('108646292', 'noSymbols') // => true
*/
function isFormatValid(input, formatType = 'any') {
return getFormatExpressions(formatType).reduce(
(isValid, exp) => isValid || exp.test(input),
false
)
}
/**
*
* @param {string|number} input
* @returns {boolean} Returns true when the given input has only valid characters
* is of length two or greater and its digit is valid for the serial number.
* Does not validate neither max length nor input format.
* @example
* isValid('108646292') // => true
* isValid('10.864.629-2') // => true
* isValid('111') // => false
* isValid('1n1') // => false
*/
function isValid(input) {
const cleanInput = removeInvalidChars(input)
return (
hasOnlyValidChars(input) &&
hasMinLength(cleanInput) &&
isDigitValid(split(cleanInput))
)
}
// Format related functions
function standardFormatter({ serial, digit }) {
return `${formatWithSeparators(serial)}-${digit}`
}
function dashOnlyFormatter({ serial, digit }) {
return `${serial}-${digit}`
}
function noSymbolsFormatter({ serial, digit }) {
return `${serial}${digit}`
}
function formatWithSeparators(serial) {
return splitBySeparatorPosition(serial).join('.')
}
function splitBySeparatorPosition(
serial,
terms = [],
breakpoint = serial.length % 3 || 3
) {
if (serial.length == 0) {
return terms
}
terms.push(serial.slice(0, breakpoint))
return splitBySeparatorPosition(serial.slice(breakpoint), terms)
}
function getFormatExpressions(formatType) {
const FORMATS = {
[STANDARD]: /^\d{1,3}(\.\d{3})+-[0-9Kk]$/,
[DASH_ONLY]: /^\d{1,3}(\d{3})+-[0-9Kk]$/,
[NO_SYMBOLS]: /^\d+[0-9Kk]$/,
get: function (key) {
return this[key] || this[STANDARD]
},
}
return formatType == 'any'
? [STANDARD, DASH_ONLY, NO_SYMBOLS].map((type) => FORMATS[type])
: [FORMATS.get(formatType)]
}
// Validation related functions
function hasOnlyValidChars(input) {
return !/[^0-9Kk\.-]/g.test(input)
}
function hasMinLength(rut) {
return rut.length > 1
}
function isDigitValid({ serial, digit }) {
return getValidDigit(serial) == digit.toUpperCase()
}
function getValidDigit(serial) {
return parseNumericToDigit(calculateNumericDigit(serial))
}
// These constants are part of the algorithm definition. See https://es.wikipedia.org/wiki/Rol_%C3%9Anico_Tributario
function calculateNumericDigit(serial) {
const MODULE = 11
const MIN_FACTOR = 2
const MAX_FACTOR = 7
const { sum } = serial.split('').reduceRight(
(state, char) => ({
sum: state.sum + Number(char) * state.factor,
factor: state.factor == MAX_FACTOR ? MIN_FACTOR : state.factor + 1,
}),
{ sum: 0, factor: MIN_FACTOR }
)
return MODULE - (sum % MODULE)
}
function parseNumericToDigit(num) {
switch (num) {
case 10:
return 'K'
case 11:
return '0'
default:
return Number.isNaN(num) ? null : String(num)
}
}
// Utils
function removeInvalidChars(input) {
return String(input).replace(/[^0-9Kk]/g, '')
}
/**
*
* @param {string} rut
* @returns {{serial: string, digit: string}}
*/
function split(rut) {
const lastPosition = rut.length - 1
return {
serial: rut.slice(0, lastPosition),
digit: rut.slice(lastPosition),
}
}
const rutUtils = {
format,
isFormatValid,
isValid,
toJSON,
}
module.exports = rutUtils
module.exports.default = rutUtils