Home > @taiyuuki/utils > match_range
match_range() function
This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
对数字进行模式匹配
Signature:
typescript
declare function match_range<T>(n: number, pattern: Record<string, T | ((range: MathRange) => T)>): T | null;
declare function match_range<T>(n: number, pattern: Record<string, T | ((range: MathRange) => T)>): T | null;
Parameters
Parameter | Type | Description |
---|---|---|
n | number | 一个数字 |
pattern | Record<string, T | ((range: MathRange) => T)> | 模式匹配对象 |
Returns:
T | null
返回值取决于模式对象
Example
ts
const result = match_range(15, {
'[0, 10]': 0,
'(10, 20]': (range) => range.end - range.start,
'(20, 30]': 2,
_: 0, // _ 表示匹配任意其他范围的数字
}) // result = 10
const result = match_range(15, {
'[0, 10]': 0,
'(10, 20]': (range) => range.end - range.start,
'(20, 30]': 2,
_: 0, // _ 表示匹配任意其他范围的数字
}) // result = 10