template.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = template;
  6. var _interpolation = require('./interpolation');
  7. /**
  8. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. */
  14. function template(title, headings, row) {
  15. const table = convertRowToTable(row, headings);
  16. const templates = convertTableToTemplates(table, headings);
  17. return templates.map((template, index) => ({
  18. arguments: [template],
  19. title: (0, _interpolation.interpolateVariables)(title, template, index)
  20. }));
  21. }
  22. const convertRowToTable = (row, headings) =>
  23. Array.from({
  24. length: row.length / headings.length
  25. }).map((_, index) =>
  26. row.slice(
  27. index * headings.length,
  28. index * headings.length + headings.length
  29. )
  30. );
  31. const convertTableToTemplates = (table, headings) =>
  32. table.map(row =>
  33. row.reduce(
  34. (acc, value, index) =>
  35. Object.assign(acc, {
  36. [headings[index]]: value
  37. }),
  38. {}
  39. )
  40. );