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 / 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 / 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 / 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 / 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 / compose.ts
Last active March 23, 2024 13:50
不会因未调用 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 {