File tree Expand file tree Collapse file tree 5 files changed +23
-6
lines changed
Expand file tree Collapse file tree 5 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,6 @@ export function clean(rut: string): string;
22
33export function validate ( rut : string ) : boolean ;
44
5- export function format ( rut : string ) : string ;
5+ export function format ( rut : string , options ?: { dots : boolean } ) : string ;
66
77export function getCheckDigit ( rut : string ) : string ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change @@ -50,6 +50,12 @@ format('189726317') // '18.972.631-7'
5050format (' 18*972*631*7' ) // '18.972.631-7'
5151format (' 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 */
Original file line number Diff line number Diff 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
4044test ( 'does not validate rut with 0 on most right digit' , t => {
You can’t perform that action at this time.
0 commit comments