rut.ts 1.1.0 → 1.3.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 +17 -8
- package/dist/cjs/index.d.ts +5 -4
- package/dist/cjs/index.js +9 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.min.js +1 -0
- package/dist/esm/index.d.ts +5 -4
- package/dist/esm/index.js +6 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +1 -0
- package/package.json +12 -7
package/README.md
CHANGED
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
<div align="center">
|
|
2
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
|
|
3
|
+
<h1>Rut.ts: Handle chilean RUT values with ease using TypeScript.</h1>
|
|
4
4
|
</div>
|
|
5
5
|
|
|
6
|
+
## Features
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
- **Validation**: Check if a RUT is valid, with optional strict mode to detect suspicious patterns.
|
|
9
|
+
- **Cleaning**: Remove extraneous characters and leading zeros from RUT strings.
|
|
10
|
+
- **Formatting**: Convert RUTs into a standardized format, with or without dots.
|
|
11
|
+
- **Decomposition**: Split a RUT into its body and verifier digit.
|
|
12
|
+
- **Generation**: Generate valid random RUT numbers.
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
## Installation
|
|
10
15
|
|
|
11
16
|
Using [npm](https://www.npmjs.com/):
|
|
12
17
|
|
|
13
|
-
$ npm
|
|
18
|
+
$ npm install rut.ts
|
|
14
19
|
|
|
20
|
+
Using [pnpm](https://pnpm.io/):
|
|
15
21
|
|
|
16
|
-
|
|
22
|
+
$ pnpm install rut.ts
|
|
17
23
|
|
|
18
|
-
$ yarn add rut.ts
|
|
19
24
|
|
|
25
|
+
## Usage
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
Please refer to [the documentation](https://rutts-arrowsw.vercel.app/) for examples on how to use this library.
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
|
|
30
|
+
## Contributing
|
|
31
|
+
|
|
32
|
+
Contributions to this library are welcome. Please feel free to submit pull requests or create issues for bugs and feature requests.
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
type
|
|
1
|
+
type DecomposedRut = {
|
|
2
2
|
body: string;
|
|
3
3
|
verifier: string;
|
|
4
4
|
};
|
|
5
5
|
declare const clean: (rut: string) => string;
|
|
6
6
|
declare const validate: (rut: string, strict?: boolean) => boolean;
|
|
7
7
|
declare const format: (rut: string, dots?: boolean) => string;
|
|
8
|
-
declare const
|
|
8
|
+
declare const calculateVerifier: (rut: string) => string;
|
|
9
9
|
declare const getBody: (rut: string) => string;
|
|
10
|
-
declare const
|
|
10
|
+
declare const getVerifier: (rut: string) => string;
|
|
11
|
+
declare const decompose: (rut: string) => DecomposedRut;
|
|
11
12
|
declare const generate: () => string;
|
|
12
|
-
export { validate, clean, format,
|
|
13
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generate = exports.
|
|
3
|
+
exports.generate = exports.decompose = exports.getVerifier = exports.getBody = exports.calculateVerifier = exports.format = exports.clean = exports.validate = void 0;
|
|
4
4
|
const getInvalidRutError = (rut) => `String "${rut}" is not valid as a RUT input`;
|
|
5
5
|
const patterns = {
|
|
6
6
|
cleaning: /^0+|[^0-9kK]+/g,
|
|
@@ -47,7 +47,7 @@ const format = (rut, dots = true) => {
|
|
|
47
47
|
return cleanRut.slice(0, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
48
48
|
};
|
|
49
49
|
exports.format = format;
|
|
50
|
-
const
|
|
50
|
+
const calculateVerifier = (rut) => {
|
|
51
51
|
const r = Array.from(clean(rut), Number);
|
|
52
52
|
if (r.length === 0 || r.includes(NaN))
|
|
53
53
|
throw new Error(getInvalidRutError(rut));
|
|
@@ -63,14 +63,16 @@ const getVerifier = (rut) => {
|
|
|
63
63
|
return '0';
|
|
64
64
|
return `${verifierDigit}`;
|
|
65
65
|
};
|
|
66
|
-
exports.
|
|
66
|
+
exports.calculateVerifier = calculateVerifier;
|
|
67
67
|
const getBody = (rut) => clean(rut).slice(0, -1);
|
|
68
68
|
exports.getBody = getBody;
|
|
69
|
-
const
|
|
70
|
-
exports.
|
|
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;
|
|
71
73
|
const generate = () => {
|
|
72
|
-
const body = Math.floor(
|
|
73
|
-
const verifier =
|
|
74
|
+
const body = Math.floor(Math.random() * (24999999 - 1000000 + 1) + 1000000).toString();
|
|
75
|
+
const verifier = calculateVerifier(body);
|
|
74
76
|
return format(body + verifier);
|
|
75
77
|
};
|
|
76
78
|
exports.generate = generate;
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
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,CAA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,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 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.generate=exports.decompose=exports.getVerifier=exports.getBody=exports.calculateVerifier=exports.format=exports.clean=exports.validate=void 0;const getInvalidRutError=e=>`String "${e}" is not valid as a RUT input`,patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|kK)?$/gi},clean=e=>{const t=e.toUpperCase().replace(patterns.cleaning,"");if(t.length<8||t.length>9)throw new Error(getInvalidRutError(e));return t};exports.clean=clean;const isRutLike=e=>patterns.rutLike.test(e),isSuspicious=e=>patterns.suspicious.test(e),validate=(e,t)=>{if(!isRutLike(e))return!1;if(t&&isSuspicious(e))return!1;const r=clean(e);let o=parseInt(r.slice(0,-1),10),s=0,i=1;for(;o>0;)i=(i+o%10*(9-s++%6))%11,o=Math.floor(o/10);return(i>0?""+(i-1):"K")===r.slice(-1)};exports.validate=validate;const format=(e,t=!0)=>{if(0===e.length)return"";const r=clean(e);if(t){let e=r.slice(-4,-1)+"-"+r.substring(r.length-1);for(let t=4;t<r.length;t+=3)e=r.slice(-3-t,-t)+"."+e;return e}return r.slice(0,-1)+"-"+r.substring(r.length-1)};exports.format=format;const calculateVerifier=e=>{const t=Array.from(clean(e),Number);if(0===t.length||t.includes(NaN))throw new Error(getInvalidRutError(e));const r=11-t.reverse().reduce(((e,t,r)=>e+t*(r%6+2)),0)%11;return 10===r?"K":11===r?"0":`${r}`};exports.calculateVerifier=calculateVerifier;const getBody=e=>clean(e).slice(0,-1);exports.getBody=getBody;const getVerifier=e=>clean(e).slice(-1);exports.getVerifier=getVerifier;const decompose=e=>({body:getBody(e),verifier:getVerifier(e)});exports.decompose=decompose;const generate=()=>{const e=Math.floor(24e6*Math.random()+1e6).toString(),t=calculateVerifier(e);return format(e+t)};exports.generate=generate;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
type
|
|
1
|
+
type DecomposedRut = {
|
|
2
2
|
body: string;
|
|
3
3
|
verifier: string;
|
|
4
4
|
};
|
|
5
5
|
declare const clean: (rut: string) => string;
|
|
6
6
|
declare const validate: (rut: string, strict?: boolean) => boolean;
|
|
7
7
|
declare const format: (rut: string, dots?: boolean) => string;
|
|
8
|
-
declare const
|
|
8
|
+
declare const calculateVerifier: (rut: string) => string;
|
|
9
9
|
declare const getBody: (rut: string) => string;
|
|
10
|
-
declare const
|
|
10
|
+
declare const getVerifier: (rut: string) => string;
|
|
11
|
+
declare const decompose: (rut: string) => DecomposedRut;
|
|
11
12
|
declare const generate: () => string;
|
|
12
|
-
export { validate, clean, format,
|
|
13
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
package/dist/esm/index.js
CHANGED
|
@@ -41,7 +41,7 @@ const format = (rut, dots = true) => {
|
|
|
41
41
|
}
|
|
42
42
|
return cleanRut.slice(0, -1) + '-' + cleanRut.substring(cleanRut.length - 1);
|
|
43
43
|
};
|
|
44
|
-
const
|
|
44
|
+
const calculateVerifier = (rut) => {
|
|
45
45
|
const r = Array.from(clean(rut), Number);
|
|
46
46
|
if (r.length === 0 || r.includes(NaN))
|
|
47
47
|
throw new Error(getInvalidRutError(rut));
|
|
@@ -58,11 +58,12 @@ const getVerifier = (rut) => {
|
|
|
58
58
|
return `${verifierDigit}`;
|
|
59
59
|
};
|
|
60
60
|
const getBody = (rut) => clean(rut).slice(0, -1);
|
|
61
|
-
const
|
|
61
|
+
const getVerifier = (rut) => clean(rut).slice(-1);
|
|
62
|
+
const decompose = (rut) => ({ body: getBody(rut), verifier: getVerifier(rut) });
|
|
62
63
|
const generate = () => {
|
|
63
|
-
const body = Math.floor(
|
|
64
|
-
const verifier =
|
|
64
|
+
const body = Math.floor(Math.random() * (24999999 - 1000000 + 1) + 1000000).toString();
|
|
65
|
+
const verifier = calculateVerifier(body);
|
|
65
66
|
return format(body + verifier);
|
|
66
67
|
};
|
|
67
|
-
export { validate, clean, format,
|
|
68
|
+
export { validate, clean, format, calculateVerifier, getBody, getVerifier, decompose, generate };
|
|
68
69
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
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,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;IACjE,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAA;IACxF,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA;AAED,MAAM,SAAS,GAAG,CAAC,GAAW,EAAW,E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 @@
|
|
|
1
|
+
const getInvalidRutError=e=>`String "${e}" is not valid as a RUT input`,patterns={cleaning:/^0+|[^0-9kK]+/g,rutLike:/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/,suspicious:/^(\d)\1?\.?(\1{3})\.?(\1{3})-?(\d|kK)?$/gi},clean=e=>{const r=e.toUpperCase().replace(patterns.cleaning,"");if(r.length<8||r.length>9)throw new Error(getInvalidRutError(e));return r},isRutLike=e=>patterns.rutLike.test(e),isSuspicious=e=>patterns.suspicious.test(e),validate=(e,r)=>{if(!isRutLike(e))return!1;if(r&&isSuspicious(e))return!1;const t=clean(e);let i=parseInt(t.slice(0,-1),10),n=0,s=1;for(;i>0;)s=(s+i%10*(9-n++%6))%11,i=Math.floor(i/10);return(s>0?""+(s-1):"K")===t.slice(-1)},format=(e,r=!0)=>{if(0===e.length)return"";const t=clean(e);if(r){let e=t.slice(-4,-1)+"-"+t.substring(t.length-1);for(let r=4;r<t.length;r+=3)e=t.slice(-3-r,-r)+"."+e;return e}return t.slice(0,-1)+"-"+t.substring(t.length-1)},calculateVerifier=e=>{const r=Array.from(clean(e),Number);if(0===r.length||r.includes(NaN))throw new Error(getInvalidRutError(e));const t=11-r.reverse().reduce(((e,r,t)=>e+r*(t%6+2)),0)%11;return 10===t?"K":11===t?"0":`${t}`},getBody=e=>clean(e).slice(0,-1),getVerifier=e=>clean(e).slice(-1),decompose=e=>({body:getBody(e),verifier:getVerifier(e)}),generate=()=>{const e=Math.floor(24e6*Math.random()+1e6).toString(),r=calculateVerifier(e);return format(e+r)};export{validate,clean,format,calculateVerifier,getBody,getVerifier,decompose,generate};
|
package/package.json
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rut.ts",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Handle chilean RUT values with ease.",
|
|
5
5
|
"author": "hansfpc",
|
|
6
|
-
"main": "./dist/cjs/index.js",
|
|
7
|
-
"module": "./dist/esm/index.js",
|
|
6
|
+
"main": "./dist/cjs/index.min.js",
|
|
7
|
+
"module": "./dist/esm/index.min.js",
|
|
8
8
|
"types": "./dist/esm/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "npm run build:esm && npm run build:cjs",
|
|
10
|
+
"build": "npm run build:esm && npm run build:cjs && npm run minify",
|
|
11
11
|
"build:esm": "tsc",
|
|
12
12
|
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
13
13
|
"lint": "eslint \"{**/*,*}.{js,ts,jsx,tsx}\"",
|
|
14
14
|
"prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\"",
|
|
15
15
|
"test": "jest --config jestconfig.json",
|
|
16
16
|
"prepare": "npm run build",
|
|
17
|
-
"prepublishOnly": "npm run test && npm run prettier && npm run lint"
|
|
17
|
+
"prepublishOnly": "npm run test && npm run prettier && npm run lint",
|
|
18
|
+
"minify:esm": "terser dist/esm/index.js -o dist/esm/index.min.js -c -m",
|
|
19
|
+
"minify:cjs": "terser dist/cjs/index.js -o dist/cjs/index.min.js -c -m",
|
|
20
|
+
"minify": "npm run minify:esm && npm run minify:cjs"
|
|
18
21
|
},
|
|
19
22
|
"license": "MIT",
|
|
20
23
|
"devDependencies": {
|
|
24
|
+
"@next/eslint-plugin-next": "14.2.3",
|
|
21
25
|
"@types/jest": "29.5.12",
|
|
22
26
|
"@typescript-eslint/eslint-plugin": "7.7.1",
|
|
23
27
|
"@typescript-eslint/parser": "7.7.1",
|
|
@@ -28,6 +32,7 @@
|
|
|
28
32
|
"eslint-plugin-react-hooks": "4.6.0",
|
|
29
33
|
"jest": "29.7.0",
|
|
30
34
|
"prettier": "3.2.5",
|
|
35
|
+
"terser": "5.31.0",
|
|
31
36
|
"ts-jest": "29.1.2",
|
|
32
37
|
"typescript": "5.4.5"
|
|
33
38
|
},
|
|
@@ -44,7 +49,7 @@
|
|
|
44
49
|
"deconstruct"
|
|
45
50
|
],
|
|
46
51
|
"repository": {
|
|
47
|
-
"url": "https://github.com/
|
|
52
|
+
"url": "https://github.com/arrowsoftwarehq/rut.ts.git",
|
|
48
53
|
"type": "git"
|
|
49
54
|
},
|
|
50
55
|
"files": [
|