index.js

Source:
Version:
  • 1.0.0
Author:

简化复合状态对于代码复杂性的影响,增强代码可读性

Example

import ComplexState from 'complexState'

// 获得如下数据及其取值范围
const stateObj = {
  state1: true, // 类型{Boolean} 取值[true|false]
  state2: 0, // 类型{Number} 取值[0|1|2|3]
  state3: 'STATE_1' // 类型{String} 取值['STATE_1'|'STATE_2']
}

// 定义复合状态关系表
const source = [
  ['state1', 'state2', 'state3'],
  [  true  ,    0    , 'STATE_1', 'showLogoA', 'this is infoA'],
  [  true  ,    0    , 'STATE_2', 'showLogoB', 'this is infoB'],
  [  true  ,  [1,2]  , 'STATE_1', 'showLogoC', 'this is infoC']
]
// 复合关系表格式:
//      \ | ...子状态                        | token(复合状态的令牌)| value(复合状态的值)
// -----: | ------------------------------- | :----------------: | :----------------:
//    表头 | 'state1', 'state2', 'state3'    | ----------------- | ------------------
// 复合关系 | true    ,   0     , 'STATE_1'  | 'showLogoA'        | 'this is infoA'
// 复合关系 | true    ,   0     , 'STATE_2'  | 'showLogoB'        | 'this is infoB'
// 复合关系 | true    ,  [1,2]  , 'STATE_3'  | 'showLogoC'        | 'this is infoC'

// 实例化ComplexState对象
const demoResource = new ComplexState(source)

// 得到复合状态值(value)---返回复合状态值
demoResource.getState({state1: true, state2: 0, state3: 'STATE_1'}) // this is infoA
demoResource.getState(true, 0, 'STATE_1') // this is infoA
demoResource.getStateByMap(['state2', 'state3', 'state1'], 0, 'STATE_1', true) // this is infoA

const getResource1 = demoResource.getStateResource()
getResource1({state1: true, state2: 0, state3: 'STATE_1'}) // this is infoA
getResource1(true, 0, 'STATE_1') // this is infoA

const getResource2 = demoResource.getStateResource(['state2', 'state3', 'state1'])
getResource2({state1: true, state2: 0, state3: 'STATE_1'}) // this is infoA
getResource2(0, 'STATE_1', true) // this is infoA

// 验证复合状态值(value)---返回true或false
demoResource.isState('showLogoB', {state1: true, state2: 0, state3: 'STATE_2'}) // true
demoResource.isState('showLogoB', true, 0, 'STATE_2') // true
demoResource.isStateByMap(['state2', 'state3', 'state1'], 'showLogoB', 0, 'STATE_2', true) // true

const showLogoB_1 = demoResource.isStateResource('showLogoB')
showLogoB_1({state1: true, state2: 0, state3: 'STATE_2'}) // true
showLogoB_1(true, 0, 'STATE_2') // true

const showLogoB_2 = demoResource.isStateResource('showLogoB', ['state2', 'state3', 'state1'])
showLogoB_2({state1: true, state2: 0, state3: 'STATE_2'}) // true
showLogoB_2(0, 'STATE_2', true) // true

// 关于复合状态关系表的source[3][1] 为 [1,2]的问题
// 若复合状态关系表里存在数组,将被视为是逻辑:或,即[1,2]为,1或2
demoResource.getState({state1: true, state2: 0, state3: 'STATE_3'}) // undefined
demoResource.getState({state1: true, state2: 1, state3: 'STATE_3'}) // this is infoC
demoResource.getState({state1: true, state2: 2, state3: 'STATE_3'}) // this is infoC

demoResource.isState('showLogoC', {state1: true, state2: 0, state3: 'STATE_3'}) // false
demoResource.isState('showLogoC', {state1: true, state2: 1, state3: 'STATE_3'}) // true
demoResource.isState('showLogoC', {state1: true, state2: 2, state3: 'STATE_3'}) // true