Skip to content

Instantly share code, notes, and snippets.

export default function createSubset(array) {
class Node {
constructor(level, antiparticle = false) {
this.level = level;
this.v = array[level];
this.l = array[level + 1] && new Node(level + 1);
this.r = array[level + 1] && new Node(level + 1, true);
this.antiparticle = antiparticle;
this.visited = false;
}
const createDoublyLinkedList = arr => {
class Node {
constructor(v) {
this.v = v;
this.l = null;
this.r = null;
}
static createNode(v) {
return new Node(v);
}
@sungchuni
sungchuni / Defer.js
Last active January 8, 2023 14:02
use deferreds
class Defer {
constructor() {
this.promise = new Promise((resolve, reject) =>
Object.assign(this, {resolve, reject})
);
}
}
@sungchuni
sungchuni / httpCancellator.js
Last active January 21, 2021 04:03
http cancellator wrapper, for Axios instance and Fetch.fetch
function httpCancellatorForAxios(fn, axiosInstance) {
const configArgIndices = [
["request", 0],
["get", 1],
["delete", 1],
["head", 1],
["options", 1],
["post", 2],
["put", 2],
["patch", 2],
"use strict";
/**
* directive @paginate on FIELD_DEFINITION
* """
* abstract type Page {
* totalCount: Int!
* edges: [Edge!]!
* pageInfo: PageInfo!
* }
@sungchuni
sungchuni / fisherYatesShuffle.js
Last active December 22, 2020 07:58
simple implementation fisher-yates shuffle
function fisherYatesShuffle(array) {
const {length} = array;
array.forEach((value, index) => {
const rand = (index + Math.random() * (length - index)) | 0;
rand !== index && ([array[index], array[rand]] = [array[rand], value]);
});
return array;
}
@sungchuni
sungchuni / throttle.js
Last active December 7, 2020 14:30
simple implementation throttle
function throttle(fn, wait) {
let timestamp = -wait || 0;
return function throttled(...args) {
const now = performance.now();
now - timestamp > wait && (timestamp = now) && fn.apply(this, args);
};
}
@sungchuni
sungchuni / debounce.js
Last active December 8, 2020 09:22
simple implementation debounce
function debounce(fn, wait) {
let cancelId = null
const hasWait = Number.isSafeInteger(wait)
const [timer, cancel] =
hasWait
? [setTimeout, clearTimeout]
: [requestAnimationFrame, cancelAnimationFrame]
return function debounced(...args) {
cancel(cancelId)
cancelId = timer(fn.bind(this, ...args), wait)
@sungchuni
sungchuni / computeAverageRandomSort.js
Created November 11, 2020 14:55
무작위 정렬을 수행할 것이라 예상하는 방식이 얼만큼 무효한지를 검증하기 위한 함수
function computeAverageRandomSort(length = 16, iteration = 1024) {
function generateRandomArray() {
return Array.from({length})
.map((_, i) => i)
.sort(() => Math.random() - .5);
}
const arrays = Array.from({length: iteration}).map(generateRandomArray);
return Array.from({length}).map(
(_, i) =>
arrays.map(array => array[i]).reduce((sum, value) => sum + value) / iteration
function getSamples(length) {
const array = Array.from(this || []);
const sampleLength = Math.min(length, array.length);
const result = [];
while (result.length < sampleLength) {
const draw = Math.random() * array.length;
const sample = array.splice(draw, 1).shift();
result.push(sample);
}
return result;