Skip to content

Instantly share code, notes, and snippets.

View iam1maker's full-sized avatar
🎯
Focusing

1maker iam1maker

🎯
Focusing
View GitHub Profile
@iam1maker
iam1maker / downloadImages.ts
Last active May 29, 2024 14:56
Download pictures concurrently
import fetch from "node-fetch";
const fs = require("fs");
const path = require("path");
async function downloadImage(url: string): Promise<void> {
    // 实际下载并保存图片
    try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error(`HTTP error ${response.status}`);
@iam1maker
iam1maker / spyDecorator.ts
Last active September 12, 2024 10:26
Spy decorator
// 定义一个 spy 函数,用于监视传入函数的调用
function spy(func: (...args: any[]) => any) {
const calls: any[][] = []; // 存储函数调用的参数
function wrapper(...args: any[]) {
calls.push(args); // 将调用的参数存入 calls 数组
return func(...args); // 调用原始函数
}
wrapper.calls = calls; // 将 calls 数组附加到 wrapper 函数上
@iam1maker
iam1maker / delayDecorator.ts
Created September 12, 2024 10:25
Delay decorator
// 定义 delay 函数
function delay(func: Function, ms: number) {
return function(...args: any[]) {
setTimeout(() => {
func.apply(this, args); // 调用原始函数,并传递所有参数和上下文
}, ms);
};
}
// 定义一个示例函数 f
@iam1maker
iam1maker / debounceDecorator.ts
Created September 12, 2024 10:31
Debounce Decorator
function debounce(func: Function, ms: number) {
let timeout: NodeJS.Timeout;
return function(this: any, ...args: any[]) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args);

FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.


Effective Engineer - Notes

What's an Effective Engineer?

@iam1maker
iam1maker / ProductAdapter.tsx
Last active October 8, 2024 03:33
Adapter How to with React and Context API
import React, { createContext, useContext, useEffect, useState } from 'react';
// Adapter Context
const AdapterContext = createContext();
// Product Adapter
const createProductAdapter = (productService) => ({
getProductList: async () => {
const products = await productService.getProducts();
const formattedProducts = products.map(({ id, name, price }) => ({
@iam1maker
iam1maker / makeSafe.ts
Last active October 22, 2024 04:01
高阶函数
const makeSafe =
<TParams extends any[], TReturn>(func: (...args: TParams) => TReturn) =>
(
...args: TParams
):
| {
type: "success";
result: TReturn;
}
| {
@iam1maker
iam1maker / clean_code.md
Created May 9, 2025 10:27 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules