Skip to content

Instantly share code, notes, and snippets.

View JayKan's full-sized avatar
🎯
🚀

Jay Kan JayKan

🎯
🚀
View GitHub Profile
@JayKan
JayKan / functional-utils.js
Created April 9, 2019 04:31 — forked from bendc/functional-utils.js
A set of pure ES2015 functions aimed to make functional JavaScript more idiomatic.
// array utils
// =================================================================================================
const combine = (...arrays) => [].concat(...arrays);
const compact = arr => arr.filter(Boolean);
const contains = (() => Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
@JayKan
JayKan / index.html
Created March 13, 2019 08:29
Popups without JavaScript :)
<!DOCTYPE html>
<html>
<head>
<title>Popup without Javascript</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<h1>Popup without javascript demo></h1>
<p><a href="#my_popup">Open popup</a></p>
<p>HTML visible body ends here</p>
@JayKan
JayKan / readme.md
Created January 17, 2019 21:38 — forked from brauliodiez/readme.md
lodash/fp set and flow

lodash/fp - set / flow

In this gist we are going to learn about basic goodies that we get from the library lodash/fp (fp stands for functional programming, great for ensuring immutability).We'll learn just by doing (step by step guided sample + codepens).

We'll cover lodash set and flow functions

Steps

  • We'll use codepen as our playground, navigate to this page:

Recover the Sequence

Merge sort is one of the classic sorting algorithms. It divides the input array into two halves, recursively sorts each half, then merges the two sorted halves.

In this problem merge sort is used to sort an array of integers in ascending order. The exact behavior is given by the following pseudo-code:

function merge_sort(arr):
    n = arr.length()
    if n <= 1:
@JayKan
JayKan / simple-promise.js
Created January 4, 2019 11:51
A simple promise re-implementation in JavaScript
// NOTE: https://medium.com/gitconnected/understand-javascript-promises-by-building-a-promise-from-scratch-84c0fd855720
class SimplePromise {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
@JayKan
JayKan / promisfy.js
Created January 4, 2019 11:48
A tiny promisfy re-implmentation
// Keep in mind this could be simply retrieved in nodejs by
// const { promisify } = require('util');
function promisify(fn) {
return function(...args) {
return new Promise((res, rej) => {
fn(...args, function() {
res(arguments)
})
})
@JayKan
JayKan / observer.js
Created January 4, 2019 11:46
A simple observer implementation in JavaScript
function createObserver(listeners = {}) {
function addListener(eventType, cb) {
if (listeners.hasOwnProperty(eventType)) {
listeners[eventType].push(cb);
} else {
listeners[eventType] = [cb];
}
return function removeListener() {
const index = listeners[eventType].indexOf(cb);
@JayKan
JayKan / music.md
Created December 26, 2018 01:35 — forked from staltz/music.md
coding music

Not for everyone. Each programmer has their own appreciation of what is good coding music.

For when I need to think deep, debug something, or design

(From most influential to least)

// NOTE: you will NOT write code like this when using suspense
// instead, you'll use react-cache (not yet officially published)
// it'll handle things like pre-loading, handling rapid re-renders, etc.
const cache = {}
function FetchPokemon({pokemonName}) {
const pokemon = cache[pokemonName]
if (!pokemon) {
const promise = fetchPokemon(pokemonName).then(
@JayKan
JayKan / Compose.js
Last active December 18, 2018 20:51
[experimental] Compose render props
import { cloneElement } from 'react';
import PropTypes from 'prop-types';
function Compose(props) {
return function renderRecursive(props.children, props.components, results = []) {
if (!remaining[0]) {
render(results);
}
function nextRender(value) {