serializer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. var _ = require('lodash');
  6. var Constants = require('./constants');
  7. var defaults = {
  8. line_breaks: 'unix',
  9. keep_quotes: false
  10. };
  11. var Serializer = function () {
  12. function Serializer() {
  13. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  14. _classCallCheck(this, Serializer);
  15. this.options = _extends({}, defaults, options);
  16. }
  17. _createClass(Serializer, [{
  18. key: 'needToBeQuoted',
  19. value: function needToBeQuoted(value) {
  20. if (this.options.keep_quotes) {
  21. return false;
  22. }
  23. // wrapped with qoutes
  24. if (value.match(/^"[\s\S]*?"$/g)) {
  25. return false;
  26. }
  27. // escaped quote at the end
  28. if (value.match(/^[\s\S]*?\\"$/g)) {
  29. return true;
  30. }
  31. // ends or starts with a quote
  32. if (value.match(/^[\s\S]*?"$/g) || value.match(/^"[\s\S]*?$/g)) {
  33. return false;
  34. }
  35. return true;
  36. }
  37. }, {
  38. key: 'serialize',
  39. value: function serialize(content) {
  40. var _this = this;
  41. return _.reduce(content, function (output, sectionContent, section) {
  42. output += '[' + section + ']' + Constants.line_breaks[_this.options.line_breaks];
  43. output += _this.serializeContent(sectionContent, '');
  44. return output;
  45. }, '');
  46. }
  47. }, {
  48. key: 'serializeContent',
  49. value: function serializeContent(content, path) {
  50. var _this2 = this;
  51. return _.reduce(content, function (serialized, subContent, key) {
  52. if (_.isArray(subContent)) {
  53. var _iteratorNormalCompletion = true;
  54. var _didIteratorError = false;
  55. var _iteratorError = undefined;
  56. try {
  57. for (var _iterator = subContent[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  58. var value = _step.value;
  59. if (_this2.needToBeQuoted(value)) {
  60. value = '"' + value + '"';
  61. }
  62. serialized += path + (path.length > 0 ? '.' : '') + key + "[]=" + value + Constants.line_breaks[_this2.options.line_breaks];
  63. }
  64. } catch (err) {
  65. _didIteratorError = true;
  66. _iteratorError = err;
  67. } finally {
  68. try {
  69. if (!_iteratorNormalCompletion && _iterator.return) {
  70. _iterator.return();
  71. }
  72. } finally {
  73. if (_didIteratorError) {
  74. throw _iteratorError;
  75. }
  76. }
  77. }
  78. } else if (_.isObject(subContent)) {
  79. serialized += _this2.serializeContent(subContent, path + (path.length > 0 ? '.' : '') + key);
  80. } else {
  81. if (_this2.needToBeQuoted(subContent)) {
  82. subContent = '"' + subContent + '"';
  83. }
  84. serialized += path + (path.length > 0 ? '.' : '') + key + "=" + subContent + Constants.line_breaks[_this2.options.line_breaks];
  85. }
  86. return serialized;
  87. }, '');
  88. }
  89. }]);
  90. return Serializer;
  91. }();
  92. module.exports = Serializer;