Skip to content

Commit 2cad702

Browse files
authored
Merge pull request #23 from Azuky/master
Nueva opción para formatear rut con o sin puntos
2 parents 114ad6e + f4de878 commit 2cad702

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export function clean(rut: string): string;
22

33
export function validate(rut: string): boolean;
44

5-
export function format(rut: string): string;
5+
export function format(rut: string, options?: { dots: boolean }): string;
66

77
export function getCheckDigit(rut: string): string;

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ function validate (rut) {
3434
return v === rut.slice(-1)
3535
}
3636

37-
function format (rut) {
37+
function format (rut, options = {
38+
dots: true
39+
}) {
3840
rut = clean(rut)
3941

40-
let result = rut.slice(-4, -1) + '-' + rut.substr(rut.length - 1)
41-
for (let i = 4; i < rut.length; i += 3) {
42-
result = rut.slice(-3 - i, -i) + '.' + result
42+
let result
43+
if (options.dots) {
44+
result = rut.slice(-4, -1) + '-' + rut.substr(rut.length - 1)
45+
for (let i = 4; i < rut.length; i += 3) {
46+
result = rut.slice(-3 - i, -i) + '.' + result
47+
}
48+
} else {
49+
result = rut.slice(0, -1) + '-' + rut.substr(rut.length - 1)
4350
}
4451

4552
return result

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rut.js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Sencilla y pequeña libreria para validar y dar formato al RUT",
55
"license": "MIT",
66
"repository": "jlobos/rut.js",

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ format('189726317') // '18.972.631-7'
5050
format('18*972*631*7') // '18.972.631-7'
5151
format('9068826-k') // '9.068.826-K'
5252

53+
// Dots es true por default
54+
format('18.972.631-7', { dots: false }) // '18972631-7'
55+
format('189726317', { dots: false }) // '18972631-7'
56+
format('18*972*631*7', { dots: false }) // '18972631-7'
57+
format('9068826-k', { dots: false }) // '9068826-K'
58+
5359
/**
5460
* Obtener el dígito verificador
5561
*/

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ test('format', (t) => {
3535
t.is(format('189726317'), '18.972.631-7')
3636
t.is(format('18*972*631*7'), '18.972.631-7')
3737
t.is(format('9068826-k'), '9.068.826-K')
38+
t.is(format('18.972.631-7', { dots: false }), '18972631-7')
39+
t.is(format('189726317', { dots: false }), '18972631-7')
40+
t.is(format('18*972*631*7', { dots: false }), '18972631-7')
41+
t.is(format('9068826-k', { dots: false }), '9068826-K')
3842
})
3943

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

0 commit comments

Comments
 (0)