Manual Reference Source

app/modules/pipeline/formatter/formatfunctions/index.js

  1. // @flow
  2. const _ = require('lodash');
  3. const Utils = require('../../../utils/utils');
  4. const Handlebars = require('../../../utils/templating');
  5.  
  6. async function oarray_to_array(info: any): Promise<any> {
  7. const keys = Object.keys(info);
  8. keys.sort((a, b) => parseInt(a, 10) - parseInt(b, 10));
  9. return keys.reduce((obj, k) => {
  10. obj.push(info[k]);
  11. return obj;
  12. }, []);
  13. }
  14.  
  15.  
  16. function generic_formatter(template: string): Function {
  17. return async (existing_content: any, fullobject: Object): Promise<any> => {
  18. const t = Handlebars.compile(template)({ object: fullobject });
  19. return t;
  20. };
  21. }
  22.  
  23. async function filter_empty_or_null_objects(result: Array<any>): Promise<Array<any>> {
  24. return Utils.filter_empty_or_null_objects(result);
  25. }
  26.  
  27. /*
  28. *
  29. */
  30. function set_default_lang_for_array(flang: string, iflang: string): Function {
  31. return async (result: Array<Object>, object: Object): Promise<Array<Object>> => {
  32. if (!object[flang]) {
  33. return result;
  34. }
  35.  
  36. if (result.length > 1) {
  37. return result;
  38. }
  39.  
  40. return result.map((obj) => {
  41. if (obj[iflang]) {
  42. return obj;
  43. }
  44. obj[iflang] = object[flang];
  45. return obj;
  46. });
  47. };
  48. }
  49.  
  50. function format_string(format: String): Function {
  51. return async (result: String): Promise<String> => {
  52. if (typeof result !== 'string') {
  53. return result;
  54. }
  55. switch (format) {
  56. case 'trim':
  57. return result.trim();
  58. case 'lower':
  59. return result.toLowerCase();
  60. case 'upper':
  61. return result.toUpperCase();
  62. default:
  63. return result;
  64. }
  65. };
  66. }
  67.  
  68. module.exports = {
  69. oarray_to_array,
  70. generic_formatter,
  71. filter_empty_or_null_objects,
  72. set_default_lang_for_array,
  73. format_string,
  74. };