Skip to content

Commit

Permalink
Merge pull request #4 from jlobos/dev
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
jlobos authored Jan 15, 2017
2 parents 61a0032 + 3d2aa01 commit cf19fe0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '7'
- '6'
- '5'
- '4'
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
environment:
matrix:
- nodejs_version: "7"
- nodejs_version: "6"
- nodejs_version: "5"
- nodejs_version: "4"
Expand Down
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'
const clean = rut => {
return (rut || typeof rut === 'string')
? rut.toString().replace(/[^0-9kK]+/g, '').toUpperCase()

function clean (rut) {
return typeof rut === 'string'
? rut.replace(/[^0-9kK]+/g, '').toUpperCase()
: ''
}

const validate = rut => {
if (!rut || typeof rut !== 'string') { return false }
function validate (rut) {
if (typeof rut !== 'string') { return false }
if (!/^0*(\d{1,3}(\.?\d{3})*)-?([\dkK])$/.test(rut)) {
return false
}
Expand All @@ -22,14 +23,14 @@ const validate = rut => {
t = Math.floor(t / 10)
}

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

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

let result = rut.slice(-4, -1) + '-' + rut.substr(rut.length - 1)
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
}
Expand Down

0 comments on commit cf19fe0

Please sign in to comment.