createOrderInfo.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const _ = require('lodash');
  2. module.exports = function createOrderInfo(input) {
  3. let order = {};
  4. let expectedPosition = 0;
  5. let separatedGroup = 1;
  6. let groupPosition = -1;
  7. appendGroup({ properties: input });
  8. function appendGroup(group) {
  9. groupPosition += 1;
  10. group.properties.forEach((item) => appendItem(item, false, group));
  11. }
  12. function appendItem(item, inFlexibleGroup, group) {
  13. if (_.isString(item)) {
  14. // In flexible groups, the expectedPosition does not ascend
  15. // to make that flexibility work;
  16. // otherwise, it will always ascend
  17. if (!inFlexibleGroup) {
  18. expectedPosition += 1;
  19. }
  20. order[item] = {
  21. separatedGroup,
  22. groupPosition,
  23. expectedPosition,
  24. groupName: group.groupName,
  25. noEmptyLineBeforeInsideGroup: group.noEmptyLineBetween,
  26. };
  27. return;
  28. }
  29. // If item is not a string, it's a group...
  30. if (item.emptyLineBefore) {
  31. separatedGroup += 1;
  32. }
  33. if (item.order === 'flexible') {
  34. expectedPosition += 1;
  35. groupPosition += 1;
  36. item.properties.forEach((property) => {
  37. appendItem(property, true, item);
  38. });
  39. } else {
  40. appendGroup(item);
  41. }
  42. }
  43. return order;
  44. };