Skip to content

Commit 84ecf36

Browse files
Merge pull request #2 from bennu/feat/publish-npm
Feat/publish npm
2 parents ccfe633 + 7683769 commit 84ecf36

File tree

14 files changed

+487
-7
lines changed

14 files changed

+487
-7
lines changed

.github/workflows/publish-npm.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ jobs:
2222
node-version: '18.x'
2323
registry-url: 'https://registry.npmjs.org'
2424

25-
- name: Install dependencies and generate package-lock.json
26-
run: |
27-
npm install
28-
npm install --package-lock-only
29-
git add package-lock.json
30-
git commit -m "chore: update package-lock.json" || echo "No changes to commit"
25+
- name: Install dependencies
26+
run: npm ci
3127

3228
- name: Update package.json with release tag
3329
run: |
@@ -46,6 +42,11 @@ jobs:
4642
env:
4743
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4844

45+
- name: Build TypeScript
46+
run: |
47+
npx tsc --target es2018 --module commonjs --declaration --outDir ./dist --strict --esModuleInterop --skipLibCheck --forceConsistentCasingInFileNames src/**/*.ts
48+
ls -la dist/
49+
4950
- name: Publish to npm
5051
run: npm publish --access public
5152
env:

dist/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Chilean RUT Validator
3+
* A TypeScript package to validate Chilean RUT (Rol Único Tributario) numbers.
4+
*/
5+
export { isValidRut, cleanRut, calculateVerificationDigit, RutValidationResult } from './validate-chilean-rut';

dist/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
/**
3+
* Chilean RUT Validator
4+
* A TypeScript package to validate Chilean RUT (Rol Único Tributario) numbers.
5+
*/
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
exports.calculateVerificationDigit = exports.cleanRut = exports.isValidRut = void 0;
8+
var validate_chilean_rut_1 = require("./validate-chilean-rut");
9+
Object.defineProperty(exports, "isValidRut", { enumerable: true, get: function () { return validate_chilean_rut_1.isValidRut; } });
10+
Object.defineProperty(exports, "cleanRut", { enumerable: true, get: function () { return validate_chilean_rut_1.cleanRut; } });
11+
Object.defineProperty(exports, "calculateVerificationDigit", { enumerable: true, get: function () { return validate_chilean_rut_1.calculateVerificationDigit; } });

dist/validate-chilean-rut.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Chilean RUT Validator
3+
*
4+
* A TypeScript implementation to validate Chilean RUT numbers.
5+
*
6+
* @author Created by Bennu
7+
* @license MIT
8+
*/
9+
/**
10+
* Interface for validation result returned by validateRut
11+
*/
12+
export interface RutValidationResult {
13+
isValid: boolean;
14+
formatted: string | null;
15+
raw: string;
16+
rutNumber?: string;
17+
verificationDigit?: string;
18+
}
19+
/**
20+
* Safely converts input to string and cleans a RUT by removing formatting characters
21+
*
22+
* @param rut - RUT input to clean
23+
* @returns Cleaned RUT string
24+
*/
25+
export declare function cleanRut(rut: unknown): string;
26+
/**
27+
* Calculates the verification digit for a given RUT
28+
*
29+
* @param rutNumber - RUT number without verification digit
30+
* @returns Calculated verification digit
31+
*/
32+
export declare function calculateVerificationDigit(rutNumber: string | number): string;
33+
/**
34+
* Validates if a given string is a valid Chilean RUT
35+
*
36+
* @param rut - RUT to validate
37+
* @returns True if the RUT is valid, false otherwise
38+
*/
39+
export declare function isValidRut(rut: unknown): boolean;

dist/validate-chilean-rut.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"use strict";
2+
/**
3+
* Chilean RUT Validator
4+
*
5+
* A TypeScript implementation to validate Chilean RUT numbers.
6+
*
7+
* @author Created by Bennu
8+
* @license MIT
9+
*/
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.cleanRut = cleanRut;
12+
exports.calculateVerificationDigit = calculateVerificationDigit;
13+
exports.isValidRut = isValidRut;
14+
/**
15+
* Safely converts input to string and cleans a RUT by removing formatting characters
16+
*
17+
* @param rut - RUT input to clean
18+
* @returns Cleaned RUT string
19+
*/
20+
function cleanRut(rut) {
21+
if (rut === null || rut === undefined) {
22+
return '';
23+
}
24+
let rutString;
25+
try {
26+
rutString = String(rut);
27+
}
28+
catch (e) {
29+
return '';
30+
}
31+
return rutString.replace(/[.-\s]/g, '').toUpperCase();
32+
}
33+
/**
34+
* Calculates the verification digit for a given RUT
35+
*
36+
* @param rutNumber - RUT number without verification digit
37+
* @returns Calculated verification digit
38+
*/
39+
function calculateVerificationDigit(rutNumber) {
40+
// Convert to string safely
41+
const rutString = String(rutNumber);
42+
if (!/^\d+$/.test(rutString)) {
43+
return '';
44+
}
45+
if (rutString.length > 20) {
46+
return '';
47+
}
48+
const rutDigits = rutString.split('').reverse();
49+
const multipliers = [2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7];
50+
let sum = 0;
51+
for (let i = 0; i < rutDigits.length; i++) {
52+
sum += parseInt(rutDigits[i], 10) * multipliers[i % multipliers.length];
53+
}
54+
const remainder = sum % 11;
55+
const verificationDigit = 11 - remainder;
56+
if (verificationDigit === 11)
57+
return '0';
58+
if (verificationDigit === 10)
59+
return 'K';
60+
return String(verificationDigit);
61+
}
62+
/**
63+
* Validates if a given string is a valid Chilean RUT
64+
*
65+
* @param rut - RUT to validate
66+
* @returns True if the RUT is valid, false otherwise
67+
*/
68+
function isValidRut(rut) {
69+
try {
70+
const cleanedRut = cleanRut(rut);
71+
if (cleanedRut.length < 2)
72+
return false;
73+
if (cleanedRut.length > 20)
74+
return false;
75+
const rutRegex = /^(\d+)([K\d])$/;
76+
if (!rutRegex.test(cleanedRut))
77+
return false;
78+
const match = cleanedRut.match(rutRegex);
79+
if (!match || match.length !== 3)
80+
return false;
81+
const rutNumber = match[1];
82+
const providedVerificationDigit = match[2];
83+
if (rutNumber.length > 20)
84+
return false;
85+
const calculatedVerificationDigit = calculateVerificationDigit(rutNumber);
86+
if (!calculatedVerificationDigit)
87+
return false;
88+
return calculatedVerificationDigit === providedVerificationDigit;
89+
}
90+
catch (e) {
91+
return false;
92+
}
93+
}

node_modules/.package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/fsevents/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/fsevents/README.md

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/fsevents/fsevents.d.ts

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)