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 / rollup-import-meta-glob.ts
Last active April 7, 2024 01:15
rollup-import-meta-glob.ts
/**
* @module import-meta-glob
*/
import path from 'path';
import glob from 'fast-glob';
import { Plugin } from 'rollup';
import MagicString from 'magic-string';
import { stripLiteral } from 'strip-literal';
import acorn, { parseExpressionAt } from 'acorn';
@nuintun
nuintun / getFiles.ts
Created April 6, 2024 13:09
获取指定目录及其子目录下的所有文件,符号链接除外。
/**
* @module getFiles
*/
import { Dirent } from 'fs';
import { join, resolve } from 'path';
import { readdir } from 'fs/promises';
type Waiting = [
// Current dirname
@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 March 23, 2024 13:50
不会因未调用 next 而被打断的 compose 方法,可用于实现洋葱圈结构式的插件组合
/**
* @module compose
*/
interface CallStack {
index: number;
}
export interface Next {
(): Promise<void>;
@nuintun
nuintun / 用JS计算字符所占字节数.markdown
Last active March 18, 2024 03:31
用JS计算字符所占字节数
/**
 * 计算字符串所占的内存字节数,默认使用UTF-8的编码方式计算,也可制定为UTF-16
 * UTF-8 是一种可变长度的 Unicode 编码格式,使用一至四个字节为每个字符编码
 *
 * 000000 - 00007F(128个代码)      0zzzzzzz(00-7F)                             一个字节
 * 000080 - 0007FF(1920个代码)     110yyyyy(C0-DF) 10zzzzzz(80-BF)             两个字节
 * 000800 - 00D7FF
 * 00E000 - 00FFFF(61440个代码)    1110xxxx(E0-EF) 10yyyyyy 10zzzzzz           三个字节
 * 010000 - 10FFFF(1048576个代码)  11110www(F0-F7) 10xxxxxx 10yyyyyy 10zzzzzz  四个字节
@nuintun
nuintun / BitMatrix.ts
Last active March 8, 2024 06:37
BitMatrix
/**
* @module BitMatrix
*/
function toUint32(uint32: number): number {
// 防止溢出 0-0xffffffff
return uint32 >>> 0;
}
export class BitMatrix {
@nuintun
nuintun / Integer.ts
Created March 8, 2024 06:35
快速整数转换算法
// 使用位运算将数字转换为8位有符号整数
function toInt8(value: number): number {
return (value << 24) >> 24;
}
// 使用位运算将数字转换为8位无符号整数
function toUint8(value: number): number {
return value & 0xff;
}
@nuintun
nuintun / sjis.ts
Last active June 12, 2023 07:21
sjis encode
// prettier-ignore
// @see https://github.com/soldair/node-qrcode/blob/master/helper/to-sjis.js
// @see https://github.com/soldair/node-qrcode/pull/319
// @see http://ash.jp/code/unitbl21.htm
// @see https://seiai.ed.jp/sys/text/java/shiftjis_table.html
const SJIS_TABLE: [offset: number, sjis: string][] = [
[0x8140, ' 、。,.・:;?!゛゜´`¨^ ̄_ヽヾゝゞ〃仝々〆〇ー―‐/\~∥|…‥‘’“”()〔〕[]{}〈〉《》「」『』【】+-±×'],
[0x8180, '÷=≠<>≦≧∞∴♂♀°′″℃¥$¢£%#&*@§☆★○●◎◇◆□■△▲▽▼※〒→←↑↓〓'],
[0x81b8, '∈∋⊆⊇⊂⊃∪∩'],
[0x81c8, '∧∨¬⇒⇔∀∃'],
@nuintun
nuintun / BitArray.ts
Last active June 5, 2023 06:46
BitArray
/**
* @module BitArray
*/
const LOAD_FACTOR = 0.75;
function toUInt32(uint32: number): number {
// 防止溢出 0-0xffffffff
return uint32 >>> 0;
}
@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>;