Skip to content

Instantly share code, notes, and snippets.

View santiago-puch-giner's full-sized avatar
💫
In an exciting new adventure

Santi Puch Giner santiago-puch-giner

💫
In an exciting new adventure
  • 03:42 (UTC +02:00)
View GitHub Profile
@santiago-puch-giner
santiago-puch-giner / filterListObjects.js
Last active September 25, 2018 23:28
Filter an array of objects to have "unique" objects with respect some of their properties. Uses new Map from ES6.
// Data
let data = [
{
name: 'John',
SSID: 1
},
{
name: 'John',
SSID: 2
},
@santiago-puch-giner
santiago-puch-giner / filterListUnique.js
Created April 25, 2016 14:54
Filter an array of Strings, Numbers, etc. to have only the unique values
let my_array = ['Elem1', 'Elem2', 1, 2, 'Elem3', 'Elem2', 'Elem4', 1, 5];
let filtered = my_array.filter( (element, index, thisArray) => thisArray.indexOf(element) === index);
console.log(my_array);
console.log(filtered);
@santiago-puch-giner
santiago-puch-giner / promises_es6.js
Last active May 5, 2016 22:18
Promises in Javascript ES6
// Snippet for Promises (ES6)
// Create a Promise that is resolved and handle it using "then" methods
let p = new Promise( (resolve, reject) => {
setTimeout( () => {
resolve("Good to go!");
}, 1000);
});
p.then(
@santiago-puch-giner
santiago-puch-giner / rest_spread_es6.js
Created May 5, 2016 22:17
Rest parameters and spread operator in Javascript (ES6)
// Snippet for rest parameters and spread operator (ES6)
// Rest parameters allow you to capture and indefinite number of parameters
// passed to a function
let sum = function(...args) {
// args is an Array with the parameters
return args.reduce( (prev, curr) => prev + curr);
}
console.log(sum(1, 1, 1, 1, 3)) // 7
@santiago-puch-giner
santiago-puch-giner / arrow_functions_es6.js
Created May 5, 2016 22:19
Arrow functions in Javascript (ES6)
// Snippet for arrow functions (ES6)
// Example of a normal arrow function
let substract = (a, b) => {
return a - b;
}
// Example of a one-line arrow function
let add = (a, b) => a + b;
@santiago-puch-giner
santiago-puch-giner / iife.js
Created May 5, 2016 22:21
Immediately Invoked Function Expression in Javascript
// Snippet for IIFE
// An IIFE (Immediately Invoked Function Expression) is a JavaScript function
// that runs as soon as it is defined. They can be written in different ways,
// though a common convention is to enclose both the function expression and
// invocation in parentheses.
var sulphuricAcid = (function(){
var corrosive = true;
var pH = 2;
@santiago-puch-giner
santiago-puch-giner / template_str_es6.js
Created May 5, 2016 22:22
Template string in Javascript (ES6)
// Snippet for template strings (ES6)
let firstName = "Santi";
let lastName = "Puch";
// Instead of doing this...
console.log("My name is " + firstName + " " + lastName);
// Use this...
console.log(`My name is ${firstName} ${lastName}`);
@santiago-puch-giner
santiago-puch-giner / currying.js
Created May 5, 2016 22:22
Currying in Javascript
// Snippet for currying
// Currying can be described as transforming a function that takes N arguments
// so that it can be called as a chain of N functions each taking a single argument.
let greetings = from =>
message =>
recipient =>
'Dear ${recipient},\n\t${message}\n${from}.';
let person = greetings("Jerry");
@santiago-puch-giner
santiago-puch-giner / closure.js
Created May 5, 2016 22:23
Closures in Javascript
// Snippet for closures
// A closure is a special kind of object that combines two things: a function,
// and the environment in which that function was created
function Person(name) {
var _name = name; // Private variable name
this.getName = function() {
return _name;
@santiago-puch-giner
santiago-puch-giner / prototype_inheritance.js
Created May 5, 2016 22:25
Prototype inheritance in Javascript
// Snippet showing inheritance through prototypes
var Animal = function(type){
this.type = type;
};
Animal.prototype.eat = function(){
console.log("They eat food to survive.");
};
var Mammal = function(){};