Skip to content

Commit

Permalink
Merge pull request #23 from Azuky/master
Browse files Browse the repository at this point in the history
Nueva opción para formatear rut con o sin puntos
  • Loading branch information
jlobos authored Oct 27, 2021
2 parents 114ad6e + f4de878 commit 2cad702
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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, options?: { dots: boolean }): string;

export function getCheckDigit(rut: string): string;
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ function validate (rut) {
return v === rut.slice(-1)
}

function format (rut) {
function format (rut, options = {
dots: true
}) {
rut = clean(rut)

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
let result
if (options.dots) {
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
}
} else {
result = rut.slice(0, -1) + '-' + rut.substr(rut.length - 1)
}

return result
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rut.js",
"version": "2.0.0",
"version": "2.1.0",
"description": "Sencilla y pequeña libreria para validar y dar formato al RUT",
"license": "MIT",
"repository": "jlobos/rut.js",
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ format('189726317') // '18.972.631-7'
format('18*972*631*7') // '18.972.631-7'
format('9068826-k') // '9.068.826-K'

// Dots es true por default
format('18.972.631-7', { dots: false }) // '18972631-7'
format('189726317', { dots: false }) // '18972631-7'
format('18*972*631*7', { dots: false }) // '18972631-7'
format('9068826-k', { dots: false }) // '9068826-K'

/**
* Obtener el dígito verificador
*/
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ test('format', (t) => {
t.is(format('189726317'), '18.972.631-7')
t.is(format('18*972*631*7'), '18.972.631-7')
t.is(format('9068826-k'), '9.068.826-K')
t.is(format('18.972.631-7', { dots: false }), '18972631-7')
t.is(format('189726317', { dots: false }), '18972631-7')
t.is(format('18*972*631*7', { dots: false }), '18972631-7')
t.is(format('9068826-k', { dots: false }), '9068826-K')
})

test('does not validate rut with 0 on most right digit', t => {
Expand Down

0 comments on commit 2cad702

Please sign in to comment.