dataclasses/columns.js

  1. const { mergeAll, trim } = require('ramda');
  2. const $_type = require('./$_type');
  3. /**
  4. * Target type definition
  5. * @typedef {Object} Target
  6. * @memberof dataclasses
  7. */
  8. /**
  9. * Create a new target
  10. * @param {object} target target
  11. * @param {string} target.target_id - unique id for this target
  12. * @param {string} target.label - label that will be displayed for this target
  13. * @param {string} target.type_id - the type_id name this target has
  14. * @return {Target} {@link dataclasses.Target}
  15. * @memberof dataclasses
  16. */
  17. function Target(target) {
  18. const { target_id, label, type_id } = _requireProps(
  19. target,
  20. 'target_id,label,type_id'
  21. );
  22. // target MUST at least have the attributes bellow
  23. return mergeAll([$_type('Target'), { target_id, label, type_id }, target]);
  24. }
  25. /**
  26. * @param {Target} target target
  27. * @return {Object} JSON serializable object
  28. */
  29. Target.toJSON = function(target) {
  30. return {
  31. target_id: target.target_id,
  32. };
  33. };
  34. /**
  35. * A type operator
  36. * @typedef Operator
  37. * @memberof dataclasses
  38. */
  39. /**
  40. * Create a new operator
  41. * @param {object} operator operator
  42. * @param {string} operator.operator_id - unique id for this operator
  43. * @param {string} operator.argumentType_id - the argumentType associated with the operator. For example to define a "is" operator and associate it with two targets "article publishing date" and "article title" then we would have to define 2 operators because ["article publishing date" "is"] and ["article title" "is"] do NOT have the same kind of arguments (one is a date the other is a string) and associated UI components (one is a date picker the other is a text input)
  44. * @param {string} operator.label - label that will be displayed for this operator
  45. * @return {Operator} {@link dataclasses.Operator}
  46. * @memberof dataclasses
  47. */
  48. function Operator(operator) {
  49. // operator MUST at least have the properties bellow
  50. const { operator_id, label, argumentType_id } = _requireProps(
  51. operator,
  52. 'operator_id,label,argumentType_id'
  53. );
  54. return mergeAll([
  55. $_type('Operator'),
  56. {
  57. operator_id,
  58. argumentType_id,
  59. label,
  60. },
  61. operator,
  62. ]);
  63. }
  64. /**
  65. * @param {Operator} operator operator
  66. * @return {Object} JSON serializable object
  67. */
  68. Operator.toJSON = function(operator) {
  69. return {
  70. operator_id: operator.operator_id,
  71. };
  72. };
  73. /**
  74. * Defines a target type
  75. * @typedef Type
  76. * @memberof dataclasses
  77. */
  78. /**
  79. * Create a new type
  80. * @param {Object} type type
  81. * @param {string} type.type_id type_id
  82. * @param {string[]} type.operator_ids array of operator_id
  83. * @return {Type} {@link dataclasses.Type}
  84. * @memberof dataclasses
  85. */
  86. function Type(type) {
  87. // type MUST at least have the properties bellow
  88. const { type_id, operator_ids } = _requireProps(type, 'type_id,operator_ids');
  89. return mergeAll([
  90. $_type('Type'),
  91. {
  92. type_id,
  93. operator_ids,
  94. },
  95. type,
  96. ]);
  97. }
  98. /**
  99. * Create a new type logical type
  100. * Logical types or used in CompoundPredicates
  101. * @param {Object} logicalType The predicate logic
  102. * @param {string} logicalType.logicalType_id logicalType_id
  103. * @param {string} logicalType.label label
  104. * @return {object} logicalType object
  105. * @memberof dataclasses
  106. */
  107. function LogicalType(logicalType) {
  108. // logicalType MUST at least have the properties bellow
  109. const { logicalType_id, label } = _requireProps(
  110. logicalType,
  111. 'logicalType_id,label'
  112. );
  113. return mergeAll([
  114. $_type('LogicalType'),
  115. {
  116. logicalType_id,
  117. label,
  118. },
  119. logicalType,
  120. ]);
  121. }
  122. /**
  123. * @param {LogicalType} logicalType logicalType
  124. * @return {Object} JSON serializable object
  125. */
  126. LogicalType.toJSON = function(logicalType) {
  127. return {
  128. logicalType_id: logicalType.logicalType_id,
  129. };
  130. };
  131. /**
  132. * Create a new argument type
  133. * @param {Object} argumentType argumentType
  134. * @param {string} type.argumentType_id argumentType_id
  135. * @param {*} type.component this attribute will be used by the UI Framework adapter
  136. * @return {object} ArgumentType
  137. * @memberof dataclasses
  138. */
  139. function ArgumentType(argumentType) {
  140. // argumentType MUST at least have the properties bellow
  141. const { argumentType_id, component } = _requireProps(
  142. argumentType,
  143. 'argumentType_id,component'
  144. );
  145. return mergeAll([
  146. $_type('ArgumentType'),
  147. {
  148. argumentType_id,
  149. component,
  150. },
  151. argumentType,
  152. ]);
  153. }
  154. const _toString = Object.prototype.toString;
  155. function _isObject(mixed) {
  156. return _toString.call(mixed) === '[object Object]';
  157. }
  158. /**
  159. * @param {Object} object object
  160. * @param {string} properties comma-separated list of properties
  161. * @return {Object} the passed object
  162. * @throws throw if a property is missing from the object
  163. * @private
  164. */
  165. function _requireProps(object, properties) {
  166. if (!_isObject(object))
  167. throw new Error(`Object is required, got: ${JSON.stringify(object)}.`);
  168. const props = properties.split(',').map(trim);
  169. let prop;
  170. while ((prop = props.pop())) {
  171. if (!object.hasOwnProperty(prop)) {
  172. throw new Error(
  173. `Object ${JSON.stringify(object)} MUST have a '${prop}' property.`
  174. );
  175. }
  176. }
  177. return object;
  178. }
  179. module.exports = {
  180. Type,
  181. Target,
  182. Operator,
  183. LogicalType,
  184. ArgumentType,
  185. _requireProps,
  186. };