rut.ts 1.2.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.min.js +1 -0
- package/dist/esm/index.min.js +1 -0
- package/package.json +11 -6
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.
|
|
@@ -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;
|
|
@@ -0,0 +1 @@
|
|
|
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
|
},
|