Skip to content

Instantly share code, notes, and snippets.

View sairion's full-sized avatar
⚛️

Jaeho Lee (Jay) sairion

⚛️
View GitHub Profile
@sairion
sairion / look-and-say.js
Created October 20, 2016 05:18
Look and say sequence - JavaScript version
/*
look-and-say-sequence by Jaeho Lee,
public domain
1 => [[1, 1]]
11 => [[1, 2]]
21 => [[2, 1], [1, 1]]
1211
111221
*/
// Promise exception handling is actually handled by mulitple level - therfore you need to care about many things
new Promise((res, rej) => {
setTimeout(_ => {
// case 1) error outside of promise context
res('booom 2');
throw new Error('error 2'); // this won't get ignored like case 1 and have to be catched manually. Also this don't automatically goes to `onRejected` handler unlike case 1.
}, 3000);
// case 2)
// res('booom 1');
// throw new Error('error 1'); // This gets ignored because it is thrown after resolve function gets called.
function rgb2hsl(r, g, b){
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if(max == min){
h = 0; // achromatic
@sairion
sairion / non-hoisting-behavior-of-let.js
Created August 30, 2016 12:46
non-hoisting-behavior-of-let.js
// ES5 lte
try {
var a = {
c:1, b: (function b() { return this.c }).bind(a)
};
} catch(e) { console.warn(e); }
// ES2015
try {
let a = {
@sairion
sairion / Instagram shared data.js
Created August 28, 2016 10:35
Get Instagram shared data via fetching HTML (i.e. location data)
let __DEBUG_HTML = '';
function fetchLoc(locID){
return fetch(`https://www.instagram.com/explore/locations/${locID}/`)
.then(res => res.text())
.then(txt => {
__DEBUG_HTML = txt;
const doc = parseInstgramExplorePage(txt);
const sharedData = extractSharedData(doc);
const locData = extractLocationData(sharedData);
// This function can be impure in JS context...
function sum(arr) {
var z = 0;
for (var i=0;i<arr.length;i++) {
z += arr[i];
}
return z;
}
// sec 1: valueOf() impureness
import ReactDOM from 'react';
// render without using JSX
export function jsRender(Component, data={}, targetElement, callback=null) {
ReactDOM.render(<Component {...data} />, targetElement, callback)
}
// Render after specific element.
export function wrappedRenderCreator(
function pp(givenNth/*: int*/, curNth = 0/*: int*/, dir = 1 /* { 1 | -1 } */, result = 0/*: int*/) {
// console.log(arguments[1], arguments[2], arguments[3]);
'use strict';
if (givenNth === curNth) {
return result;
} else {
return pp(givenNth, curNth + 1, isTurnAround(curNth + 1) * dir, result + dir);
}
}
// %
function mod(num, by) {
var tmp_num = num;
while (tmp_num - by >= 0) {
tmp_num -= by
}
return tmp_num;
}
function fizzbuzzMod (limit) {
@sairion
sairion / DebounceUntilTimeout.js
Last active April 22, 2016 15:43
keyboard event type (i.e. keyword-based recommendation)
export function debounceUntilTimeout(fun, delay, context = null) {
var timer;
return function delayedFunctionWrapper(...args) {
clearTimeout(timer);
args.unshift(context);
timer = setTimeout(fun.bind.apply(fun, args), delay);
};
}