Skip to content

Instantly share code, notes, and snippets.

View sergiodxa's full-sized avatar

Sergio Xalambrí sergiodxa

View GitHub Profile
@sergiodxa
sergiodxa / string.capitalize.js
Last active August 29, 2015 14:06
Función para capitalizar strings
function capitalize() {
try {
if (typeof this !== 'string') throw new Error('Capitalize must be used in a String value.');
const str = this.split(' ')
.map(v => {
return v.split('')
.map((v,i) => (i === 0) ? v.toUpperCase() : v)
.join('');
}).join(' ');
return str;
@sergiodxa
sergiodxa / isInViewport.js
Last active August 29, 2015 14:09
Function to check if a DOM element is in the viewport
/*
@el = DOM element
@full = true | default: false // If true check if any part of the element is in the viewport
*/
function isInViewport (el, full) {
var full = full || false;
// check if the element is a jQuery object
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
@sergiodxa
sergiodxa / async-functions.js
Last active August 29, 2015 14:21
Funciones asíncronas
import xhr from 'promised-xhr';
async function mostrarDatos () {
try {
const data = await xhr.get('/api/resource');
const data2 = await xhr.get(data.body.url);
console.log(`${data2.body}!`);
} catch (err) {
console.log(err);
@sergiodxa
sergiodxa / async-generators.js
Last active August 29, 2015 14:21
Generadores asíncronos
import xhr from 'promised-xhr';
async function* obtenerDatos () {
try {
let finised = false;
let page = 0;
while(!finished) {
page++;
const { body: data } = await xhr.get(`/api/resource?page=${page}`);
@sergiodxa
sergiodxa / callbacks.js
Last active August 29, 2015 14:21
Callbacks
import xhr from 'xhr';
xhr({
url: '/api/resource'
}, response => {
console.log(response);
}, error => {
console.log(error);
});
@sergiodxa
sergiodxa / generators.js
Last active August 29, 2015 14:21
Generadores
import xhr from 'promised-xhr';
function* obtenerDatos () {
try {
const data = yield xhr.get('/api/resource');
const data2 = yield xhr.get(data.url);
return `${data2}!`;
} catch (error) {
throw error;
}
@sergiodxa
sergiodxa / promises.js
Last active August 29, 2015 14:21
Promesas
import xhr from 'promised-xhr';
xhr.get('/api/resource')
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
@sergiodxa
sergiodxa / callback-hell.js
Last active August 29, 2015 14:21
Callback hell
import xhr from 'xhr';
const config = { url: '/api/resource' }
xhr(config, response => {
xhr(config, response => {
xhr(config, response => {
xhr(config, response => {
xhr(config, response => {
xhr(config, response => {
@sergiodxa
sergiodxa / node-callback.js
Created May 26, 2015 23:01
Callbacks en Node.js
import fs from 'fs';
fs.readFile('archivo.txt', (error, data) => {
if (error) throw error;
console.log(data.toString());
});