utils.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { ComparisonOperator } from './types'
  2. import { VarType } from '@/app/components/workflow/types'
  3. import type { Branch } from '@/app/components/workflow/types'
  4. export const isEmptyRelatedOperator = (operator: ComparisonOperator) => {
  5. return [ComparisonOperator.empty, ComparisonOperator.notEmpty, ComparisonOperator.isNull, ComparisonOperator.isNotNull, ComparisonOperator.exists, ComparisonOperator.notExists].includes(operator)
  6. }
  7. const notTranslateKey = [
  8. ComparisonOperator.equal, ComparisonOperator.notEqual,
  9. ComparisonOperator.largerThan, ComparisonOperator.largerThanOrEqual,
  10. ComparisonOperator.lessThan, ComparisonOperator.lessThanOrEqual,
  11. ]
  12. export const isComparisonOperatorNeedTranslate = (operator?: ComparisonOperator) => {
  13. if (!operator)
  14. return false
  15. return !notTranslateKey.includes(operator)
  16. }
  17. export const getOperators = (type?: VarType, file?: { key: string }) => {
  18. const isFile = !!file
  19. if (isFile) {
  20. const { key } = file
  21. switch (key) {
  22. case 'name':
  23. return [
  24. ComparisonOperator.contains,
  25. ComparisonOperator.notContains,
  26. ComparisonOperator.startWith,
  27. ComparisonOperator.endWith,
  28. ComparisonOperator.is,
  29. ComparisonOperator.isNot,
  30. ComparisonOperator.empty,
  31. ComparisonOperator.notEmpty,
  32. ]
  33. case 'type':
  34. return [
  35. ComparisonOperator.in,
  36. ComparisonOperator.notIn,
  37. ]
  38. case 'size':
  39. return [
  40. ComparisonOperator.largerThan,
  41. ComparisonOperator.largerThanOrEqual,
  42. ComparisonOperator.lessThan,
  43. ComparisonOperator.lessThanOrEqual,
  44. ]
  45. case 'extension':
  46. return [
  47. ComparisonOperator.is,
  48. ComparisonOperator.isNot,
  49. ComparisonOperator.contains,
  50. ComparisonOperator.notContains,
  51. ]
  52. case 'mime_type':
  53. return [
  54. ComparisonOperator.contains,
  55. ComparisonOperator.notContains,
  56. ComparisonOperator.startWith,
  57. ComparisonOperator.endWith,
  58. ComparisonOperator.is,
  59. ComparisonOperator.isNot,
  60. ComparisonOperator.empty,
  61. ComparisonOperator.notEmpty,
  62. ]
  63. case 'transfer_method':
  64. return [
  65. ComparisonOperator.in,
  66. ComparisonOperator.notIn,
  67. ]
  68. case 'url':
  69. return [
  70. ComparisonOperator.contains,
  71. ComparisonOperator.notContains,
  72. ComparisonOperator.startWith,
  73. ComparisonOperator.endWith,
  74. ComparisonOperator.is,
  75. ComparisonOperator.isNot,
  76. ComparisonOperator.empty,
  77. ComparisonOperator.notEmpty,
  78. ]
  79. }
  80. return []
  81. }
  82. switch (type) {
  83. case VarType.string:
  84. return [
  85. ComparisonOperator.contains,
  86. ComparisonOperator.notContains,
  87. ComparisonOperator.startWith,
  88. ComparisonOperator.endWith,
  89. ComparisonOperator.is,
  90. ComparisonOperator.isNot,
  91. ComparisonOperator.empty,
  92. ComparisonOperator.notEmpty,
  93. ]
  94. case VarType.number:
  95. return [
  96. ComparisonOperator.equal,
  97. ComparisonOperator.notEqual,
  98. ComparisonOperator.largerThan,
  99. ComparisonOperator.lessThan,
  100. ComparisonOperator.largerThanOrEqual,
  101. ComparisonOperator.lessThanOrEqual,
  102. ComparisonOperator.empty,
  103. ComparisonOperator.notEmpty,
  104. ]
  105. case VarType.file:
  106. return [
  107. ComparisonOperator.exists,
  108. ComparisonOperator.notExists,
  109. ]
  110. case VarType.arrayString:
  111. case VarType.arrayNumber:
  112. return [
  113. ComparisonOperator.contains,
  114. ComparisonOperator.notContains,
  115. ComparisonOperator.empty,
  116. ComparisonOperator.notEmpty,
  117. ]
  118. case VarType.array:
  119. case VarType.arrayObject:
  120. return [
  121. ComparisonOperator.empty,
  122. ComparisonOperator.notEmpty,
  123. ]
  124. case VarType.arrayFile:
  125. return [
  126. ComparisonOperator.contains,
  127. ComparisonOperator.notContains,
  128. ComparisonOperator.allOf,
  129. ComparisonOperator.empty,
  130. ComparisonOperator.notEmpty,
  131. ]
  132. default:
  133. return [
  134. ComparisonOperator.is,
  135. ComparisonOperator.isNot,
  136. ComparisonOperator.empty,
  137. ComparisonOperator.notEmpty,
  138. ]
  139. }
  140. }
  141. export const comparisonOperatorNotRequireValue = (operator?: ComparisonOperator) => {
  142. if (!operator)
  143. return false
  144. return [ComparisonOperator.empty, ComparisonOperator.notEmpty, ComparisonOperator.isNull, ComparisonOperator.isNotNull, ComparisonOperator.exists, ComparisonOperator.notExists].includes(operator)
  145. }
  146. export const branchNameCorrect = (branches: Branch[]) => {
  147. const branchLength = branches.length
  148. if (branchLength < 2)
  149. throw new Error('if-else node branch number must than 2')
  150. if (branchLength === 2) {
  151. return branches.map((branch) => {
  152. return {
  153. ...branch,
  154. name: branch.id === 'false' ? 'ELSE' : 'IF',
  155. }
  156. })
  157. }
  158. return branches.map((branch, index) => {
  159. return {
  160. ...branch,
  161. name: branch.id === 'false' ? 'ELSE' : `CASE ${index + 1}`,
  162. }
  163. })
  164. }