Skip to content

Instantly share code, notes, and snippets.

@porfidev
Last active February 13, 2018 17:22
Show Gist options
  • Save porfidev/51421fa7d2473cbaf1fc77b4bedc7a01 to your computer and use it in GitHub Desktop.
Save porfidev/51421fa7d2473cbaf1fc77b4bedc7a01 to your computer and use it in GitHub Desktop.
Javascript ES6 Util Codes
let langSentence = 'Estos son los lenguajes requeridos: Javascript, PHP, Java, Python.';
// Inicio y fin de busqueda
let startSearch = langSentence.indexOf(':');
let endSearch = langSentence.indexOf('.', startSearch + 1);
// Obtener el texto despujes del inicio y fin
let langString = langSentence.substring(startSearch + 1, endSearch);
// Convertir cada elemento separado por coma en un elemento del arreglo
let languages = langString.split(',');
console.log(languages); // [' Javascript', ' PHP', ' Java', ' Python']
// ########### Limpiar espacios
languages.forEach((element, index, array) => {
array[index] = element.trim();
});
console.log(languages); // ['Javascript', 'PHP', 'Java', 'Python']
// ########### Alternativa a limpiar espacios con expresión regular
languages = langString.split(/\s*,\s*/);
console.log(languages); // ['Javascript', 'PHP', 'Java', 'Python']
// ########### Simplificar el código
languages = langSentence.substring(startSearch +1, endSearch).split(',');
/** Verfica que la variable este definida y tenga un texto **/
let sampleText = 'Hola Mundo!';
if((sampleText != null && sampleText.length > 0) && typeof sampleText.valueOf() === 'string'){
console.log('good var');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment