Skip to content

Instantly share code, notes, and snippets.

View leihuang23's full-sized avatar
🥳

Lei Huang leihuang23

🥳
  • 09:45 (UTC +01:00)
View GitHub Profile
@leihuang23
leihuang23 / isCyclic.js
Created February 11, 2023 09:38
Detect cyclic object reference in JavaScript.
export default function isCyclic(object, seen = new WeakSet().add(object)) {
const props = getProps(object)
let len = props.length
while (len--) {
const value = object[props[len]]
if (value instanceof Object) {
if (seen.has(value)) {
return true
}
@leihuang23
leihuang23 / memoPromise.js
Created February 11, 2023 09:32
Cache promise result with support for expiring
/**
* @param asyncFn - A promise-returning function. e.g. an HTTP call.
* @param expiresIn - The number of milliseconds for the result from calling `asyncFn` to expire.
* @returns A memoized function that will always resolve to the first result of calling `asyncFn`,
* as long as the result has not expired.
*/
export function memoPromise(asyncFn, expiresIn) {
const emptyId = Symbol("empty")
const memo = {}
@leihuang23
leihuang23 / detect-if-user-is-from-china.js
Last active May 30, 2022 11:25
Detect if the user is from China or other countries where Google is blocked
// This code is mostly useless. I put it here for
// the purpose of entertainment...
function detectIfUserIsFromChina() {
const img = new Image()
img.setAttribute("src", `https://www.google.com/favicon.ico?t=${Date.now()}`)
img.setAttribute("style", "width:0;height:0;visibility:hidden;")
document.body.appendChild(img)
return new Promise(res => {
img.onerror = () => res(true)
@leihuang23
leihuang23 / task-batcher.js
Last active August 18, 2020 07:22
Tail call optimization
let timeout = null;
const queue = new Set();
function process() {
for (const task of queue) {
task();
}
queue.clear();
timeout = null;
}
@leihuang23
leihuang23 / cps_demo.js
Created July 31, 2020 10:20
Parsing and processing DOM nodes in a non-blocking way using CPS technique.
function traverse(node, processTask, continuation) {
processTask(node, function handleChildren() {
const children = node.childNodes;
function handleOne(i, len, continuation) {
if (i < len) {
traverse(children[i], processTask, function handleNext() {
handleOne(i + 1, len, continuation);
});
} else {
@leihuang23
leihuang23 / trie.js
Created March 29, 2020 23:07
Trie implemented in JavaScript
class TrieNode {
constructor(char) {
this.char = char;
this.validWord = false;
this.parent = null;
this.children = [];
}
}
class Trie {
@leihuang23
leihuang23 / go.js
Created April 4, 2019 03:47 — forked from Gozala/go.js
Go routines for JS
// Utility function for detecting generators.
let isGenerator = x => {
return Function.isGenerator &&
Function.isGenerator.call(x)
}
// Data type represents channel into which values
// can be `put`, or `received` from. Channel is
// very much like queue where reads and writes are
// synchronized via continuation passing.
@leihuang23
leihuang23 / sequence.js
Created March 28, 2019 12:32
A sequence library implemented with Crockford functions
function toList(next, reversed) {
const arr = [];
let value = next();
while (value !== undefined) {
if (reversed) {
arr.unshift(value);
value = next();
} else {
arr.push(value);
value = next();
@leihuang23
leihuang23 / lens.js
Created January 18, 2019 00:08
Lens implementation in JavaScript
const curry = fn => (...args) =>
args.length >= fn.length ? fn(...args) : curry(fn.bind(undefined, ...args))
const always = a => b => a
const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args)
const getFunctor = x =>
Object.freeze({
value: x,
@leihuang23
leihuang23 / easing.js
Created January 15, 2019 02:48 — forked from gre/easing.js
Simple Easing Functions in Javascript - see https://github.com/gre/bezier-easing
/*
* Easing Functions - inspired from http://gizma.com/easing/
* only considering the t value for the range [0, 1] => [0, 1]
*/
EasingFunctions = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity