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
Válidos los comentarios, muchas gracias.
- En cuanto al
Object.prototype.toString, luego de varias pruebas de rendimiento el uso de expresiones regulares penaliza bastante para el objetivo que deseamos, por lo que la opción delslicees mejor, sin embargo, una función como:
function toType(obj) {
return ({}).toString.call(obj).slice(8,-1);
}presentó mejor desempeño que usando Object.prototype.toString.
- En cuanto a la expresión regular, es correcto, dentro de las "clases de caracteres" los metacaracteres pierden su significado y se convierten en literales, por ello efectivamente debería quedar
[a-zA-Z]. En cuanto al uso de expresiones regulares, para este caso en particular las pruebas de rendimiento indican que es mejor evitarlas.
Según mis datos la más lenta es precisamente esa
var getObjectType2 = function(obj) {
return {}.toString.call(obj).slice(8,-1);
}Por cierto los paréntesis son innecesarios como puedes ver.
Author
Mis pruebas http://jsperf.com/test-getobjecttype
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Respecto a la función
toTypecreo necesario hacer un par de comentarios:Object.prototype.toStringy en lugar de ir directamente a por ella creamos un objeto transitorio{}y accedemos a dicha función como propiedad hererada. Ahorramos 12 caracteres en el código fuente, pero a cambio crearemos un nuevo objeto inútil cada vez que ejecutemos la función. Puede haber quien confie en que el interprete realice la sustitución y no cree nada.[a-z|A-Z]representa a cualquier minuscula, al caracter '|' y a cualquier mayuscula. Los valores que esperamos en deObject.prototype.toStringno contendrán dicho caracter. Esto no dará ningún error por supuesto ya que la condición de inicio la impone el espacio y da lo mismo lo que venga después siempre que no sea un ']' que es el que cierra el valor devuelto porObject.prototype.toString. Pero formalmente no es muy correcto.Resumiendo hasta aquí: sería más conveniente utilizar
Por otra parte, creo que es preferible evitar la expresiones regulares en la medida de lo posible. Yo utilizo esta función: