dataclasses/columns.js

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