Skip to content

Instantly share code, notes, and snippets.

@alexGuerraDev
Last active May 26, 2021 15:39
Show Gist options
  • Save alexGuerraDev/cfad48d755b99053b04f3ec3291bfe18 to your computer and use it in GitHub Desktop.
Save alexGuerraDev/cfad48d755b99053b04f3ec3291bfe18 to your computer and use it in GitHub Desktop.
IPSUM- JOSE GUERRA
-----------------------------------------------------------------------------------------------------
// OPTIMIZACION DE CODIGO
-----------------------------------------------------------------------------------------------------
//Problema 1
//Suponiendo que el modulo implementado de HTTP es de la libreria por defecto de NODEJS
const HTTP = require('http')
let getSomeData = (url_api) => {
return new Promise((resolve, reject) => {
HTTP.get(url_api, (res) => {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk.toString();
});
res.on('end', () => resolve(rawData));
res.on('error', (error) => reject(error));
})
})
}
getSomeData('http://jsonplaceholder.typicode.com/users').then(data => this.renderDataAtDom(data))
-----------------------------------------------------------------
//otra posible solucion suponiendo que este bloque de codigo se ejecute dentro de una funcion async
let getSomeData = ( url_api ) => {
let res = HTTP.get( url_api )
return res
}
let data = await getSomeData( 'http://localhost:8080/api/users' )
this.renderDataAtDom( data )
-------------------------------------------------------------------
//problema 2
// asumiendo que data es un iterable y que el getSomeData es un array no es necesario iterar
// por lo cual se puede asignar el valor de manera inmediata
let data = getSomeData( 'http://localhost:8080/api/users' )
data.some_attribute = ( data.age/2 )
this.renderAtDom( data )
//Sin remover el For la mejor optimizacion es eliminar la operacion que esta demas
let data = getSomeData( 'http://localhost:8080/api/users' )
for( let i = 0; i < data.length; i++ ){
data.some_attribute = ( data.age/2 ) // se remueve operacion innecesaria
}
this.renderAtDom( data )
-----------------------------------------------------------------------------------------------------
// CICLOS DE VIDA
-----------------------------------------------------------------------------------------------------
// SIN RESOLVER
-----------------------------------------------------------------------------------------------------
// OPERADORES ES6
-----------------------------------------------------------------------------------------------------
//Problema 1
const problem1 = () => {
const admins = []
let data = getSomeData('http://localhost:8080/api/users')
data.map((el) => {
if (el.role === 'admin') admins.push(el)
})
return admins
}
-------------------------------------------------------------
//problema 2
class MyComponent {
public data: Array<Object>;
public some_attribute: string;
async constructor() {
let info = await this.getSomeData(
'http://localhost:8080/api/users')
let check = (data) => {
this.some_attribute = data.some_attribute
}
check(info)
}
}
--------------------------------------------------------------
// Problema 3
const problem3 = () => {
let array_one = [1, 2, 3, 4]
let array_two = [5, 6, 7, 8]
let joined_array = [...array_one, ...array_two]
let object_one = { attr: 1, attr_two: 2 }
let object_two = { attr_three: 3, attr_four: 4 }
let joined_object = { ...object_one, ...object_two }
return {
joined_array,
joined_object
}
}
--------------------------------------------------------------
// Problema 4
const problem4 = () => {
const randomValue = () => {
const values = [true, false]
return values[Math.floor(Math.random() * values.length)]
}
let array_from_api_users = [
{
name: 'Postulante 1',
main_tech: ['JS', 'Java']
},
{
name: 'Postulante 2',
main_tech: ['JS', 'Java', 'Python']
}
]
const users_joined = array_from_api_users.map(user => ({ ...user, show_info: randomValue() }))
return users_joined.filter(user => user.show_info === false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment