npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-async-logic

v1.0.2

Published

异步逻辑执行

Readme

simple-async-logic

轻量的异步逻辑组合执行库。通过声明式的配置结构(AND / OR / NOT)将异步判断条件组合为可执行的逻辑表达式。

特性

  • 声明式逻辑配置,支持 andornot 三种逻辑运算
  • 支持任意层级的嵌套组合
  • 完整的 TypeScript 泛型支持
  • 零运行时依赖
  • 支持 ESM 与 CommonJS

安装

npm i simple-async-logic

快速开始

import asyncLogic from 'simple-async-logic';

// 定义一个异步判断函数
const checkPermission: (role: number) => Promise<boolean> = (role) =>
  new Promise((resolve) => {
    setTimeout(() => resolve(role === 1 || role === 2), 200);
  });

// 创建逻辑执行器
const judge = asyncLogic(checkPermission);

// 执行逻辑判断
await judge({ value: 1 });                            // true
await judge({ and: [{ value: 1 }, { value: 2 }] });  // true
await judge({ or: [{ value: 1 }, { value: 3 }] });   // true
await judge({ not: { value: 1 } });                   // false

逻辑配置类型

interface LogicConfig<T> {
  and?: LogicConfig<T>[]   // 所有子条件都为 true 时结果为 true
  or?: LogicConfig<T>[]    // 任一子条件为 true 时结果为 true
  not?: LogicConfig<T>     // 对子条件取反
  value?: T                // 叶子节点,传入原始值交由判断函数处理
}

配置对象应只包含一个键(andornotvalue)。若包含多个键或无法识别的键,执行结果为 false

API

asyncLogic<T>(callback)

创建一个逻辑执行器。

参数:

| 参数 | 类型 | 说明 | |---|---|---| | callback | (arg: T) => Promise<boolean> | 接收原始值、返回异步布尔结果的判断函数 |

返回:

(config: LogicConfig<T>) => Promise<boolean> — 传入逻辑配置,返回最终判断结果。

逻辑运算规则

| 运算 | 空数组行为 | 说明 | |---|---|---| | and | 返回 true | 所有子条件都通过 | | or | 返回 true | 存在任一通过的子条件 | | not | — | 对子条件结果取反 |

嵌套组合示例

await judge({
  and: [
    { value: 1 },
    { value: 2 },
    { or: [{ value: 1 }, { value: 4 }] },
    { not: { value: 4 } },
    { not: { and: [{ value: 4 }, { value: 1 }] } },
  ],
});
// → true

License

MIT