Skip to content

Instantly share code, notes, and snippets.

View juanmaguitar's full-sized avatar

JuanMa juanmaguitar

View GitHub Profile
function saySomething( message, name ) {
console.log (message + " " + name + "!")
}
saySomething("Hi", "juanma") // "Hi juanma!"
saySomething("Bye", "juanma") // "Bye juanma!"
var sayHi = saySomething.bind(null, "Hi");
typeof sayHi === 'function' // true
sayHi("juanma") // "Hi juanma!"
@juanmaguitar
juanmaguitar / app.js
Created November 4, 2016 11:29
Promises Examples
const fs = require("fs")
fs.readFile('test.txt', 'utf-8', (err, contentFile) => {
setTimeout( ()=> {
contentFile = contentFile.toUpperCase();
fs.writeFile('output.txt', contentFile, (err) => {
console.log("file written")
@juanmaguitar
juanmaguitar / studentShuffle.js
Last active September 21, 2017 11:25
shuffle and group
function shuffleAndGroup (students, sizeGroups = 2) {
var shuffled = students.reduce( (acc, item, i, array) => {
var random = Math.floor(Math.random() * array.length);
array[i] = array.splice(random,1,item)[0]
return array
},[])
var grouped = shuffled.reduce((acc, current, index, array) => {
if (index % sizeGroups) {
@juanmaguitar
juanmaguitar / es2015-enhanced-objects-examples.js
Last active November 3, 2016 11:52
es2015 Enhanced Objects Example
var name = "juanma";
var handler = () => `handling things for ${this.name}...`;
var theProtoObj = {
location: "barcelona",
toString() {
return `I'm super!!`
}
}
@juanmaguitar
juanmaguitar / GeospacialSpherical.png
Last active February 21, 2018 03:40
MongoDb notes
GeospacialSpherical.png
@juanmaguitar
juanmaguitar / index3.html
Created October 15, 2016 08:44
React component change state (basic example)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Component</title>
</head>
<body>
<!-- container node -->
<div id="app"></div>
@juanmaguitar
juanmaguitar / index.html
Last active October 15, 2016 08:44
Basic React Component
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First Component</title>
</head>
<body>
<!-- container node -->
<div id="app"></div>
@juanmaguitar
juanmaguitar / user-pass-generated.js
Last active October 6, 2016 17:13
User password genrerated
function User ( name, username ) {
var password = User.generatePassword(15);
this.name = name;
this.username = username;
this.password = User.encryptPassword( password );
console.log("Your password is : " + password)
@juanmaguitar
juanmaguitar / generators-es2015.js
Last active September 29, 2016 19:28
Generators -ES2015
# Generators - ES2015
http://riadbenguella.com/how-es6-generators-are-changing-how-we-write-javascript/
Generators are special functions that can be run, paused and resumed at different stages of their execution, thanks to the special keyword `yield`.
```javascript
function* myGenerator() {
yield 'first';
let input = yield 'second';
@juanmaguitar
juanmaguitar / callback-promises-generators-async.js
Last active June 20, 2018 10:22
callbacks vs promises vs generators vs async
// https://medium.com/@rdsubhas/es6-from-callbacks-to-promises-to-generators-87f1c0cd8f2e#.q7boouq4o
/* Step 1: Callback hell — N levels deep */
var request = require('request');
var url1='http://httpbin.org/', url2=url1, url3=url1, url4=url1;
function foo(finalCallback) {
request.get(url1, function(err1, res1) {
if (err1) { return finalCallback(err1); }