Skip to content

Instantly share code, notes, and snippets.

View juanmaguitar's full-sized avatar

JuanMa juanmaguitar

View GitHub Profile
@juanmaguitar
juanmaguitar / iterators-fibonacci.js
Last active September 28, 2016 10:21
Iterators Fibonacci (ES2015)
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1
return {
next () {
[ pre, cur ] = [ cur, pre + cur ]
return { done: false, value: cur }
}
}
},
@juanmaguitar
juanmaguitar / iterators-es2015.md
Last active September 28, 2016 15:37
Iterators ES-2015

Iterators (ES-2015)

Iterators

An object is an iterator when it knows how to access items from a collection one at a time, while keeping track of its current position within that sequence.

In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method returns an object with two properties: done and `value.

@juanmaguitar
juanmaguitar / maps-es2015.md
Last active September 28, 2016 14:40
Maps - ES2015

Maps (ES2015)

The Map object is a simple key/value map. Any value (both objects and primitive values) may be used as either a key or a value.

> var animalSounds = new Map();
> animalSounds.set("dog", "woof");
Map {"dog" => "woof"}
> animalSounds.set("cat", "meow");
@juanmaguitar
juanmaguitar / fibonacci-generators-es2015.js
Created September 28, 2016 19:28
fibonacci - generators
function *fibonacci() {
let [prev, current] = [0, 1];
while(true) {
[prev, current] = [current, prev + current];
yield current;
}
}
let seq = fibonacci();
@juanmaguitar
juanmaguitar / generators-examples-es2015.js
Last active September 29, 2016 19:25
generators examples
/* ------------------------------------------------------- */
/* GENERATORS USING PREV VALUES */
/* ------------------------------------------------------- */
function *foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
@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); }
@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 / 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 / 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 / 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>