rut.ts 1.0.0 → 1.2.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/LICENSE +21 -0
- package/README.md +22 -2
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +79 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +69 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +37 -8
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -65
- package/index.ts +0 -71
- package/tsconfig.json +0 -13
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 @hansfpc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://user-images.githubusercontent.com/12705403/158434864-7f13401a-b973-4267-b035-d9882cf6c545.png" alt="Rut.ts logo" width="100%">
|
|
3
|
+
<h1>Rut.ts: Handle chilean RUT values with ease</h1>
|
|
4
|
+
</div>
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
|
|
7
|
+
This is an isomorphic TypeScript library. Just ~5kb.
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
Using [npm](https://www.npmjs.com/):
|
|
12
|
+
|
|
13
|
+
$ npm i rut.ts
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Using [yarn](https://yarnpkg.com/):
|
|
17
|
+
|
|
18
|
+
$ yarn add rut.ts
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Basic Usage
|
|
22
|
+
|
|
23
|
+
Description is pending...
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type DecomposedRut = {
|
|
2
|
+
body: string;
|
|
3
|
+
verifier: string;
|
|
4
|
+
};
|
|
5
|
+
declare const clean: (rut: string) => string;
|
|
6
|
+
declare const validate: (rut: string, strict?: boolean) => boolean;
|
|
7
|
+
declare const format: (rut: string, dots?: boolean) => string;
|
|
8
|
+
declare const calculateVerifier: (rut: string) => string;
|
|
9
|
+
declare const getBody: (rut: string) => string;
|
|
10
|
+
declare const getVerifier: (rut: string) => string;
|
|
11
|
+
declare const decompose: (rut: string) => DecomposedRut;
|
|
12
|
+
declare const generate: () => string;
|
|
13
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generate = exports.decompose = exports.getVerifier = exports.getBody = exports.calculateVerifier = exports.format = exports.clean = exports.validate = void 0;
|
|
4
|
+
const getInvalidRutError = (rut) => `String "${rut}" is not valid as a RUT input`;
|
|
5
|
+
const patterns = {
|
|
6
|
+
cleaning: /^0+|[^0-9kK]+/g,
|
|
7
|
+
rutLike: /^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,
|
|
8
|
+
suspicious: /^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|kK)?$/gi,
|
|
9
|
+
};
|
|
10
|
+
const clean = (rut) => {
|
|
11
|
+
const cleanRut = rut.toUpperCase().replace(patterns.cleaning, '');
|
|
12
|
+
if (cleanRut.length < 8 || cleanRut.length > 9)
|
|
13
|
+
throw new Error(getInvalidRutError(rut));
|
|
14
|
+
return cleanRut;
|
|
15
|
+
};
|
|
16
|
+
exports.clean = clean;
|
|
17
|
+
const isRutLike = (rut) => patterns.rutLike.test(rut);
|
|
18
|
+
const isSuspicious = (rut) => patterns.suspicious.test(rut);
|
|
19
|
+
const validate = (rut, strict) => {
|
|
20
|
+
if (!isRutLike(rut))
|
|
21
|
+
return false;
|
|
22
|
+
if (strict && isSuspicious(rut))
|
|
23
|
+
return false;
|
|
24
|
+
const r = clean(rut);
|
|
25
|
+
let t = parseInt(r.slice(0, -1), 10);
|
|
26
|
+
let m = 0;
|
|
27
|
+
let s = 1;
|
|
28
|
+
while (t > 0) {
|
|
29
|
+
s = (s + (t % 10) * (9 - (m++ % 6))) % 11;
|
|
30
|
+
t = Math.floor(t / 10);
|
|
31
|
+
}
|
|
32
|
+
const v = s > 0 ? '' + (s - 1) : 'K';
|
|
33
|
+
return v === r.slice(-1);
|
|
34
|
+
};
|
|
35
|
+
exports.validate = validate;
|
|
36
|
+
const format = (rut, dots = true) => {
|
|
37
|
+
if (rut.length === 0)
|
|
38
|
+
return '';
|
|
39
|
+
const cleanRut = clean(rut);
|
|
40
|
+
if (dots) {
|
|
41
|
+
let result = cleanRut.slice(-4, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
42
|
+
for (let i = 4; i < cleanRut.length; i += 3) {
|
|
43
|
+
result = cleanRut.slice(-3 - i, -i) + '.' + result;
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
return cleanRut.slice(0, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
48
|
+
};
|
|
49
|
+
exports.format = format;
|
|
50
|
+
const calculateVerifier = (rut) => {
|
|
51
|
+
const r = Array.from(clean(rut), Number);
|
|
52
|
+
if (r.length === 0 || r.includes(NaN))
|
|
53
|
+
throw new Error(getInvalidRutError(rut));
|
|
54
|
+
const modulus = 11;
|
|
55
|
+
const initialValue = 0;
|
|
56
|
+
const sum = r
|
|
57
|
+
.reverse()
|
|
58
|
+
.reduce((accumulator, currentValue, index) => accumulator + currentValue * ((index % 6) + 2), initialValue);
|
|
59
|
+
const verifierDigit = modulus - (sum % modulus);
|
|
60
|
+
if (verifierDigit === 10)
|
|
61
|
+
return 'K';
|
|
62
|
+
if (verifierDigit === 11)
|
|
63
|
+
return '0';
|
|
64
|
+
return `${verifierDigit}`;
|
|
65
|
+
};
|
|
66
|
+
exports.calculateVerifier = calculateVerifier;
|
|
67
|
+
const getBody = (rut) => clean(rut).slice(0, -1);
|
|
68
|
+
exports.getBody = getBody;
|
|
69
|
+
const getVerifier = (rut) => clean(rut).slice(-1);
|
|
70
|
+
exports.getVerifier = getVerifier;
|
|
71
|
+
const decompose = (rut) => ({ body: getBody(rut), verifier: getVerifier(rut) });
|
|
72
|
+
exports.decompose = decompose;
|
|
73
|
+
const generate = () => {
|
|
74
|
+
const body = Math.floor(Math.random() * (24999999 - 1000000 + 1) + 1000000).toString();
|
|
75
|
+
const verifier = calculateVerifier(body);
|
|
76
|
+
return format(body + verifier);
|
|
77
|
+
};
|
|
78
|
+
exports.generate = generate;
|
|
79
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index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,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;AASH,kCAAW;AARzE,MAAM,SAAS,GAAG,CAAC,GAAW,EAAiB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;AAQ3B,8BAAS;AANpF,MAAM,QAAQ,GAAG,GAAW,EAAE;IAC5B,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,GAAG,OAAO,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAA;IACtF,MAAM,QAAQ,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACxC,OAAO,MAAM,CAAC,IAAI,GAAG,QAAQ,CAAC,CAAA;AAChC,CAAC,CAAA;AAEqF,4BAAQ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type DecomposedRut = {
|
|
2
|
+
body: string;
|
|
3
|
+
verifier: string;
|
|
4
|
+
};
|
|
5
|
+
declare const clean: (rut: string) => string;
|
|
6
|
+
declare const validate: (rut: string, strict?: boolean) => boolean;
|
|
7
|
+
declare const format: (rut: string, dots?: boolean) => string;
|
|
8
|
+
declare const calculateVerifier: (rut: string) => string;
|
|
9
|
+
declare const getBody: (rut: string) => string;
|
|
10
|
+
declare const getVerifier: (rut: string) => string;
|
|
11
|
+
declare const decompose: (rut: string) => DecomposedRut;
|
|
12
|
+
declare const generate: () => string;
|
|
13
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const getInvalidRutError = (rut) => `String "${rut}" is not valid as a RUT input`;
|
|
2
|
+
const patterns = {
|
|
3
|
+
cleaning: /^0+|[^0-9kK]+/g,
|
|
4
|
+
rutLike: /^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,
|
|
5
|
+
suspicious: /^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|kK)?$/gi,
|
|
6
|
+
};
|
|
7
|
+
const clean = (rut) => {
|
|
8
|
+
const cleanRut = rut.toUpperCase().replace(patterns.cleaning, '');
|
|
9
|
+
if (cleanRut.length < 8 || cleanRut.length > 9)
|
|
10
|
+
throw new Error(getInvalidRutError(rut));
|
|
11
|
+
return cleanRut;
|
|
12
|
+
};
|
|
13
|
+
const isRutLike = (rut) => patterns.rutLike.test(rut);
|
|
14
|
+
const isSuspicious = (rut) => patterns.suspicious.test(rut);
|
|
15
|
+
const validate = (rut, strict) => {
|
|
16
|
+
if (!isRutLike(rut))
|
|
17
|
+
return false;
|
|
18
|
+
if (strict && isSuspicious(rut))
|
|
19
|
+
return false;
|
|
20
|
+
const r = clean(rut);
|
|
21
|
+
let t = parseInt(r.slice(0, -1), 10);
|
|
22
|
+
let m = 0;
|
|
23
|
+
let s = 1;
|
|
24
|
+
while (t > 0) {
|
|
25
|
+
s = (s + (t % 10) * (9 - (m++ % 6))) % 11;
|
|
26
|
+
t = Math.floor(t / 10);
|
|
27
|
+
}
|
|
28
|
+
const v = s > 0 ? '' + (s - 1) : 'K';
|
|
29
|
+
return v === r.slice(-1);
|
|
30
|
+
};
|
|
31
|
+
const format = (rut, dots = true) => {
|
|
32
|
+
if (rut.length === 0)
|
|
33
|
+
return '';
|
|
34
|
+
const cleanRut = clean(rut);
|
|
35
|
+
if (dots) {
|
|
36
|
+
let result = cleanRut.slice(-4, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
37
|
+
for (let i = 4; i < cleanRut.length; i += 3) {
|
|
38
|
+
result = cleanRut.slice(-3 - i, -i) + '.' + result;
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
return cleanRut.slice(0, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
43
|
+
};
|
|
44
|
+
const calculateVerifier = (rut) => {
|
|
45
|
+
const r = Array.from(clean(rut), Number);
|
|
46
|
+
if (r.length === 0 || r.includes(NaN))
|
|
47
|
+
throw new Error(getInvalidRutError(rut));
|
|
48
|
+
const modulus = 11;
|
|
49
|
+
const initialValue = 0;
|
|
50
|
+
const sum = r
|
|
51
|
+
.reverse()
|
|
52
|
+
.reduce((accumulator, currentValue, index) => accumulator + currentValue * ((index % 6) + 2), initialValue);
|
|
53
|
+
const verifierDigit = modulus - (sum % modulus);
|
|
54
|
+
if (verifierDigit === 10)
|
|
55
|
+
return 'K';
|
|
56
|
+
if (verifierDigit === 11)
|
|
57
|
+
return '0';
|
|
58
|
+
return `${verifierDigit}`;
|
|
59
|
+
};
|
|
60
|
+
const getBody = (rut) => clean(rut).slice(0, -1);
|
|
61
|
+
const getVerifier = (rut) => clean(rut).slice(-1);
|
|
62
|
+
const decompose = (rut) => ({ body: getBody(rut), verifier: getVerifier(rut) });
|
|
63
|
+
const generate = () => {
|
|
64
|
+
const body = Math.floor(Math.random() * (24999999 - 1000000 + 1) + 1000000).toString();
|
|
65
|
+
const verifier = calculateVerifier(body);
|
|
66
|
+
return format(body + verifier);
|
|
67
|
+
};
|
|
68
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,kBAAkB,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,WAAW,GAAG,+BAA+B,CAAA;AAEjG,MAAM,QAAQ,GAAuB;IACnC,QAAQ,EAAE,gBAAgB;IAC1B,OAAO,EAAE,oCAAoC;IAC7C,UAAU,EAAE,2CAA2C;CACxD,CAAA;AAED,MAAM,KAAK,GAAG,CAAC,GAAW,EAAU,EAAE;IACpC,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,Q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AA;IACtF,MAAM,QAAQ,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACxC,OAAO,MAAM,CAAC,IAAI,GAAG,QAAQ,CAAC,CAAA;AAChC,CAAC,CAAA;AAED,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rut.ts",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "The ultimate tool for handling Chilean RUT values in both Node.js and the browser.",
|
|
5
5
|
"author": "hansfpc",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/esm/index.d.ts",
|
|
8
9
|
"scripts": {
|
|
9
|
-
"build": "
|
|
10
|
-
"
|
|
10
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
11
|
+
"build:esm": "tsc",
|
|
12
|
+
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
13
|
+
"lint": "eslint \"{**/*,*}.{js,ts,jsx,tsx}\"",
|
|
14
|
+
"prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\"",
|
|
15
|
+
"test": "jest --config jestconfig.json",
|
|
16
|
+
"prepare": "npm run build",
|
|
17
|
+
"prepublishOnly": "npm run test && npm run prettier && npm run lint"
|
|
11
18
|
},
|
|
12
19
|
"license": "MIT",
|
|
13
20
|
"devDependencies": {
|
|
14
|
-
"
|
|
21
|
+
"@types/jest": "29.5.12",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "7.7.1",
|
|
23
|
+
"@typescript-eslint/parser": "7.7.1",
|
|
24
|
+
"eslint": "8.57.0",
|
|
25
|
+
"eslint-config-prettier": "9.1.0",
|
|
26
|
+
"eslint-plugin-prettier": "5.1.3",
|
|
27
|
+
"eslint-plugin-react": "7.34.1",
|
|
28
|
+
"eslint-plugin-react-hooks": "4.6.0",
|
|
29
|
+
"jest": "29.7.0",
|
|
30
|
+
"prettier": "3.2.5",
|
|
31
|
+
"ts-jest": "29.1.2",
|
|
32
|
+
"typescript": "5.4.5"
|
|
15
33
|
},
|
|
16
34
|
"keywords": [
|
|
17
35
|
"validation",
|
|
@@ -21,6 +39,17 @@
|
|
|
21
39
|
"rut",
|
|
22
40
|
"formatting",
|
|
23
41
|
"node",
|
|
24
|
-
"browser"
|
|
42
|
+
"browser",
|
|
43
|
+
"generate",
|
|
44
|
+
"deconstruct"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"url": "https://github.com/arrowsoftwarehq/rut.ts.git",
|
|
48
|
+
"type": "git"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"LICENSE",
|
|
53
|
+
"README.md"
|
|
25
54
|
]
|
|
26
55
|
}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
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/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|