Created
August 22, 2011 17:57
-
-
Save wchiquito/1163036 to your computer and use it in GitHub Desktop.
v.1/Ejercicio 4/Reunión #4/Grupo de Estudio JavaScript
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
| var _STRING = "string", _ARRAY = "array", _HELLO = "Hola", _SPACE = " "; | |
| function toType(obj) { | |
| //return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
| //Expresión regular mejorada | |
| //return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); | |
| // Usando el "slice" | |
| return ({}).toString.call(obj).slice(8,-1).toLowerCase(); | |
| } | |
| function isArray(input) { | |
| return toType(input) === _ARRAY; | |
| } | |
| function isString(input) { | |
| return toType(input) === _STRING; | |
| } | |
| function convertString(input) { | |
| (toType(input) === _STRING && (input = input.trim())) || (input = ""); | |
| return input; | |
| } | |
| function getNamesFromArray(input) { | |
| var names = [], | |
| namesMaxLen = input.length, | |
| item, | |
| current; | |
| for (item = namesMaxLen; item > 0; --item) { | |
| current = input[namesMaxLen - item]; | |
| if (isString(current)) { | |
| names.push(current); | |
| } else if (isArray(current)) { | |
| names = names.concat(getNamesFromArray(current)); | |
| } | |
| } | |
| return names; | |
| } | |
| var crearSaludo = function(input) { | |
| var result, | |
| current, | |
| names, | |
| namesMaxLen, | |
| subNamesMaxLen, | |
| item, | |
| subItem, | |
| funcSaludo; | |
| var saludo = function() { | |
| current = convertString(input); | |
| if (current.length > 0) { | |
| result = _HELLO + _SPACE + current; | |
| } | |
| return result; | |
| }; | |
| if (isArray(input)) { | |
| funcSaludo = [], names = [], namesMaxLen = input.length; | |
| for (item = namesMaxLen; item > 0; --item) { | |
| current = input[namesMaxLen - item]; | |
| if (isString(current)) { | |
| funcSaludo.push(crearSaludo(current)); | |
| } else if (isArray(current)) { | |
| names = getNamesFromArray(current); | |
| subNamesMaxLen = names.length; | |
| for (subItem = subNamesMaxLen; subItem > 0; --subItem) { | |
| funcSaludo.push(crearSaludo(names[subNamesMaxLen - subItem])); | |
| } | |
| } | |
| } | |
| } else { | |
| funcSaludo = saludo; | |
| } | |
| return funcSaludo; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mis pruebas http://jsperf.com/test-getobjecttype