Created
October 29, 2019 20:06
-
-
Save romaefGit/14c37eb32a4e0bfb0fd42c3b1b8c716a to your computer and use it in GitHub Desktop.
JavaScript - Genera el consecutivo de cualquier número tipo String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // JS | |
| /** | |
| * Autor - Romario Augusto Estrada Flórez -romarioestrada.ff@hotmail.com | |
| * [generaConsecutivo Genera el consecutivo de cualquier cifra con ceros antes del valor o no. | |
| * Ejemplo: 002, genera el string 003, si viene 999, genera el 1000, si viene 099, genera el 100.] | |
| * @param {[String]} ultimoConsecutivo [el consecutivo en string.] | |
| * @return {[String]} [el consecutivo sumado en uno.] | |
| */ | |
| function generaNuevoConsecutivo(ultimoConsecutivo) { | |
| var cantidadCaracteresConsecutivo = ultimoConsecutivo.length; | |
| var nuevoConsecutivo = parseInt(ultimoConsecutivo) + 1; | |
| nuevoConsecutivo = '' + nuevoConsecutivo; | |
| var cantidadCaracteresConsecutivoInt = nuevoConsecutivo.length; | |
| // console.log(cantidadCaracteresConsecutivoInt +' < '+ cantidadCaracteresConsecutivo); | |
| if (cantidadCaracteresConsecutivoInt <= cantidadCaracteresConsecutivo) { | |
| var resultadoCantidadCeros = cantidadCaracteresConsecutivo - cantidadCaracteresConsecutivoInt; | |
| var ceros = ''; | |
| // console.log('resultadoCantidadCeros > ',resultadoCantidadCeros); | |
| for (var i = 0; i < resultadoCantidadCeros; i++) { | |
| ceros += '0'; | |
| } | |
| // console.log('ceros > ', ceros); | |
| nuevoConsecutivo = '' + nuevoConsecutivo; | |
| nuevoConsecutivo = ceros + nuevoConsecutivo | |
| } | |
| if (cantidadCaracteresConsecutivoInt >= cantidadCaracteresConsecutivo) { | |
| nuevoConsecutivo = parseInt(ultimoConsecutivo) + 1; | |
| } | |
| // console.log('nuevoConsecutivo > ', nuevoConsecutivo); | |
| return nuevoConsecutivo; | |
| }; | |
| // ANGULAR Typscript | |
| /** | |
| * Autor - Romario Augusto Estrada Flórez -romarioestrada.ff@hotmail.com | |
| * [generaConsecutivo Genera el consecutivo de cualquier cifra con ceros antes del valor o no. | |
| * Ejemplo: 002, genera el string 003, si viene 999, genera el 1000, si viene 099, genera el 100.] | |
| * @param {[String]} ultimoConsecutivo [el consecutivo en string.] | |
| * @return {[String]} [el consecutivo sumado en uno.] | |
| */ | |
| generaNuevoConsecutivo(ultimoConsecutivo) { | |
| var cantidadCaracteresConsecutivo = ultimoConsecutivo.length; | |
| var nuevoConsecutivo = (parseInt(ultimoConsecutivo) + 1).toString(); | |
| nuevoConsecutivo = '' + nuevoConsecutivo; | |
| var cantidadCaracteresConsecutivoInt = nuevoConsecutivo.length; | |
| // console.log(cantidadCaracteresConsecutivoInt +' < '+ cantidadCaracteresConsecutivo); | |
| if (cantidadCaracteresConsecutivoInt <= cantidadCaracteresConsecutivo) { | |
| var resultadoCantidadCeros = cantidadCaracteresConsecutivo - cantidadCaracteresConsecutivoInt; | |
| var ceros = ''; | |
| // console.log('resultadoCantidadCeros > ',resultadoCantidadCeros); | |
| for (var i = 0; i < resultadoCantidadCeros; i++) { | |
| ceros += '0'; | |
| } | |
| // console.log('ceros > ', ceros); | |
| nuevoConsecutivo = '' + nuevoConsecutivo; | |
| nuevoConsecutivo = ceros + nuevoConsecutivo | |
| } | |
| if (cantidadCaracteresConsecutivoInt >= cantidadCaracteresConsecutivo) { | |
| nuevoConsecutivo = (parseInt(ultimoConsecutivo) + 1).toString(); | |
| } | |
| // console.log('nuevoConsecutivo > ', nuevoConsecutivo); | |
| return nuevoConsecutivo; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment