Skip to content

Instantly share code, notes, and snippets.

View roden0's full-sized avatar
🎯
Focusing

Rodrigo Encinas roden0

🎯
Focusing
  • Barcelona
View GitHub Profile
// sequence of functions that you want to be invoked for a specific value
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
// nvokes the functions in reverse order
const compose = (...fns) => x => fns.reverse().reduce((v, f) => f(v), x);
/*
// example: calculating the checkout total
const calculateTotal = pipe(
// Use the browser's built-in functionality to quickly and safely escape
// the string
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
@roden0
roden0 / endOfTheWorldDate.js
Created July 17, 2018 12:07
End of the World Date
(function(){
//https://en.wikipedia.org/wiki/Year_2038_problem
return new Date(0x7fffffff * 1e3);
/**
* is the maximum number of seconds elapsed
* since 1 January 1970 00:00:00 UTC
* expressible by a signed 32-bit integer
*/
})();
@roden0
roden0 / fibonacci_gen.js
Created May 28, 2018 20:06
fibonacci generator
function* fibonacci() { // una función generador
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
console.log(n);
@roden0
roden0 / prom_gen.js
Created May 28, 2018 20:05
promise generator
function* genFunc(){ //looks synchronously written
var post1title = yield fetch("https://jsonplaceholder.typicode.com/posts/1");
console.log(post1title);
//post1title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
var post2title = yield fetch("https://jsonplaceholder.typicode.com/posts/2");
console.log(post2title);
//post2title = "qui est esse"
}
@roden0
roden0 / swars.js
Created May 28, 2018 20:01
SWARS API service with generators
function run(genFunc){
const genObject= genFunc(); //creating a generator object
function iterate(iteration){ //recursive function to iterate through promises
if(iteration.done) //stop iterating when done and return the final value wrapped in a promise
return Promise.resolve(iteration.value);
return Promise.resolve(iteration.value) //returns a promise with its then() and catch() methods filled
.then(x => iterate(genObject.next(x))) //calls recursive function on the next value to be iterated
.catch(x => iterate(genObject.throw(x))); //throws an error if a rejection is encountered
}
@roden0
roden0 / fetch.js
Created May 28, 2018 19:58
fetch API demo
var initObject = {
method: 'POST',
headers: new Headers(),
mode: 'cors',
body: "{}"
}
var request = new Request("https://jsonplaceholder.typicode.com/posts",initObject)
//first time using Request object
@roden0
roden0 / csvmaker.html
Created April 30, 2017 10:49
build CSV on Javascript and download it
<a download="somedata.csv"href="data:application/csv;charset=utf-8,Col1%2CCol2%2CCol3%0AVal1%2CVal2%2CVal3%0AVal11%2CVal22%2CVal33%0AVal111%2CVal222%2CVal333">Example</a>
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@roden0
roden0 / an_gen.js
Last active August 29, 2015 14:19
AlphaNumeric random generator.
Math.random().toString(36).substr(2, 8);
//one step beyond
btoa(Math.random().toString(36).substr(2, 8));