From 4233ff25e53e8f0fddac95cfbcdbec5c6cddcd3a Mon Sep 17 00:00:00 2001 From: Willian Dias Tamagi Date: Wed, 2 Oct 2019 13:13:22 -0300 Subject: [PATCH 1/2] Changed variable declarations on code for #31 . --- lib/boleto.js | 88 ++++++++++++++++++++++++++++++----------------- lib/edi-helper.js | 10 +++--- lib/formatters.js | 80 ++++++++++++++++++++++++------------------ 3 files changed, 109 insertions(+), 69 deletions(-) diff --git a/lib/boleto.js b/lib/boleto.js index 5eb2b53..a4e4c6a 100644 --- a/lib/boleto.js +++ b/lib/boleto.js @@ -4,24 +4,24 @@ const barcode = require('./barcode') const path = require('path') const moment = require('moment') -var banks = null +let banks = null -var hashString = function (string) { - var hash = 0 - var i - var chr - var len +const hashString = function(string) { + let hash = 0 + let i + let chr + let len if (string.length == 0) return hash for (i = 0, len = string.length; i < len; i++) { chr = string.charCodeAt(i) - hash = ((hash << 5) - hash) + chr + hash = (hash << 5) - hash + chr hash |= 0 // Convert to 32bit integer } return hash } -var Boleto = function (options) { +const Boleto = function(options) { if (!options) { throw 'No options provided initializing Boleto.' } @@ -34,16 +34,26 @@ var Boleto = function (options) { if (!options['data_emissao']) { options['data_emissao'] = moment().utc() } else { - options['data_emissao'] = moment(moment(options['data_emissao']).utc().format('YYYY-MM-DD')) + options['data_emissao'] = moment( + moment(options['data_emissao']) + .utc() + .format('YYYY-MM-DD') + ) } if (!options['data_vencimento']) { - options['data_vencimento'] = moment().utc().add('5', 'days') + options['data_vencimento'] = moment() + .utc() + .add('5', 'days') } else { - options['data_vencimento'] = moment(moment(options['data_vencimento']).utc().format('YYYY-MM-DD')) + options['data_vencimento'] = moment( + moment(options['data_vencimento']) + .utc() + .format('YYYY-MM-DD') + ) } - for (var key in options) { + for (let key in options) { this[key] = options[key] } @@ -51,7 +61,9 @@ var Boleto = function (options) { this['instrucoes'] = formatters.htmlString(this['instrucoes']) if (!this['local_de_pagamento']) { - this['local_de_pagamento'] = 'Até o vencimento, preferencialmente no Banco ' + formatters.capitalize(this['banco']) + this['local_de_pagamento'] = + 'Até o vencimento, preferencialmente no Banco ' + + formatters.capitalize(this['banco']) } this._calculate() @@ -59,21 +71,22 @@ var Boleto = function (options) { Boleto.barcodeRenderEngine = 'img' -Boleto.prototype._calculate = function () { - this['codigo_banco'] = this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo) +Boleto.prototype._calculate = function() { + this['codigo_banco'] = + this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo) this['nosso_numero_dv'] = formatters.mod11(this['nosso_numero'].toString()) this['barcode_data'] = this.bank.barcodeData(this) this['linha_digitavel'] = this.bank.linhaDigitavel(this['barcode_data']) } -Boleto.prototype.renderHTML = function (callback) { - var self = this +Boleto.prototype.renderHTML = function(callback) { + const self = this - var renderOptions = self.bank.options + let renderOptions = self.bank.options renderOptions.boleto = self // Copy renderHelper's methods to renderOptions - for (var key in formatters) { + for (let key in formatters) { renderOptions[key] = formatters[key] } @@ -81,25 +94,36 @@ Boleto.prototype.renderHTML = function (callback) { renderOptions['barcode_height'] = '50' if (Boleto.barcodeRenderEngine == 'bmp') { - renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData(self['barcode_data']) + renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData( + self['barcode_data'] + ) } else if (Boleto.barcodeRenderEngine == 'img') { - renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData(self['barcode_data']) + renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData( + self['barcode_data'] + ) } - renderOptions['boleto']['linha_digitavel_hash'] = hashString(renderOptions['boleto']['linha_digitavel']).toString() - - ejs.renderFile(path.join(__dirname, '/../assets/layout.ejs'), renderOptions, { - cache: true - }, function (err, html) { - if (err) { - throw new Error(err) + renderOptions['boleto']['linha_digitavel_hash'] = hashString( + renderOptions['boleto']['linha_digitavel'] + ).toString() + + ejs.renderFile( + path.join(__dirname, '/../assets/layout.ejs'), + renderOptions, + { + cache: true, + }, + function(err, html) { + if (err) { + throw new Error(err) + } + + callback(html) } - - callback(html) - }) + ) } -module.exports = function (_banks) { +module.exports = function(_banks) { banks = _banks return Boleto } diff --git a/lib/edi-helper.js b/lib/edi-helper.js index 991cf1d..b8cf557 100644 --- a/lib/edi-helper.js +++ b/lib/edi-helper.js @@ -1,6 +1,8 @@ -var crypto = require('crypto') +const crypto = require('crypto') -exports.calculateLineChecksum = function (line) { - return crypto.createHash('sha1').update(line).digest('hex') +exports.calculateLineChecksum = function(line) { + return crypto + .createHash('sha1') + .update(line) + .digest('hex') } - diff --git a/lib/formatters.js b/lib/formatters.js index 62286a9..466b44a 100644 --- a/lib/formatters.js +++ b/lib/formatters.js @@ -1,11 +1,11 @@ const escapeXML = require('ejs').escapeXML const moment = require('moment') -exports.capitalize = function (string) { +exports.capitalize = function(string) { return string.charAt(0).toUpperCase() + string.slice(1) } -exports.addTrailingZeros = function (string, length) { +exports.addTrailingZeros = function(string, length) { string = string.toString() while (string.length < length) { @@ -15,14 +15,20 @@ exports.addTrailingZeros = function (string, length) { return string } -exports.formatAmount = function (amount) { +exports.formatAmount = function(amount) { amount = amount.toString() - var cents = exports.addTrailingZeros(amount.substring(amount.length - 2, amount.length), 2) - var integers = exports.addTrailingZeros(amount.substring(0, amount.length - 2), 1) - - var newIntegers = '' - - for (var i = 0; i < integers.length; i++) { + const cents = exports.addTrailingZeros( + amount.substring(amount.length - 2, amount.length), + 2 + ) + const integers = exports.addTrailingZeros( + amount.substring(0, amount.length - 2), + 1 + ) + + let newIntegers = '' + + for (let i = 0; i < integers.length; i++) { if (i > 0 && (integers.length - i) % 3 == 0) newIntegers += '.' newIntegers += integers[i] } @@ -30,19 +36,19 @@ exports.formatAmount = function (amount) { return 'R$ ' + newIntegers + ',' + cents } -exports.formatDate = function (date) { +exports.formatDate = function(date) { return moment(date).format('DD/MM/YYYY') } -exports.mod11 = function (num, base, r) { +exports.mod11 = function(num, base, r) { if (!base) base = 9 if (!r) r = 0 - var soma = 0 - var fator = 2 + let soma = 0 + let fator = 2 - for (var i = num.length - 1; i >= 0; i--) { - var parcial = parseInt(num[i]) * fator + for (let i = num.length - 1; i >= 0; i--) { + const parcial = parseInt(num[i]) * fator soma += parcial if (fator == base) { @@ -54,42 +60,50 @@ exports.mod11 = function (num, base, r) { if (r == 0) { soma *= 10 - var digito = soma % 11 + const digito = soma % 11 return digito == 10 ? 0 : digito } else if (r == 1) { return soma % 11 } } -exports.mod10 = function (num) { - var total = 0 - var fator = 2 +exports.mod10 = function(num) { + let total = 0 + let fator = 2 - for (var i = num.length - 1; i >= 0; i--) { - var temp = (parseInt(num[i]) * fator).toString() - var tempSum = 0 - for (var j = 0; j < temp.length; j++) { + for (let i = num.length - 1; i >= 0; i--) { + const temp = (parseInt(num[i]) * fator).toString() + let tempSum = 0 + for (let j = 0; j < temp.length; j++) { tempSum += parseInt(temp[j]) } total += tempSum - fator = (fator == 2) ? 1 : 2 + fator = fator == 2 ? 1 : 2 } - var resto = total % 10 - return (resto == 0) ? 0 : (10 - resto) + const resto = total % 10 + return resto == 0 ? 0 : 10 - resto } -exports.fatorVencimento = function (date) { - const parsedDate = moment(date).utc().format('YYYY-MM-DD') - const startDate = moment('1997-10-07').utc().format('YYYY-MM-DD') +exports.fatorVencimento = function(date) { + const parsedDate = moment(date) + .utc() + .format('YYYY-MM-DD') + const startDate = moment('1997-10-07') + .utc() + .format('YYYY-MM-DD') return exports.addTrailingZeros(moment(parsedDate).diff(startDate, 'days'), 4) } -exports.dateFromEdiDate = function (ediDate) { - return new Date(parseInt(ediDate.substring(4, 8)), parseInt(ediDate.substring(2, 4)) - 1, parseInt(ediDate.substring(0, 2))) +exports.dateFromEdiDate = function(ediDate) { + return new Date( + parseInt(ediDate.substring(4, 8)), + parseInt(ediDate.substring(2, 4)) - 1, + parseInt(ediDate.substring(0, 2)) + ) } -exports.removeTrailingZeros = function (string) { +exports.removeTrailingZeros = function(string) { while (string.charAt(0) == '0') { string = string.substring(1, string.length) } @@ -97,6 +111,6 @@ exports.removeTrailingZeros = function (string) { return string } -exports.htmlString = function (str) { +exports.htmlString = function(str) { return str ? escapeXML(str).replace(/\n/g, '
') : str } From 316a2fc17f119d064ad3710ca06d2283933da0be Mon Sep 17 00:00:00 2001 From: Willian Dias Tamagi Date: Wed, 2 Oct 2019 13:48:28 -0300 Subject: [PATCH 2/2] Fixed travis validations. --- lib/boleto.js | 76 +++++++++++++++++++++++------------------------ lib/edi-helper.js | 2 +- lib/formatters.js | 44 +++++++++++++-------------- 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/lib/boleto.js b/lib/boleto.js index a4e4c6a..966398c 100644 --- a/lib/boleto.js +++ b/lib/boleto.js @@ -6,13 +6,13 @@ const moment = require('moment') let banks = null -const hashString = function(string) { +const hashString = function (string) { let hash = 0 let i let chr let len - if (string.length == 0) return hash + if (string.length === 0) return hash for (i = 0, len = string.length; i < len; i++) { chr = string.charCodeAt(i) hash = (hash << 5) - hash + chr @@ -21,49 +21,49 @@ const hashString = function(string) { return hash } -const Boleto = function(options) { +const Boleto = function (options) { if (!options) { throw 'No options provided initializing Boleto.' } - this.bank = banks[options['banco']] + this.bank = banks[ options[ 'banco' ] ] if (!this.bank) { throw 'Invalid bank.' } - if (!options['data_emissao']) { - options['data_emissao'] = moment().utc() + if (!options[ 'data_emissao' ]) { + options[ 'data_emissao' ] = moment().utc() } else { - options['data_emissao'] = moment( - moment(options['data_emissao']) + options[ 'data_emissao' ] = moment( + moment(options[ 'data_emissao' ]) .utc() .format('YYYY-MM-DD') ) } - if (!options['data_vencimento']) { - options['data_vencimento'] = moment() + if (!options[ 'data_vencimento' ]) { + options[ 'data_vencimento' ] = moment() .utc() .add('5', 'days') } else { - options['data_vencimento'] = moment( - moment(options['data_vencimento']) + options[ 'data_vencimento' ] = moment( + moment(options[ 'data_vencimento' ]) .utc() .format('YYYY-MM-DD') ) } for (let key in options) { - this[key] = options[key] + this[ key ] = options[ key ] } - this['pagador'] = formatters.htmlString(this['pagador']) - this['instrucoes'] = formatters.htmlString(this['instrucoes']) + this[ 'pagador' ] = formatters.htmlString(this[ 'pagador' ]) + this[ 'instrucoes' ] = formatters.htmlString(this[ 'instrucoes' ]) - if (!this['local_de_pagamento']) { - this['local_de_pagamento'] = + if (!this[ 'local_de_pagamento' ]) { + this[ 'local_de_pagamento' ] = 'Até o vencimento, preferencialmente no Banco ' + - formatters.capitalize(this['banco']) + formatters.capitalize(this[ 'banco' ]) } this._calculate() @@ -71,15 +71,15 @@ const Boleto = function(options) { Boleto.barcodeRenderEngine = 'img' -Boleto.prototype._calculate = function() { - this['codigo_banco'] = +Boleto.prototype._calculate = function () { + this[ 'codigo_banco' ] = this.bank.options.codigo + '-' + formatters.mod11(this.bank.options.codigo) - this['nosso_numero_dv'] = formatters.mod11(this['nosso_numero'].toString()) - this['barcode_data'] = this.bank.barcodeData(this) - this['linha_digitavel'] = this.bank.linhaDigitavel(this['barcode_data']) + this[ 'nosso_numero_dv' ] = formatters.mod11(this[ 'nosso_numero' ].toString()) + this[ 'barcode_data' ] = this.bank.barcodeData(this) + this[ 'linha_digitavel' ] = this.bank.linhaDigitavel(this[ 'barcode_data' ]) } -Boleto.prototype.renderHTML = function(callback) { +Boleto.prototype.renderHTML = function (callback) { const self = this let renderOptions = self.bank.options @@ -87,33 +87,33 @@ Boleto.prototype.renderHTML = function(callback) { // Copy renderHelper's methods to renderOptions for (let key in formatters) { - renderOptions[key] = formatters[key] + renderOptions[ key ] = formatters[ key ] } - renderOptions['barcode_render_engine'] = Boleto.barcodeRenderEngine - renderOptions['barcode_height'] = '50' + renderOptions[ 'barcode_render_engine' ] = Boleto.barcodeRenderEngine + renderOptions[ 'barcode_height' ] = '50' - if (Boleto.barcodeRenderEngine == 'bmp') { - renderOptions['barcode_data'] = barcode.bmpLineForBarcodeData( - self['barcode_data'] + if (Boleto.barcodeRenderEngine === 'bmp') { + renderOptions[ 'barcode_data' ] = barcode.bmpLineForBarcodeData( + self[ 'barcode_data' ] ) - } else if (Boleto.barcodeRenderEngine == 'img') { - renderOptions['barcode_data'] = barcode.binaryRepresentationForBarcodeData( - self['barcode_data'] + } else if (Boleto.barcodeRenderEngine === 'img') { + renderOptions[ 'barcode_data' ] = barcode.binaryRepresentationForBarcodeData( + self[ 'barcode_data' ] ) } - renderOptions['boleto']['linha_digitavel_hash'] = hashString( - renderOptions['boleto']['linha_digitavel'] + renderOptions[ 'boleto' ][ 'linha_digitavel_hash' ] = hashString( + renderOptions[ 'boleto' ][ 'linha_digitavel' ] ).toString() ejs.renderFile( path.join(__dirname, '/../assets/layout.ejs'), renderOptions, { - cache: true, + cache: true }, - function(err, html) { + function (err, html) { if (err) { throw new Error(err) } @@ -123,7 +123,7 @@ Boleto.prototype.renderHTML = function(callback) { ) } -module.exports = function(_banks) { +module.exports = function (_banks) { banks = _banks return Boleto } diff --git a/lib/edi-helper.js b/lib/edi-helper.js index b8cf557..947df2e 100644 --- a/lib/edi-helper.js +++ b/lib/edi-helper.js @@ -1,6 +1,6 @@ const crypto = require('crypto') -exports.calculateLineChecksum = function(line) { +exports.calculateLineChecksum = function (line) { return crypto .createHash('sha1') .update(line) diff --git a/lib/formatters.js b/lib/formatters.js index 466b44a..1c38c3e 100644 --- a/lib/formatters.js +++ b/lib/formatters.js @@ -1,11 +1,11 @@ const escapeXML = require('ejs').escapeXML const moment = require('moment') -exports.capitalize = function(string) { +exports.capitalize = function (string) { return string.charAt(0).toUpperCase() + string.slice(1) } -exports.addTrailingZeros = function(string, length) { +exports.addTrailingZeros = function (string, length) { string = string.toString() while (string.length < length) { @@ -15,7 +15,7 @@ exports.addTrailingZeros = function(string, length) { return string } -exports.formatAmount = function(amount) { +exports.formatAmount = function (amount) { amount = amount.toString() const cents = exports.addTrailingZeros( amount.substring(amount.length - 2, amount.length), @@ -29,18 +29,18 @@ exports.formatAmount = function(amount) { let newIntegers = '' for (let i = 0; i < integers.length; i++) { - if (i > 0 && (integers.length - i) % 3 == 0) newIntegers += '.' - newIntegers += integers[i] + if (i > 0 && (integers.length - i) % 3 === 0) newIntegers += '.' + newIntegers += integers[ i ] } return 'R$ ' + newIntegers + ',' + cents } -exports.formatDate = function(date) { +exports.formatDate = function (date) { return moment(date).format('DD/MM/YYYY') } -exports.mod11 = function(num, base, r) { +exports.mod11 = function (num, base, r) { if (!base) base = 9 if (!r) r = 0 @@ -48,44 +48,44 @@ exports.mod11 = function(num, base, r) { let fator = 2 for (let i = num.length - 1; i >= 0; i--) { - const parcial = parseInt(num[i]) * fator + const parcial = parseInt(num[ i ]) * fator soma += parcial - if (fator == base) { + if (fator === base) { fator = 1 } fator++ } - if (r == 0) { + if (r === 0) { soma *= 10 const digito = soma % 11 - return digito == 10 ? 0 : digito - } else if (r == 1) { + return digito === 10 ? 0 : digito + } else if (r === 1) { return soma % 11 } } -exports.mod10 = function(num) { +exports.mod10 = function (num) { let total = 0 let fator = 2 for (let i = num.length - 1; i >= 0; i--) { - const temp = (parseInt(num[i]) * fator).toString() + const temp = (parseInt(num[ i ]) * fator).toString() let tempSum = 0 for (let j = 0; j < temp.length; j++) { - tempSum += parseInt(temp[j]) + tempSum += parseInt(temp[ j ]) } total += tempSum - fator = fator == 2 ? 1 : 2 + fator = fator === 2 ? 1 : 2 } const resto = total % 10 - return resto == 0 ? 0 : 10 - resto + return resto === 0 ? 0 : 10 - resto } -exports.fatorVencimento = function(date) { +exports.fatorVencimento = function (date) { const parsedDate = moment(date) .utc() .format('YYYY-MM-DD') @@ -95,7 +95,7 @@ exports.fatorVencimento = function(date) { return exports.addTrailingZeros(moment(parsedDate).diff(startDate, 'days'), 4) } -exports.dateFromEdiDate = function(ediDate) { +exports.dateFromEdiDate = function (ediDate) { return new Date( parseInt(ediDate.substring(4, 8)), parseInt(ediDate.substring(2, 4)) - 1, @@ -103,14 +103,14 @@ exports.dateFromEdiDate = function(ediDate) { ) } -exports.removeTrailingZeros = function(string) { - while (string.charAt(0) == '0') { +exports.removeTrailingZeros = function (string) { + while (string.charAt(0) === '0') { string = string.substring(1, string.length) } return string } -exports.htmlString = function(str) { +exports.htmlString = function (str) { return str ? escapeXML(str).replace(/\n/g, '
') : str }