binaryScalarOperations.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { defaultAddOperationHandler } from './shared/operationUtils';
  2. import { QueryBuilderOperation, QueryBuilderOperationDef, QueryBuilderOperationParamDef } from './shared/types';
  3. import { PromOperationId, PromVisualQueryOperationCategory } from './types';
  4. export const binaryScalarDefs = [
  5. {
  6. id: PromOperationId.Addition,
  7. name: 'Add scalar',
  8. sign: '+',
  9. },
  10. {
  11. id: PromOperationId.Subtraction,
  12. name: 'Subtract scalar',
  13. sign: '-',
  14. },
  15. {
  16. id: PromOperationId.MultiplyBy,
  17. name: 'Multiply by scalar',
  18. sign: '*',
  19. },
  20. {
  21. id: PromOperationId.DivideBy,
  22. name: 'Divide by scalar',
  23. sign: '/',
  24. },
  25. {
  26. id: PromOperationId.Modulo,
  27. name: 'Modulo by scalar',
  28. sign: '%',
  29. },
  30. {
  31. id: PromOperationId.Exponent,
  32. name: 'Exponent',
  33. sign: '^',
  34. },
  35. {
  36. id: PromOperationId.EqualTo,
  37. name: 'Equal to',
  38. sign: '==',
  39. comparison: true,
  40. },
  41. {
  42. id: PromOperationId.NotEqualTo,
  43. name: 'Not equal to',
  44. sign: '!=',
  45. comparison: true,
  46. },
  47. {
  48. id: PromOperationId.GreaterThan,
  49. name: 'Greater than',
  50. sign: '>',
  51. comparison: true,
  52. },
  53. {
  54. id: PromOperationId.LessThan,
  55. name: 'Less than',
  56. sign: '<',
  57. comparison: true,
  58. },
  59. {
  60. id: PromOperationId.GreaterOrEqual,
  61. name: 'Greater or equal to',
  62. sign: '>=',
  63. comparison: true,
  64. },
  65. {
  66. id: PromOperationId.LessOrEqual,
  67. name: 'Less or equal to',
  68. sign: '<=',
  69. comparison: true,
  70. },
  71. ];
  72. export const binaryScalarOperatorToOperatorName = binaryScalarDefs.reduce((acc, def) => {
  73. acc[def.sign] = {
  74. id: def.id,
  75. comparison: def.comparison,
  76. };
  77. return acc;
  78. }, {} as Record<string, { id: string; comparison?: boolean }>);
  79. // Not sure about this one. It could also be a more generic 'Simple math operation' where user specifies
  80. // both the operator and the operand in a single input
  81. export const binaryScalarOperations: QueryBuilderOperationDef[] = binaryScalarDefs.map((opDef) => {
  82. const params: QueryBuilderOperationParamDef[] = [{ name: 'Value', type: 'number' }];
  83. const defaultParams: any[] = [2];
  84. if (opDef.comparison) {
  85. params.push({
  86. name: 'Bool',
  87. type: 'boolean',
  88. description: 'If checked comparison will return 0 or 1 for the value rather than filtering.',
  89. });
  90. defaultParams.push(false);
  91. }
  92. return {
  93. id: opDef.id,
  94. name: opDef.name,
  95. params,
  96. defaultParams,
  97. alternativesKey: 'binary scalar operations',
  98. category: PromVisualQueryOperationCategory.BinaryOps,
  99. renderer: getSimpleBinaryRenderer(opDef.sign),
  100. addOperationHandler: defaultAddOperationHandler,
  101. };
  102. });
  103. function getSimpleBinaryRenderer(operator: string) {
  104. return function binaryRenderer(model: QueryBuilderOperation, def: QueryBuilderOperationDef, innerExpr: string) {
  105. let param = model.params[0];
  106. let bool = '';
  107. if (model.params.length === 2) {
  108. bool = model.params[1] ? ' bool' : '';
  109. }
  110. return `${innerExpr} ${operator}${bool} ${param}`;
  111. };
  112. }