- Documentation
- Packages
- intlayer
- getEnumerationContent
Documentation: getEnumerationContent Function in intlayer
Description:
The getEnumerationContent function retrieves content corresponding to a specific quantity based on predefined conditions in an enumeration object. The conditions are defined as keys, and their priority is determined by their order in the object.
Parameters:
enumerationContent: QuantityContent<Content>
- Description: An object where keys represent conditions (e.g., <=, <, >=, =) and values represent the corresponding content. The order of keys defines their matching priority.
- Type: QuantityContent<Content>
- Content can be any type.
quantity: number
- Description: The numeric value used to match against the conditions in enumerationContent.
- Type: number
Returns:
- Type: Content
- Description: The content corresponding to the first matching condition in the enumerationContent. If no match is found, it defaults to handling based on the implementation (e.g., error or fallback content).
Example Usage:
Basic Usage:
typescript
1import { getEnumerationContent } from "@intlayer/config/client";
2
3const content = getEnumerationContent(
4 {
5 "<=-2.3": "You have less than -2.3",
6 "<1": "You have less than one",
7 "2": "You have two",
8 ">=3": "You have three or more",
9 },
10 2
11);
12
13console.log(content); // Output: "You have two"
Priority of Conditions:
typescript
1const content = getEnumerationContent(
2 {
3 "<4": "You have less than four",
4 "2": "You have two",
5 },
6 2
7);
8
9console.log(content); // Output: "You have less than four"
Edge Cases:
No Matching Condition:
- If no condition matches the provided quantity, the function will either return undefined or handle the default/fallback scenario explicitly.
Ambiguous Conditions:
- If conditions overlap, the first matching condition (based on object order) takes precedence.
Invalid Keys:
- The function assumes that all keys in enumerationContent are valid and parsable as conditions. Invalid or improperly formatted keys may lead to unexpected behavior.
TypeScript Enforcement:
- The function ensures that the Content type is consistent across all keys, allowing for type safety in the retrieved content.
Notes:
- The findMatchingCondition utility is used to determine the appropriate condition based on the given quantity.
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 documentation