Skip to content

Commit

Permalink
Merge pull request #20 from jlobos/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
jlobos authored Feb 18, 2021
2 parents bc48acd + 2117ee9 commit 8f16cdf
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 61 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI
on:
- push
- pull_request
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export function clean(rut: string): string;

export function validate(rut: string): boolean;

export function format(rut: string): string;
export function format(rut: string): string;

export function getCheckDigit(rut: string): string;
63 changes: 34 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

function clean (rut) {
return typeof rut === 'string'
? rut.replace(/^0+|[^0-9kK]+/g, '').toUpperCase()
Expand All @@ -14,7 +12,7 @@ function validate (rut) {
// if it starts with 0 we return false
// so a rut like 00000000-0 will not pass
if (/^0+/.test(rut)) {
return false;
return false
}

if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut)) {
Expand All @@ -23,49 +21,56 @@ function validate (rut) {

rut = clean(rut)

var t = parseInt(rut.slice(0, -1), 10)
var m = 0
var s = 1
let t = parseInt(rut.slice(0, -1), 10)
let m = 0
let s = 1

while (t > 0) {
s = (s + (t % 10) * (9 - m++ % 6)) % 11
s = (s + (t % 10) * (9 - (m++ % 6))) % 11
t = Math.floor(t / 10)
}

var v = s > 0 ? '' + (s - 1) : 'K'
const v = s > 0 ? '' + (s - 1) : 'K'
return v === rut.slice(-1)
}

function format (rut) {
rut = clean(rut)

var result = rut.slice(-4, -1) + '-' + rut.substr(rut.length - 1)
for (var i = 4; i < rut.length; i += 3) {
let result = rut.slice(-4, -1) + '-' + rut.substr(rut.length - 1)
for (let i = 4; i < rut.length; i += 3) {
result = rut.slice(-3 - i, -i) + '.' + result
}

return result
}

function getDigit (rut) {
rut = rut+"";
// type check
if (!rut || !rut.length || typeof rut !== 'string') {
return -1;
function getCheckDigit (input) {
const rut = Array.from(clean(input), Number)

if (rut.length === 0 || rut.includes(NaN)) {
throw new Error(`"${input}" as RUT is invalid`)
}

const modulus = 11
const initialValue = 0
const sumResult = rut
.reverse()
.reduce(
(accumulator, currentValue, index) =>
accumulator + currentValue * ((index % 6) + 2),
initialValue
)

const checkDigit = modulus - (sumResult % modulus)

if (checkDigit === 10) {
return 'K'
} else if (checkDigit === 11) {
return '0'
} else {
return checkDigit.toString()
}
// serie numerica
var secuencia = [2,3,4,5,6,7,2,3];
var sum = 0;
//
for (var i=rut.length - 1; i >=0; i--) {
var d = rut.charAt(i)
sum += new Number(d)*secuencia[rut.length - (i + 1)];
};
// sum mod 11
var rest = 11 - (sum % 11);
// si es 11, retorna 0, sino si es 10 retorna K,
// en caso contrario retorna el numero
return rest === 11 ? 0 : rest === 10 ? "K" : rest;
}

module.exports = { validate: validate, clean: clean, format: format, getDigit:getDigit}
module.exports = { validate, clean, format, getCheckDigit }
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "rut.js",
"version": "1.0.2",
"version": "1.1.0",
"description": "Sencilla y pequeña libreria para validar y dar formato al RUT",
"license": "MIT",
"repository": "jlobos/rut.js",
"author": "Jesus Lobos <[email protected]> (https://jlobos.com/)",
"author": {
"name": "Jesus Lobos",
"email": "[email protected]"
},
"main": "index.js",
"scripts": {
"test": "standard && ava"
},
"engines": {
"node": ">=4"
"node": ">=10"
},
"files": [
"index.js",
Expand All @@ -24,7 +27,7 @@
"browser"
],
"devDependencies": {
"ava": "^2.3.0",
"standard": "^14.1.0"
"ava": "^3.15.0",
"standard": "^16.0.3"
}
}
19 changes: 9 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# rut.js 🇨🇱

[![Build Status](https://travis-ci.org/jlobos/rut.js.svg?branch=master)](https://travis-ci.org/jlobos/rut.js)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
Sencilla y pequeña libreria para validar y dar formato al RUT. Funciona en Node.js y Navegadores (Webpack, Browserify, etc.)

Sencilla y pequeña libreria para validar y dar formato al RUT. Funciona en Node.js y Navegadores (Webpack, Browserify)

> Utilizada en producción para manejar mas de 13 millones de chilenos en [Rutify – Rutificador](https://rutify.cl/)
> Utilizada en producción para manejar mas de 13 millones de chilenos en [~Rutify – Rutificador~](https://rutify.cl/)
```js
const { validate, clean, format } = require('rut.js')
const { validate, clean, format, getCheckDigit } = require('rut.js')

// true
validate('18.972.631-7')
Expand Down Expand Up @@ -48,6 +45,12 @@ format('18.972.631-7') // '18.972.631-7'
format('189726317') // '18.972.631-7'
format('18*972*631*7') // '18.972.631-7'
format('9068826-k') // '9.068.826-K'

/**
* Obtener el dígito verificador
*/
getCheckDigit('18.972.631') // '7'
getCheckDigit('9068826') // 'K'
```

## Instalación
Expand All @@ -62,7 +65,3 @@ npm install --save rut.js
npm install
npm test
```

## License

MIT © [Jesus Lobos](https://jlobos.com/)
34 changes: 29 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import { clean, validate, format } from './index'
const test = require('ava')
const { clean, validate, format, getCheckDigit } = require('./index')

test('clean', t => {
test('clean', (t) => {
t.is(clean('189726317'), '189726317')
t.is(clean('18.972.631-7'), '189726317')
t.is(clean('12.345.678-k'), '12345678K')
Expand All @@ -10,7 +10,7 @@ test('clean', t => {
t.is(clean('00000189726317'), '189726317')
})

test('validate', t => {
test('validate', (t) => {
t.true(validate('18.972.631-7'))
t.true(validate('18972631-7'))
t.true(validate('189726317'))
Expand All @@ -30,7 +30,7 @@ test('validate', t => {
t.false(validate(0))
})

test('format', t => {
test('format', (t) => {
t.is(format('18.972.631-7'), '18.972.631-7')
t.is(format('189726317'), '18.972.631-7')
t.is(format('18*972*631*7'), '18.972.631-7')
Expand All @@ -43,3 +43,27 @@ test('does not validate rut with 0 on most right digit', t => {
t.false(validate('0000000000000000000000-0'))
})

test('getCheckDigit', (t) => {
t.is(getCheckDigit('18.657.499-'), '0')
t.is(getCheckDigit('6.383.287'), '1')
t.is(getCheckDigit('13.466.350'), '2')
t.is(getCheckDigit('3,966,753'), '3')
t.is(getCheckDigit('4132650'), '6')
t.is(getCheckDigit('18972631'), '7')
t.is(getCheckDigit('9068826'), 'K')
t.is(getCheckDigit('14774764'), '0')
})

test('getCheckDigit: pass character like a RUT', (t) => {
const { message } = t.throws(() => {
getCheckDigit('Felipe Camiroaga')
})
t.is(message, '"Felipe Camiroaga" as RUT is invalid')
})

test('getCheckDigit: pass 0 like a RUT', (t) => {
const { message } = t.throws(() => {
getCheckDigit(0)
})
t.is(message, '"0" as RUT is invalid')
})

0 comments on commit 8f16cdf

Please sign in to comment.