multi-ini-class.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 fs = require('fs');
  6. var _ = require('lodash');
  7. var Parser = require('./parser');
  8. var Serializer = require('./serializer');
  9. var Constants = require('./constants');
  10. var defaults = {
  11. encoding: 'utf8',
  12. line_breaks: 'unix'
  13. };
  14. var MultiIni = function () {
  15. function MultiIni() {
  16. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  17. _classCallCheck(this, MultiIni);
  18. this.options = _extends({}, defaults, options);
  19. this.parser = new Parser(this.options);
  20. this.serializer = new Serializer(this.options);
  21. }
  22. _createClass(MultiIni, [{
  23. key: 'read',
  24. value: function read(filename) {
  25. if (!filename) {
  26. throw new Error('Missing filename.');
  27. }
  28. var lines = this.fetchLines(filename);
  29. return this.parser.parse(lines);
  30. }
  31. }, {
  32. key: 'fetchLines',
  33. value: function fetchLines(filename) {
  34. var content = fs.readFileSync(filename, this.options);
  35. return content.split(Constants.line_breaks[this.options.line_breaks]);
  36. }
  37. }, {
  38. key: 'write',
  39. value: function write(filename) {
  40. var content = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  41. fs.writeFileSync(filename, this.serializer.serialize(content), this.options);
  42. return;
  43. }
  44. }]);
  45. return MultiIni;
  46. }();
  47. module.exports = MultiIni;