Skip to content

Instantly share code, notes, and snippets.

View nodash.js
// follow python range func.
function range(start, end = null, step = 1) {
if (end === null) return Array(start).fill().map((_, index) => index)
const result = []
for (let i = start; step > 0 ? i < end : i > end; i += step || 1) {
result.push(i)
}
return result
}
@vv13
vv13 / composeReactElements.ts
Created Dec 31, 2021
React Compose Components.
View composeReactElements.ts
import React from 'react';
const genContainer =
(name: string): React.FC =>
({ children }) =>
(
<div>
<h1>I'm {name}</h1>
<div style={{ paddingLeft: '10px' }}>{children}</div>
</div>
View queue.ts
class Node<T> {
value: T
next?: Node<T>
constructor(value: T) {
this.value = value
}
}
class Queue<T> {
private _size = 0
@vv13
vv13 / solution.js
Created Nov 30, 2021
[JS] Async Queue Problem
View solution.js
let queue = [];
let running = false;
function start(id) {
queue.push(id);
if (running) return;
next();
}
function next() {
running = true;
@vv13
vv13 / shallowClone.js
Created Nov 30, 2021
[JS] Shallow Clone
View shallowClone.js
function shallowClone(obj) {
if (typeof obj !== 'object') return obj;
const newObj = obj instanceof Array ? [] : {};
for (let key of Object.keys(obj)) {
newObj[key] = obj[key];
}
return newObj;
}
@vv13
vv13 / deepClone.js
Created Nov 30, 2021
[JS] deepClone
View deepClone.js
function deepCopySimple(obj) {
return JSON.parse(JSON.stringify(obj));
}
function deepClone(obj) {
if (typeof obj !== 'object') return obj;
const newObj = obj instanceof Array ? [] : {};
for (let key of Object.keys(obj)) {
if (typeof obj[key] !== 'object') {
@vv13
vv13 / eventBus.js
Created Nov 30, 2021
[JS] A Simple EventBus
View eventBus.js
class EventBus {
constructor() {
this.eventMap = {}
this.onceEventMap = {}
}
on(eventName, listener) {
if (!this.eventMap[eventName]) {
this.eventMap[eventName] = [listener]
} else {
@vv13
vv13 / storage.ts
Created Nov 30, 2021
[TS] localStorage & memoryCache storage instances
View storage.ts
abstract class CustomStorage {
abstract storage: any;
abstract getItem<T>(key: string): T | null;
abstract setItem(key: string, value: any): void;
abstract removeItem(key: string): void;
abstract clear(): void;
getItemAndDelete<T>(key: string): T | null {
@vv13
vv13 / curry.js
Created Nov 28, 2021
JavaScript 函数柯里化
View curry.js
function curry(fn) {
return function curried(...args) {
if (fn.length === args.length) {
return fn.apply(this, args);
}
return (...extraArgs) => {
return curried.apply(this, args.concat(extraArgs));
};
};
}
@vv13
vv13 / debounce.js
Created Nov 28, 2021
JavaScript 防抖函数
View debounce.js
function debounce(fn, delay) {
let timerId = null;
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => {
fn(args)
}, delay)
}
}