Skip to content

Instantly share code, notes, and snippets.

@vv13
vv13 / RemoveComments.js
Created February 16, 2024 15:50
Simple remove comments in project
const fs = require("fs");
const path = require("path");
const IgnoreFiles = ["node_modules", ".idea", ".vscode", "package-lock.json", "yarn.lock"];
class RemoveComments {
sourcePath = "";
targetPath = "";
constructor(sourcePath, targetPath) {
this.sourcePath = sourcePath;
@vv13
vv13 / __dirname.js
Created October 15, 2023 13:22
__dirname in ESModule
import { fileURLToPath } from "node:url"
export const __dirname = () => fileURLToPath(import.meta.url)
@vv13
vv13 / composeReactElements.ts
Created December 31, 2021 07:18
React Compose Components.
import React from 'react';
const genContainer =
(name: string): React.FC =>
({ children }) =>
(
<div>
<h1>I'm {name}</h1>
<div style={{ paddingLeft: '10px' }}>{children}</div>
</div>
@vv13
vv13 / queue.ts
Created November 30, 2021 05:23
[TS] Queue
class Node<T> {
value: T
next?: Node<T>
constructor(value: T) {
this.value = value
}
}
class Queue<T> {
private _size = 0
@vv13
vv13 / shallowClone.js
Created November 30, 2021 05:21
[JS] Shallow Clone
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 November 30, 2021 05:20
[JS] deepClone
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 November 30, 2021 05:16
[JS] A Simple EventBus
class EventBus {
constructor() {
this.eventMap = {}
this.onceEventMap = {}
}
on(eventName, listener) {
if (!this.eventMap[eventName]) {
this.eventMap[eventName] = [listener]
} else {
@vv13
vv13 / storage.ts
Created November 30, 2021 05:10
[TS] localStorage & memoryCache storage instances
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 November 28, 2021 05:38
JavaScript 函数柯里化
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 November 28, 2021 04:16
JavaScript 防抖函数
function debounce(fn, delay) {
let timerId = null;
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => {
fn(args)
}, delay)
}
}