Schema.Condition
// @namespace Schema.Condition
import { Schema } from 'rcfm'
const Condition = Schema.Condition
Custom Types
@typedef {Object[]} ConditionStatement
@description - 条件语句
@example
[
// 这是一个条件分支,when 是条件分支关键字
// 条件分支是标准的 js object
{
// 自定义布尔表达式,返回一个布尔值
when: "<bool_expression>",
// 这是条件分支的结果,then 是分支结果关键字
// 条件分支的结果可以是任意数据类型
then: "<branch_result>"
},
// 这是另一个条件分支
{
// 条件分支的条件可串联为一个数组
when: [
"<bool_expression>",
// 串联需使用 and 或 or 关键字
// 连接后可使用 not 关键字
{and: {not: "<bool_expression>"}}
],
then: "<branch_result>"
},
// 这是默认分支
// 默认分支也是标准的 js object
// else 是其结果关键字
// 默认分支的结果也可以是任意数据类型
{
else: "<branch_result>"
}
]
@typedef {Object} ConditionCase
@description - 条件语句条件分支
@prop {*|*[]} when - 分支条件
@prop {*} then - 分支结果
@typedef {Object} ConditionFallback
@description - 条件语句默认分支
@prop {*} else - 分支结果
@typedef {Object} ConditionExtractedStatement
@description - 解构后的条件语句
@prop {Object[]} whens - 解构后的条件分支对象构成的数组
@prop {Object} else - 解构后的默认分支对象
@example
{
whens: [
{
rules: [
{logic: "", expression: "<bool_expression>"}
],
result: "<branch_result>"
},
{
rules: [
{logic: "", expression: "<bool_expression>"},
{logic: "and_not", expression: "<bool_expression>"}
],
result: "<branch_result>"
},
],
else: {
result: "<branch_result>"
}
}
@typedef {Object} ConditionExtractedCase
@description - 条件语句解构后条件分支
@prop {Object[]} rules - 条件分支解构后的布尔表达式数组
@prop {*} result - 条件分支结果
@typedef {Object} ConditionExtractedRules
@description - 条件分支解构后的布尔表达式数组
@prop {string} logic - 布尔表达式与或关键词,可能值为 "and"、"or" 、"and_not"、"or_not" 或 ""。
@prop {*} expression - 布尔表达式
@typedef {Object} ConditionExtractedFallback
@description - 条件语句解构后默认分支
@prop {*} result - 默认分支结果
Exception Types
@type {ExceptionInstance}
@description - 不是合规的实例
@prop {string} type = "CONDITION_INVALID_CONDITION"
@prop {string} [name = type]
@prop {Object} [data = {}]
@prop {string} [message]
Static Methods
.combine()
将解构后的条件语句重新拼装。
// Condition.combine(extracted)
// @param {ConditionExtractedStatement} extracted
// @returns {ConditionStatement}
// @example
// returns -
// [
// {
// when: "<bool_expression>",
// then: "<branch_result>"
// },
// {
// when: [
// "<bool_expression>",
// {and: {not: "<bool_expression>"}}
// ],
// then: "<branch_result>"
// },
// {
// else: "<branch_result>"
// }
// ]
Condition.combine({
whens: [
{
rules: [
{logic: "", expression: "<bool_expression>"}
],
result: "<branch_result>"
},
{
rules: [
{logic: "", expression: "<bool_expression>"},
{logic: "and_not", expression: "<bool_expression>"}
],
result: "<branch_result>"
},
],
else: {
result: "<branch_result>"
}
})
.extract()
解构条件语句。
// Condition.extract(statement)
// @param {ConditionStatement} statement
// @returns {ConditionExtractedStatement}
// @example
// returns -
// {
// whens: [
// {
// rules: [
// {logic: "", expression: "<bool_expression>"}
// ],
// result: "<branch_result>"
// },
// {
// rules: [
// {logic: "", expression: "<bool_expression>"},
// {logic: "and_not", expression: "<bool_expression>"}
// ],
// result: "<branch_result>"
// },
// ],
// else: {
// result: "<branch_result>"
// }
// }
Condition.extract([
{
when: "<bool_expression>",
then: "<branch_result>"
},
{
when: [
"<bool_expression>",
{and: {not: "<bool_expression>"}}
],
then: "<branch_result>"
},
{
else: "<branch_result>"
}
])
.isCondition()
判断入参是否是 ConditionStatement。
// Condition.isCondition(arg)
// @param {*} arg - 任意参数
// @returns {boolean} - 当入参是 ConditionStatement 时返回 true,否则返回 false
// @examples
Condition.isCondition([
{
when: "<bool_expression>",
then: "<branch_result>"
},
{
else: "<branch_result>"
}
]) // true
Condition.isCondition({}) // false
.isntCondition()
判断入参是否不是 ConditionStatement。
// Condition.isntCondition(arg)
// @param {*} arg - 任意参数
// @returns {boolean} - 当入参是 ConditionStatement 时返回 false,否则返回 true
// @examples
Condition.isntCondition([
{
when: "<bool_expression>",
then: "<branch_result>"
},
{
else: "<branch_result>"
}
]) // false
Condition.isntCondition({}) // true
.mustbeCondition()
当入参不是 ConditionStatement 时抛出异常。
// Condition.mustbeCondition(arg, message)
// @param {*} arg - 任意参数
// @param {string} [message] - 自定义异常对象消息
// @returns {undefined}
// @examples
Condition.mustbeCondition([
{
when: "<bool_expression>",
then: "<branch_result>"
},
{
else: "<branch_result>"
}
])
Condition.mustbeCondition({}) // Throws a "CONDITION_INVALID_CONDITION" exception.