Manual Reference Source

app/modules/pipeline/completer/complfunctions/index.js

  1. // @flow
  2. const _ = require('lodash');
  3. const Handlebars = require('../../../utils/templating');
  4. const Utils = require('../../../utils/utils');
  5. const CryptoUtils = require('../../../utils/crypto');
  6. const EntitiesUtils = require('../../../utils/entities');
  7. const LangUtils = require('../../../utils/lang');
  8.  
  9. function generic_complete(template: string): Function {
  10. return async (object: Object, path: string, info: Object = {}) => {
  11. const t = Handlebars.compile(template)({ object, info });
  12. return Utils.make_nested_object_from_path(path.split('.'), t);
  13. };
  14. }
  15.  
  16. async function key_complete(object: Object, path: string, info: Object = {}) {
  17. const result = Utils.make_nested_object_from_path(path.split('.'), CryptoUtils.generate_key(''));
  18. return result;
  19. }
  20.  
  21. async function secret_complete(object: Object, path: string, info: Object = {}) {
  22. const result = Utils.make_nested_object_from_path(path.split('.'), CryptoUtils.generate_secret());
  23. return result;
  24. }
  25.  
  26. function initial(name_path: string, use_dash_split: boolean = true) {
  27. return async (object: Object, path: string) => {
  28. const name = Utils.find_value_with_path(object, name_path.split('.'));
  29. if (name == null || name.trim() === '') {
  30. return {};
  31. }
  32.  
  33. const stopwords = ['de', 'le', 'la', 'les', 'van', 'von', 'du', 'den', 'der', 'die'];
  34. const parts = name.split(' ');
  35. const info = parts.reduce((arr, p) => {
  36. if (stopwords.indexOf(p.toLowerCase()) !== -1) {
  37. return arr;
  38. }
  39. arr.push(p);
  40. return arr;
  41. }, []);
  42.  
  43. if (info.length === 0) {
  44. return {};
  45. }
  46.  
  47. const first = info[0];
  48.  
  49. if (first.startsWith('d\'')) {
  50. return Utils.make_nested_object_from_path(path.split('.'), first.slice(2)[0]);
  51. }
  52.  
  53. if (use_dash_split) {
  54. const dash_parts = first.split('-');
  55. if (dash_parts.length > 1) {
  56. const f = dash_parts.map(p => p[0]);
  57. return Utils.make_nested_object_from_path(path.split('.'), f.join('-'));
  58. }
  59. }
  60. return Utils.make_nested_object_from_path(path.split('.'), first[0]);
  61. };
  62. }
  63.  
  64. function denormalization(from_entity: string, from_path: string,
  65. entity_path: string, flatten: boolean, translatable: boolean, search_value: string = ''): Function {
  66. const ENV = process.env.NODE_ENV || 'local';
  67.  
  68.  
  69. return async (object: Object, path: string, info: Object = {}) => {
  70. const func = (nr, from, eseg, flat, svalue) => async (id) => {
  71. if (!id) {
  72. return null;
  73. }
  74. if (nr) {
  75. let source = null;
  76.  
  77. if (svalue !== '') {
  78. const sources = await EntitiesUtils.search_and_get_sources(from, {
  79. where: { [svalue]: id },
  80. size: 1,
  81. });
  82. if (sources.length > 0) {
  83. source = sources[0];
  84. }
  85. } else {
  86. source = await EntitiesUtils.retrieve_and_get_source(from, id);
  87. }
  88.  
  89. if (source == null) {
  90. return null;
  91. }
  92.  
  93. const eobj = Utils.find_object_with_path(source, eseg);
  94. if (eobj == null) {
  95. return null;
  96. }
  97. const last = eseg[eseg.length - 1];
  98. const value = eobj[last];
  99. if (flat) {
  100. return value;
  101. }
  102. return { [last]: value };
  103. }
  104. return id;
  105. };
  106.  
  107. const need_to_retrieve = from_entity != null && from_entity.trim() !== ''
  108. && entity_path != null && entity_path.trim() !== '';
  109. const entity_segments = entity_path.split('.');
  110.  
  111. const from_path_segments = from_path.split('.');
  112. const result = await Utils.traverse_recreate_and_execute(object, from_path_segments,
  113. func(need_to_retrieve, from_entity, entity_segments, flatten, search_value));
  114. return { denormalization: result };
  115. };
  116. }
  117.  
  118. module.exports = {
  119. generic_complete,
  120. key_complete,
  121. secret_complete,
  122. denormalization,
  123. initial,
  124. };