rut.ts 1.0.0
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 +3 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +65 -0
- package/index.ts +71 -0
- package/package.json +26 -0
- package/tsconfig.json +13 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVerifierDigit = exports.format = exports.clean = exports.check = void 0;
|
|
4
|
+
var invalidRutError = function (rut) { return "String \"".concat(rut, "\" is not valid as a RUT input"); };
|
|
5
|
+
var clean = function (rut) {
|
|
6
|
+
var r = rut.replace(/^0+|[^0-9kK]+/g, '').toUpperCase();
|
|
7
|
+
if (r.length < 8 || r.length > 9)
|
|
8
|
+
throw new Error(invalidRutError(rut));
|
|
9
|
+
return r;
|
|
10
|
+
};
|
|
11
|
+
exports.clean = clean;
|
|
12
|
+
var check = function (rut) {
|
|
13
|
+
if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut))
|
|
14
|
+
return false;
|
|
15
|
+
if (/^0+/.test(rut))
|
|
16
|
+
return false;
|
|
17
|
+
if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut))
|
|
18
|
+
return false;
|
|
19
|
+
var r = clean(rut);
|
|
20
|
+
var t = parseInt(r.slice(0, -1), 10);
|
|
21
|
+
var m = 0;
|
|
22
|
+
var s = 1;
|
|
23
|
+
while (t > 0) {
|
|
24
|
+
s = (s + (t % 10) * (9 - (m++ % 6))) % 11;
|
|
25
|
+
t = Math.floor(t / 10);
|
|
26
|
+
}
|
|
27
|
+
var v = s > 0 ? '' + (s - 1) : 'K';
|
|
28
|
+
return v === r.slice(-1);
|
|
29
|
+
};
|
|
30
|
+
exports.check = check;
|
|
31
|
+
var format = function (rut, dots) {
|
|
32
|
+
if (dots === void 0) { dots = true; }
|
|
33
|
+
if (rut.length === 0)
|
|
34
|
+
return '';
|
|
35
|
+
var r = clean(rut);
|
|
36
|
+
if (dots) {
|
|
37
|
+
var result = r.slice(-4, -1) + '-' + r.substring(r.length - 1);
|
|
38
|
+
for (var i = 4; i < r.length; i += 3) {
|
|
39
|
+
result = r.slice(-3 - i, -i) + '.' + result;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
return r.slice(0, -1) + '-' + r.substring(r.length - 1);
|
|
44
|
+
};
|
|
45
|
+
exports.format = format;
|
|
46
|
+
var getVerifierDigit = function (rut) {
|
|
47
|
+
var r = Array.from(clean(rut), Number);
|
|
48
|
+
if (r.length === 0 || r.includes(NaN)) {
|
|
49
|
+
throw new Error(invalidRutError(rut));
|
|
50
|
+
}
|
|
51
|
+
var modulus = 11;
|
|
52
|
+
var initialValue = 0;
|
|
53
|
+
var sumResult = r
|
|
54
|
+
.reverse()
|
|
55
|
+
.reduce(function (accumulator, currentValue, index) {
|
|
56
|
+
return accumulator + currentValue * ((index % 6) + 2);
|
|
57
|
+
}, initialValue);
|
|
58
|
+
var verifierDigit = modulus - (sumResult % modulus);
|
|
59
|
+
if (verifierDigit === 10)
|
|
60
|
+
return 'K';
|
|
61
|
+
if (verifierDigit === 11)
|
|
62
|
+
return '0';
|
|
63
|
+
return "".concat(verifierDigit);
|
|
64
|
+
};
|
|
65
|
+
exports.getVerifierDigit = getVerifierDigit;
|
package/index.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const invalidRutError = (rut: string): string => `String "${rut}" is not valid as a RUT input`
|
|
2
|
+
|
|
3
|
+
const clean = (rut: string) : string => {
|
|
4
|
+
const r = rut.replace(/^0+|[^0-9kK]+/g, '').toUpperCase()
|
|
5
|
+
if (r.length < 8 || r.length > 9) throw new Error(invalidRutError(rut))
|
|
6
|
+
return r
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const check = (rut: string): boolean => {
|
|
10
|
+
if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut)) return false
|
|
11
|
+
|
|
12
|
+
if (/^0+/.test(rut)) return false
|
|
13
|
+
|
|
14
|
+
if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut)) return false
|
|
15
|
+
|
|
16
|
+
const r = clean(rut)
|
|
17
|
+
|
|
18
|
+
let t = parseInt(r.slice(0, -1), 10)
|
|
19
|
+
let m = 0
|
|
20
|
+
let s = 1
|
|
21
|
+
|
|
22
|
+
while (t > 0) {
|
|
23
|
+
s = (s + (t % 10) * (9 - (m++ % 6))) % 11
|
|
24
|
+
t = Math.floor(t / 10)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const v = s > 0 ? '' + (s - 1) : 'K'
|
|
28
|
+
return v === r.slice(-1)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const format = (rut: string, dots: boolean = true) : string => {
|
|
32
|
+
if(rut.length === 0) return ''
|
|
33
|
+
const r = clean(rut)
|
|
34
|
+
if (dots) {
|
|
35
|
+
let result = r.slice(-4, -1) + '-' + r.substring(r.length - 1)
|
|
36
|
+
for (let i = 4; i < r.length; i += 3) {
|
|
37
|
+
result = r.slice(-3 - i, -i) + '.' + result
|
|
38
|
+
}
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
41
|
+
return r.slice(0, -1) + '-' + r.substring(r.length - 1)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const getVerifierDigit = (rut: string): string => {
|
|
45
|
+
const r = Array.from(clean(rut), Number)
|
|
46
|
+
|
|
47
|
+
if (r.length === 0 || r.includes(NaN)) {
|
|
48
|
+
throw new Error(invalidRutError(rut))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const modulus = 11
|
|
52
|
+
const initialValue = 0
|
|
53
|
+
const sumResult = r
|
|
54
|
+
.reverse()
|
|
55
|
+
.reduce(
|
|
56
|
+
(accumulator, currentValue, index) =>
|
|
57
|
+
accumulator + currentValue * ((index % 6) + 2),
|
|
58
|
+
initialValue
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
const verifierDigit = modulus - (sumResult % modulus)
|
|
62
|
+
|
|
63
|
+
if (verifierDigit === 10) return 'K'
|
|
64
|
+
if (verifierDigit === 11) return '0'
|
|
65
|
+
|
|
66
|
+
return `${verifierDigit}`
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { check, clean, format, getVerifierDigit }
|
|
70
|
+
|
|
71
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rut.ts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Just another toolkit library for working with RUTs in JavaScript (TypeScript)",
|
|
5
|
+
"author": "hansfpc",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublish": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "^4.6.2"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"validation",
|
|
18
|
+
"check",
|
|
19
|
+
"verifierDigit",
|
|
20
|
+
"chilean",
|
|
21
|
+
"rut",
|
|
22
|
+
"formatting",
|
|
23
|
+
"node",
|
|
24
|
+
"browser"
|
|
25
|
+
]
|
|
26
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["es2017", "es7", "es6", "dom"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
}
|
|
13
|
+
}
|