Skip to content

Instantly share code, notes, and snippets.

@wchiquito
Created August 22, 2011 17:57
Show Gist options
  • Select an option

  • Save wchiquito/1163036 to your computer and use it in GitHub Desktop.

Select an option

Save wchiquito/1163036 to your computer and use it in GitHub Desktop.
v.1/Ejercicio 4/Reunión #4/Grupo de Estudio JavaScript
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;
};
@wchiquito
Copy link
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 del slice es 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.

@joseanpg
Copy link

Según mis datos la más lenta es precisamente esa

var getObjectType2 = function(obj) {
    return {}.toString.call(obj).slice(8,-1);
  }

http://jsperf.com/object-prototype-tostring-vs-obj

@joseanpg
Copy link

Por cierto los paréntesis son innecesarios como puedes ver.

@wchiquito
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment