wssf-kage-js
v1.0.2
Published
TypeScript 函数式工具:柯里化、组合、高阶拦截、纯函数、中间件等 24 类模式,面向前端与 AI 流水线
Maintainers
Readme
Kage.js (wssf-kage-js)
面向 前端与 AI 业务 的 TypeScript 函数式工具集合:柯里化、组合、高阶拦截、纯函数、惰性/记忆化、中间件、策略、Option 等 24 类常见函数模式,便于组合提示词流水线、鉴权与降级、数据变换等逻辑。
- 运行时零依赖(
dependencies为空) - 发布物仅含
dist(CJS + ESM +.d.ts),支持按需import
npm 页面展示的是本 README。更完整的说明、分节示例见仓库内 Docusaurus 文档(
pnpm run docs:dev本地预览)。
安装
npm install wssf-kage-js
# 或
pnpm add wssf-kage-js快速开始
柯里化与普通工具(curry、pipe、debounce …)
import { curry, pipe, debounce } from 'wssf-kage-js';
const add = (a: number, b: number, c: number) => a + b + c;
curry(add)(1)(2)(3); // => 6
pipe<string>((s) => s.trim(), (s) => s.toUpperCase())(' hi '); // => 'HI'
const onResize = debounce(200)(() => console.log('resize'));组合与 Prompt / 意图流水线(compose.ts)
import { sanitizePrompt, routeIntent, withFallback } from 'wssf-kage-js';
sanitizePrompt(' <b>x</b> ');
routeIntent({ text: '美元汇率' });
const safe = withFallback('降级文案')(async () => {
throw new Error('fail');
});
await safe();纯函数与闭包
import { normalizeText, sumNumbers, createCounter } from 'wssf-kage-js';
normalizeText(' Hello ');
sumNumbers([1, 2, 3]);
const c = createCounter(0);
c.increment();异步与生成器
import { asyncMap, retryAsync, createRange, streamChunks } from 'wssf-kage-js';
await asyncMap([1, 2], async (n) => n * 2);
await retryAsync(async () => 'ok', 2);
[...createRange(1, 3)];
for await (const x of streamChunks(['a', 'b'], 0)) {
console.log(x);
}导出结构(与源码目录对应)
| 模块 | 代表能力 / 符号 |
|------|------------------|
| currying | curry、compose、pipe、get/set、map/filter/reduce、debounce、throttle、logger、request、validate … |
| compose | composePipe、sanitizePrompt、safetyGate、routeIntent、tokenManager、buildPrompt、dataPipeline、processSearchList … |
| higherOrder | withPermission、withPerformance、withTokenAudit、withFallback、withSanitizer、withLock、withAuditTrail、withUIFormat |
| pureFunction | sumNumbers、normalizeText、calculateWeightedScore |
| closureFunction | createCounter、createAccumulator |
| partialFunction | partial、partialRight |
| decoratorFunction | withLogging、withRetry |
| middlewareFunction | runMiddleware、createLogMiddleware |
| strategyFunction | createStrategyRunner、financeStrategy、codingStrategy、defaultStrategy |
| guardFunction | assertDefined、assertNonEmptyArray、isRecord |
| predicateFunction | isNonEmptyString、isPositiveNumber、andPredicate、orPredicate |
| transformerFunction | toUserViewModel、transformCollection |
| reducerFunction | counterReducer、reduceItems |
| recursiveFunction / tailRecursiveFunction | factorialRecursive、flattenTree、factorialTailRecursive、sumTailRecursive |
| lazyFunction | deferValue、createOnceResolver、mapLazyValue |
| memoizedFunction | memoizeUnary、clearMemoizedCache |
| callbackFunction | mapWithCallback、forEachWithCallback、createDelayedCallback |
| asyncFunction | delay、asyncMap、retryAsync |
| generatorFunction / asyncGeneratorFunction | createRange、iterateWords、streamChunks、collectAsyncGenerator |
| factoryFunction | createFormatter、createThresholdChecker |
| pointFreeFunction | mapValues、filterValues、joinWith、pointFreeUserNames |
| monadFunction | some、none、mapOption、flatMapOption、getOptionOrElse |
完整分节说明与「每函数一例」见仓库 docs/function-types/。
本地开发与文档
pnpm install
pnpm run build # 产出 dist/
pnpm test # 测试(若已配置)
pnpm run docs:dev # Docusaurus 文档站
pnpm run docs:site:buildTypeDoc 默认输出目录为 typedoc-html/(见 typedoc.json),与文档站 Markdown 源码分离。
许可证
MIT
更新日志
1.0.2
- 修正 npm README:与当前
src导出一致,移除不存在的 lodash 式 API 描述。
1.0.1
- 发版指向 npm 官方
registry;安装仍可使用国内镜像(见仓库.npmrc说明)。
1.0.0
- 首次公开发布。
