validation.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function toText(value) {
  2. return String(value === undefined || value === null ? '' : value).trim()
  3. }
  4. function stripHexPrefix(value) {
  5. return toText(value).replace(/^0x/i, '')
  6. }
  7. function normalizeHexText(value) {
  8. return toText(value)
  9. .replace(/0x/gi, '')
  10. .replace(/[\s,;:_-]/g, ' ')
  11. .replace(/\s+/g, ' ')
  12. .trim()
  13. .toUpperCase()
  14. }
  15. function compactHexText(value) {
  16. return toText(value)
  17. .replace(/0x/gi, '')
  18. .replace(/[\s,;:_-]/g, '')
  19. .toUpperCase()
  20. }
  21. function validateHexText(value, options = {}) {
  22. const withoutPrefix = toText(value).replace(/0x/gi, '')
  23. const compact = withoutPrefix.replace(/[\s,;:_-]/g, '')
  24. const emptyMessage = options.emptyMessage || '请输入十六进制数据'
  25. const invalidMessage = options.invalidMessage || '只支持十六进制字符'
  26. const oddLengthMessage = options.oddLengthMessage || '十六进制长度必须为偶数'
  27. if (!compact) return emptyMessage
  28. if (/[^0-9a-fA-F\s,;:_-]/.test(withoutPrefix)) return invalidMessage
  29. if (compact.length % 2 !== 0) return oddLengthMessage
  30. return ''
  31. }
  32. function parseHexBytes(value) {
  33. const compact = compactHexText(value)
  34. const bytes = []
  35. for (let index = 0; index < compact.length; index += 2) {
  36. bytes.push(parseInt(compact.slice(index, index + 2), 16) & 0xFF)
  37. }
  38. return bytes
  39. }
  40. function normalizeHexUnitText(value, fallback = '') {
  41. const text = stripHexPrefix(value).toUpperCase()
  42. return text || fallback
  43. }
  44. function normalizeHexWordText(value, fallback = '') {
  45. return normalizeHexUnitText(value, fallback)
  46. }
  47. function normalizeHexDwordText(value, fallback = '') {
  48. return normalizeHexUnitText(value, fallback)
  49. }
  50. function validateHexWordText(value, label) {
  51. const text = normalizeHexWordText(value)
  52. if (!text) return `${label}请输入十六进制`
  53. if (!/^[0-9A-F]+$/i.test(text)) return `${label}只支持十六进制`
  54. if (text.length > 4) return `${label}最多 4 位十六进制`
  55. return ''
  56. }
  57. function validateHexDwordText(value, label) {
  58. const text = normalizeHexDwordText(value)
  59. if (!text) return `${label}请输入十六进制`
  60. if (!/^[0-9A-F]+$/i.test(text)) return `${label}只支持十六进制`
  61. if (text.length > 8) return `${label}最多 8 位十六进制`
  62. return ''
  63. }
  64. function parseHexNumber(value, label = '数值', maxValue = 0xFFFF) {
  65. const text = normalizeHexWordText(value)
  66. if (!text || !/^[0-9A-F]+$/i.test(text)) {
  67. throw new Error(`${label}请输入十六进制数值`)
  68. }
  69. const parsedValue = parseInt(text, 16)
  70. if (parsedValue > maxValue) {
  71. throw new Error(`${label}超出范围`)
  72. }
  73. return parsedValue
  74. }
  75. function parseHexDword(value, label = '地址') {
  76. return parseHexNumber(value, label, 0xFFFFFFFF)
  77. }
  78. function parseHexByte(value, label = '从机地址') {
  79. const text = normalizeHexWordText(value)
  80. if (!/^[0-9A-F]{1,2}$/i.test(text)) {
  81. throw new Error(`${label}需为 00 - FF`)
  82. }
  83. return parseInt(text, 16)
  84. }
  85. function normalizeHexByte(value, fallback = '00') {
  86. const fallbackText = normalizeHexWordText(fallback, '00')
  87. const text = normalizeHexWordText(value)
  88. if (!/^[0-9A-F]{1,2}$/i.test(text)) {
  89. return parseInt(fallbackText || '0', 16).toString(16).toUpperCase().padStart(2, '0')
  90. }
  91. return parseInt(text, 16).toString(16).toUpperCase().padStart(2, '0')
  92. }
  93. module.exports = {
  94. compactHexText,
  95. normalizeHexDwordText,
  96. normalizeHexByte,
  97. normalizeHexText,
  98. normalizeHexWordText,
  99. parseHexByte,
  100. parseHexBytes,
  101. parseHexDword,
  102. parseHexNumber,
  103. stripHexPrefix,
  104. validateHexDwordText,
  105. validateHexText,
  106. validateHexWordText
  107. }