Skip to content

Instantly share code, notes, and snippets.

View nuintun's full-sized avatar
😊
I may be slow to respond.

nuintun nuintun

😊
I may be slow to respond.
View GitHub Profile
@nuintun
nuintun / compose.ts
Last active June 8, 2024 01:28
不会因未调用 next 而被打断的 compose 方法,可用于实现洋葱圈结构式的插件组合
/**
* @module compose
*/
interface CallStack {
index: number;
}
export interface Next {
(): Promise<void>;
@nuintun
nuintun / normalize.ts
Last active December 16, 2022 03:05
normalize
/**
* @function normalize
* @description Normalize the path.
* @param path The path to normalize.
*/
export function normalize(path: string): string {
if (path === '') return '.';
const parts = path.split(/[\\/]+/);
const { length } = parts;
@nuintun
nuintun / dfs.ts
Created March 2, 2022 04:42
DFS tree
/**
* @module DFSTree
*/
type Resolve<T> = (node: T) => T[] | void;
type IteratorValue<T> = [node: T, parent: T | undefined];
type Waiting<T> = [iterator: Iterator<T, undefined>, parent?: T];
@nuintun
nuintun / path.ts
Last active March 3, 2022 03:21
URL resolve
/**
* @module path
*/
/**
* @function isURL
* @description 判断路径是否为 URL
* @param path 需要判断的路径
*/
function isURL(path: string): boolean {
@nuintun
nuintun / useStateMachine.ts
Last active October 14, 2025 10:11
useStateMachine.ts 旧版实现
/**
* @module useStateMachine
* @see https://github.com/cassiozen/useStateMachine
*/
import { isFunction, isString } from '/js/utils';
import { Dispatch, useEffect, useMemo, useReducer } from 'react';
type ContextUpdate<C> = (context: C) => C;
@nuintun
nuintun / koa-compose.ts
Last active March 23, 2024 13:52
koa-compose TypeScript 官方实现优化版
/**
* @module compose
*/
export interface Next {
(): Promise<void>;
}
export interface Composed<C> {
(context: C, next?: Next): Promise<void>;
@nuintun
nuintun / compose.ts
Last active September 27, 2021 06:18
compose TypeScript 增强版
/**
* @module compose
*/
export type Next<C> = (context: C) => Promise<C>;
export type Middleware<C> = (context: C, next: Next<C>) => Promise<C> | C;
export type ComposedMiddleware<C> = (context: C, next?: Next<C>) => Promise<C>;
@nuintun
nuintun / koa-compose.ts
Last active May 9, 2023 02:31
koa-compose TypeScript 实现
/**
* @module compose
*/
export interface Next {
(): Promise<void>;
}
export interface Composed<C> {
(context: C, next?: Next): Promise<void>;
@nuintun
nuintun / redux-compose.js
Last active September 16, 2021 02:34
redux-compose 实现
/**
* @function compose
* @description compose redux middlewares
* @param {function[]} funcs
* @returns {function}
*/
function compose(middlewares) {
return middlewares.reduce((compose, middleware) => {
return (...args) => compose(middleware(...args));
});
@nuintun
nuintun / koa-compose.js
Last active September 27, 2021 06:21
koa-compose 简化版实现
/**
* @function compose
* @description compose koa middlewares
* @param {function[]} middlewares
* @returns {function}
*/
function compose(middlewares) {
const done = async () => {};
const compose = middlewares.reduce((compose, middleware) => {