Creation:2024-08-11Last update:2025-06-29
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
文档:intlayer 中的 getEnumeration 函数
描述
getEnumeration 函数根据枚举对象中预定义的条件,检索与特定数量对应的内容。条件以键的形式定义,其优先级由对象中键的顺序决定。
参数
enumerationContent: QuantityContent<Content>- 描述:一个对象,键表示条件(例如
<=、<、>=、=),值表示对应的内容。键的顺序定义了匹配的优先级。 - 类型:
QuantityContent<Content>Content可以是任意类型。
- 描述:一个对象,键表示条件(例如
quantity: number- 描述:用于与
enumerationContent中条件匹配的数值。 - 类型:
number
- 描述:用于与
返回值
- 类型:
Content - 描述:对应于
enumerationContent中第一个匹配条件的内容。如果没有找到匹配项,则根据实现进行默认处理(例如错误处理或回退内容)。
示例用法
基本用法
typescript
复制代码
复制代码到剪贴板
import { getEnumeration } from "intlayer";
const content = getEnumeration(
{
"<=-2.3": "你拥有的数量少于 -2.3",
"<1": "你拥有的数量少于 1",
"2": "你拥有两个",
">=3": "你拥有三个或更多",
},
2
);
console.log(content); // 输出: "你拥有两个"条件优先级
typescript
复制代码
复制代码到剪贴板
import { getEnumeration } from "intlayer";
const content = getEnumeration(
{
"<4": "你拥有的数量少于 4",
"2": "你拥有两个",
},
2
);
console.log(content); // 输出: "你拥有的数量少于 4"边界情况
无匹配条件:
- 如果没有条件匹配所提供的数量,函数将返回
undefined或者显式处理默认/回退场景。
- 如果没有条件匹配所提供的数量,函数将返回
条件重叠:
- 如果条件重叠,优先使用第一个匹配的条件(基于对象的顺序)。
无效键:
- 该函数假设
enumerationContent中的所有键都是有效且可解析为条件的。无效或格式不正确的键可能导致意外行为。
- 该函数假设
TypeScript 强制:
- 该函数确保所有键的
Content类型一致,从而保证检索内容时的类型安全。
- 该函数确保所有键的
备注
- 使用
findMatchingCondition工具根据给定的数量确定适当的条件。