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 / useStateMachine.ts
Last active June 8, 2024 01:44
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) => {
@nuintun
nuintun / Dockerfile
Last active June 8, 2021 09:33
Nextjs Docker 应用
# 配置运行环境
FROM node:alpine AS deps
# 配置工作目录
WORKDIR /wwwroot
# 切换国内软件源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# 安装编译环境
RUN apk update && apk upgrade -f && apk add --no-cache g++ make python3
@nuintun
nuintun / Dockerfile
Last active June 8, 2021 09:33
Nextjs Docker 环境
# 配置运行环境
FROM node:alpine
# 配置工作目录
WORKDIR /wwwroot
# 配置 Node 运行模式
ENV NODE_ENV production
# 禁用 Next 数据遥测
ENV NEXT_TELEMETRY_DISABLED 1
@nuintun
nuintun / hashCode.ts
Last active March 11, 2021 02:21
Java 中字符串 hashCode 方法的 JavaScript 实现
/**
* @function hashCode
* @param {string} string
* @returns {number}
*/
function hashCode(string: string): number {
let hash: number = 0;
const { length }: string = string;
@nuintun
nuintun / hex-rgb.js
Created July 2, 2020 08:05
hex 和 rgb 颜色转换
function rgb2hex(red, green, blue) {
const rgb = (red << 16) | (green << 8) | blue;
return `#${rgb.toString(16).padStart(6, '0')}`;
}
function hex2rgb(hex) {
return [(hex >> 16) & 0xff, (hex >> 8) & 0xff, hex & 0xff];
}