createNode.js 1.7 KB
Newer Older
DarkForst's avatar
DarkForst committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
const checkSymbol = operator => {
  const symbol = ["=", "+", "-", "*", "/", ">", "<", '>=', '<=', "(", ")"];
  return symbol.includes(operator)
}

const checkKeycode = (hasShift, keyCode) => {
  const isEqual = !hasShift && keyCode === 187; // =
  const isPlus = hasShift && keyCode === 187; // +
  const isMinus = !hasShift && keyCode === 189 // -
  const isMultiply = hasShift && keyCode === 56 // *
  const isDivide= !hasShift && keyCode === 191 // 除
  const isLess = hasShift && keyCode === 188
  const isGreater = hasShift && keyCode === 190  // >
  const isLeftBracket = hasShift && keyCode === 57 // (
  const isRightBracket = hasShift && keyCode === 48 // )
  return {
    isSymbol: isEqual || isPlus || isMinus || isMultiply || isDivide || isLess || isGreater || isLeftBracket || isRightBracket,
    symbolValue: isEqual && '='
      || isPlus && '+'
      || isMinus && '-'
      || isMultiply && '*'
      || isDivide && '/'
      || isLess && '<'
      || isGreater && '>'
      || isLeftBracket && '('
      || isRightBracket && ')',
  }

  // 48 )
  // 57 (
}

const createNode = target => {
  const {type, operator, value, meterAttrName, attrId, meterName} = target
  /* 判断数字 */
  if (type === 'number') {
    return {
      type: 'number',
      value: value || null,
    }
  }
  /* 判断符号 */

  const isSymbol = checkSymbol(operator)
  if (isSymbol) {
    return {
      type: 'symbol',
      value: operator,
    }
  }
  // 属性
  if (type === 'properties_realtime') {
    return {
      type,
      value: attrId,
      meterAttrName,
      meterName: meterName || ''
    }
  }
  return {
    type: 'number',
    value: value || null,
  }

}

export {
  createNode,
  checkSymbol,
  checkKeycode
}