@flipbyte/when-condition

中文文档
0.6.0发布于 2019/08/02MITESM + CJS零依赖近 30 天下载 4825

Check conditional statements and return true/false

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. Example
  4. Single rule
  5. Rules with multiple comparisons
  6. Complex rules
  7. Evaluating rules
  8. Rules using callback
  9. Logical rules
  10. Comparison rules
  11. License

when-condition

Developed by Flipbyte

Build Status npm package Coverage Status license Codacy Badge

Check conditional statements and return true/false

Installation

npm i @flipbyte/when-condition

Usage

Define your conditionals using an array with logical rule ('and' or 'or') as the first element followed by an array of comparison conditions.

General representation of a logical rule is as follows:

['{your logical rule}', [{your comparison rule 1}], [{your comparison rule 2}], ...]

Comparison rule is an array with 3 elements:

  • The comparison rule.
  • The key of the object whose value needs to be compared with a condition.
  • The comparison value.

General representation of a comparison rule is as follows:

['{comparison rule}', '{key}', '{comparison value}']

Example

Consider an object as follows:

let data = {
    a: 1,
    b: 2,
    c: {
        d: 'string',
        e: [{
            f: 1,
            g: 2
        }]
    }
}

Single rule

To simply check one rule (say) data.a = 1

let rule = ['is', 'a', 1]

or

Check whether data.c.e[0].f = 1

let rule = ['is', 'c.e[0]`.f', 1]

Rules with multiple comparisons

Check whether both data.a = 1 and data.b = 2

let rule = ['and', ['is', 'a', 1], ['is', 'b', 2]]

Check whether either data.a = 1 or data.b = 2

let rule = ['or', ['is', 'a', 1], ['is', 'b', 2]]

Complex rules

Check whether (data.a = 1 and data.b = 2) or (data.b = 2 and data.c.d != 'string')

let rule = ['or', ['and', ['is', 'a', 1], ['is', 'b', 2]], ['and', ['is', 'b', 2], ['is', 'c.d', 'string']]]

Evaluating rules

import when from 'when-condition'

when(rule, data) // => returns true or false

Rules using callback

You can also pass a callback function to check the data. Which will be resolved to a promise. It can either be a plain function or a promise

when(function(data) {
    return data.a !== data.b
}, data).then((result) => {
    //...handle your result
})

Logical rules

There are 2 types of logical rules:

  • and - checks whether all the conditions evaluate to true.
  • or - checks whether atleast one condition evaluates to true.
  • not - returns the opposite of the evaluated comparison rule. This logical rule takes only one comparison rule.

Comparison rules

Following are the available comparison rules:

  • is - returns true if the object value strictly matches the comparison value.
  • isOfType - returns true if the object value matches the specified type (Ex: string, undefined, etc.).
  • anyOf - returns true if at least one of the comparison values matches the object key value. The comparison value needs to be an array.
  • allOf - returns true if all the object key value matches the comparison values. The comparison value needs to be an array.
  • gt - returns true if the object key value is greater than the comparison value
  • gte - returns true if the object key value is greater than or equal to the comparison value
  • lt - returns true if the object key value is lesser than the comparison value
  • lte - returns true if the object key value is lesser than or equal to the comparison value

License

The MIT License (MIT)

版本与发布记录

@flipbyte/when-condition 共发布 8 个版本,首次发布于 2019/02/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2019/08/027 年前
0.5.12019/04/257 年前
0.5.02019/04/257 年前
0.4.02019/04/247 年前
0.3.02019/04/237 年前
0.2.02019/02/217 年前

关于 @flipbyte/when-condition 的常见问题

@flipbyte/when-condition 是什么?

Check conditional statements and return true/false

如何安装 @flipbyte/when-condition?

在项目目录运行 npm install @flipbyte/when-condition,或使用 pnpm add / yarn add / bun add。

@flipbyte/when-condition 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flipbyte__when-condition 是否存在。

@flipbyte/when-condition 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flipbyte/when-condition 可以商用吗?

@flipbyte/when-condition 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flipbyte/when-condition 的下载量有多少?

根据 npm 官方下载统计,@flipbyte/when-condition 在 2026/10/15 – 2026/11/13 这 30 天内被下载 4,825 次,日均约 161 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flipbyte/when-condition 还在维护吗?

最新版本 0.6.0 发布于 2019/08/02(7 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。