parser.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. 'use strict';
  2. // match1 - section, match2 - optional full inheritance part, match3 - inherited section
  3. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  4. 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; };
  5. 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; }; }();
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. var REGEXP_SECTION = /^\s*\[\s*([^:]*?)\s*(:\s*(.+?)\s*)?\]\s*$/;
  8. var REGEXP_COMMENT = /^;.*/;
  9. var REGEXP_SINGLE_LINE = /^\s*(.*?)\s*?=\s*?(\S.*?)$/;
  10. var REGEXP_MULTI_LINE = /^\s*(.*?)\s*?=\s*?"(.*?)$/;
  11. var REGEXP_NOT_ESCAPED_MULTI_LINE_END = /^(.*?)\\"$/;
  12. var REGEXP_MULTI_LINE_END = /^(.*?)"$/;
  13. var REGEXP_ARRAY = /^(.*?)\[\]$/;
  14. var STATUS_OK = 0;
  15. var STATUS_INVALID = 1;
  16. var defaults = {
  17. ignore_invalid: true,
  18. keep_quotes: false,
  19. nested_section_names: false,
  20. keep_zero_prefix: false,
  21. oninvalid: function () {
  22. return true;
  23. },
  24. filters: [],
  25. constants: {}
  26. };
  27. var REGEXP_IGNORE_KEYS = /__proto__|constructor|prototype/;
  28. var Parser = function () {
  29. function Parser() {
  30. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  31. _classCallCheck(this, Parser);
  32. this.options = _extends({}, defaults, options);
  33. this.handlers = [this.handleMultiLineStart, this.handleMultiLineEnd, this.handleMultiLineAppend, this.handleComment, this.handleSection, this.handleSingleLine];
  34. }
  35. _createClass(Parser, [{
  36. key: 'parse',
  37. value: function parse(lines) {
  38. var ctx = {
  39. ini: {},
  40. current: {},
  41. multiLineKeys: false,
  42. multiLineValue: ''
  43. };
  44. var _iteratorNormalCompletion = true;
  45. var _didIteratorError = false;
  46. var _iteratorError = undefined;
  47. try {
  48. for (var _iterator = lines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  49. var line = _step.value;
  50. var _iteratorNormalCompletion2 = true;
  51. var _didIteratorError2 = false;
  52. var _iteratorError2 = undefined;
  53. try {
  54. for (var _iterator2 = this.handlers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  55. var handler = _step2.value;
  56. var stop = handler.call(this, ctx, line);
  57. if (stop) {
  58. break;
  59. }
  60. }
  61. } catch (err) {
  62. _didIteratorError2 = true;
  63. _iteratorError2 = err;
  64. } finally {
  65. try {
  66. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  67. _iterator2.return();
  68. }
  69. } finally {
  70. if (_didIteratorError2) {
  71. throw _iteratorError2;
  72. }
  73. }
  74. }
  75. }
  76. } catch (err) {
  77. _didIteratorError = true;
  78. _iteratorError = err;
  79. } finally {
  80. try {
  81. if (!_iteratorNormalCompletion && _iterator.return) {
  82. _iterator.return();
  83. }
  84. } finally {
  85. if (_didIteratorError) {
  86. throw _iteratorError;
  87. }
  88. }
  89. }
  90. return ctx.ini;
  91. }
  92. }, {
  93. key: 'isSection',
  94. value: function isSection(line) {
  95. return line.match(REGEXP_SECTION);
  96. }
  97. }, {
  98. key: 'getSection',
  99. value: function getSection(line) {
  100. return line.match(REGEXP_SECTION)[1];
  101. }
  102. }, {
  103. key: 'getParentSection',
  104. value: function getParentSection(line) {
  105. return line.match(REGEXP_SECTION)[3];
  106. }
  107. }, {
  108. key: 'isInheritedSection',
  109. value: function isInheritedSection(line) {
  110. return !!line.match(REGEXP_SECTION)[3];
  111. }
  112. }, {
  113. key: 'isComment',
  114. value: function isComment(line) {
  115. return line.match(REGEXP_COMMENT);
  116. }
  117. }, {
  118. key: 'isSingleLine',
  119. value: function isSingleLine(line) {
  120. var result = line.match(REGEXP_SINGLE_LINE);
  121. if (!result) {
  122. return false;
  123. }
  124. var check = result[2].match(/"/g);
  125. return !check || check.length % 2 === 0;
  126. }
  127. }, {
  128. key: 'isMultiLine',
  129. value: function isMultiLine(line) {
  130. var result = line.match(REGEXP_MULTI_LINE);
  131. if (!result) {
  132. return false;
  133. }
  134. var check = result[2].match(/"/g);
  135. return !check || check.length % 2 === 0;
  136. }
  137. }, {
  138. key: 'isMultiLineEnd',
  139. value: function isMultiLineEnd(line) {
  140. return line.match(REGEXP_MULTI_LINE_END) && !line.match(REGEXP_NOT_ESCAPED_MULTI_LINE_END);
  141. }
  142. }, {
  143. key: 'isArray',
  144. value: function isArray(line) {
  145. return line.match(REGEXP_ARRAY);
  146. }
  147. }, {
  148. key: 'assignValue',
  149. value: function assignValue(element, keys, value) {
  150. value = this.applyFilter(value);
  151. var current = element;
  152. var previous = element;
  153. var array = false;
  154. var key = void 0;
  155. if (keys.some(function (key) {
  156. return REGEXP_IGNORE_KEYS.test(key);
  157. })) {
  158. return;
  159. }
  160. var _iteratorNormalCompletion3 = true;
  161. var _didIteratorError3 = false;
  162. var _iteratorError3 = undefined;
  163. try {
  164. for (var _iterator3 = keys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  165. key = _step3.value;
  166. if (this.isArray(key)) {
  167. key = this.getArrayKey(key);
  168. array = true;
  169. }
  170. if (current[key] == null) {
  171. current[key] = array ? [] : {};
  172. }
  173. previous = current;
  174. current = current[key];
  175. }
  176. } catch (err) {
  177. _didIteratorError3 = true;
  178. _iteratorError3 = err;
  179. } finally {
  180. try {
  181. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  182. _iterator3.return();
  183. }
  184. } finally {
  185. if (_didIteratorError3) {
  186. throw _iteratorError3;
  187. }
  188. }
  189. }
  190. if (array) {
  191. current.push(value);
  192. } else {
  193. previous[key] = value;
  194. }
  195. return element;
  196. }
  197. }, {
  198. key: 'applyFilter',
  199. value: function applyFilter(value) {
  200. var _iteratorNormalCompletion4 = true;
  201. var _didIteratorError4 = false;
  202. var _iteratorError4 = undefined;
  203. try {
  204. for (var _iterator4 = this.options.filters[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  205. var filter = _step4.value;
  206. value = filter(value, this.options);
  207. }
  208. } catch (err) {
  209. _didIteratorError4 = true;
  210. _iteratorError4 = err;
  211. } finally {
  212. try {
  213. if (!_iteratorNormalCompletion4 && _iterator4.return) {
  214. _iterator4.return();
  215. }
  216. } finally {
  217. if (_didIteratorError4) {
  218. throw _iteratorError4;
  219. }
  220. }
  221. }
  222. return value;
  223. }
  224. }, {
  225. key: 'getKeyValue',
  226. value: function getKeyValue(line) {
  227. var result = line.match(REGEXP_SINGLE_LINE);
  228. if (!result) {
  229. throw new Error();
  230. }
  231. var _result = _slicedToArray(result, 3),
  232. key = _result[1],
  233. value = _result[2];
  234. if (!this.options.keep_quotes) {
  235. value = value.replace(/^\s*?"(.*?)"\s*?$/, '$1');
  236. }
  237. return { key: key, value: value, status: STATUS_OK };
  238. }
  239. }, {
  240. key: 'getMultiKeyValue',
  241. value: function getMultiKeyValue(line) {
  242. var result = line.match(REGEXP_MULTI_LINE);
  243. if (!result) {
  244. throw new Error();
  245. }
  246. var _result2 = _slicedToArray(result, 3),
  247. key = _result2[1],
  248. value = _result2[2];
  249. if (this.options.keep_quotes) {
  250. value = '"' + value;
  251. }
  252. return { key: key, value: value };
  253. }
  254. }, {
  255. key: 'getMultiLineEndValue',
  256. value: function getMultiLineEndValue(line) {
  257. var result = line.match(REGEXP_MULTI_LINE_END);
  258. if (!result) {
  259. throw new Error();
  260. }
  261. var _result3 = _slicedToArray(result, 2),
  262. value = _result3[1];
  263. if (this.options.keep_quotes) {
  264. value = value + '"';
  265. }
  266. return { value: value, status: STATUS_OK };
  267. }
  268. }, {
  269. key: 'getArrayKey',
  270. value: function getArrayKey(line) {
  271. var result = line.match(REGEXP_ARRAY);
  272. return result[1];
  273. }
  274. }, {
  275. key: 'handleMultiLineStart',
  276. value: function handleMultiLineStart(ctx, line) {
  277. if (!this.isMultiLine(line.trim())) {
  278. return false;
  279. }
  280. var _getMultiKeyValue = this.getMultiKeyValue(line),
  281. key = _getMultiKeyValue.key,
  282. value = _getMultiKeyValue.value;
  283. var keys = key.split('.');
  284. ctx.multiLineKeys = keys;
  285. ctx.multiLineValue = value;
  286. return true;
  287. }
  288. }, {
  289. key: 'handleMultiLineEnd',
  290. value: function handleMultiLineEnd(ctx, line) {
  291. if (!ctx.multiLineKeys || !this.isMultiLineEnd(line.trim())) {
  292. return false;
  293. }
  294. var _getMultiLineEndValue = this.getMultiLineEndValue(line),
  295. value = _getMultiLineEndValue.value,
  296. status = _getMultiLineEndValue.status;
  297. // abort on false of onerror callback if we meet an invalid line
  298. if (status === STATUS_INVALID && !this.options.oninvalid(line)) {
  299. return;
  300. }
  301. // ignore whole multiline on invalid
  302. if (status === STATUS_INVALID && this.options.ignore_invalid) {
  303. ctx.multiLineKeys = false;
  304. ctx.multiLineValue = '';
  305. return true;
  306. }
  307. ctx.multiLineValue += '\n' + value;
  308. this.assignValue(ctx.current, ctx.multiLineKeys, ctx.multiLineValue);
  309. ctx.multiLineKeys = false;
  310. ctx.multiLineValue = '';
  311. return true;
  312. }
  313. }, {
  314. key: 'handleMultiLineAppend',
  315. value: function handleMultiLineAppend(ctx, line) {
  316. if (!ctx.multiLineKeys || this.isMultiLineEnd(line.trim())) {
  317. return false;
  318. }
  319. ctx.multiLineValue += '\n' + line;
  320. return true;
  321. }
  322. }, {
  323. key: 'handleComment',
  324. value: function handleComment(ctx, line) {
  325. return this.isComment(line.trim());
  326. }
  327. }, {
  328. key: 'handleSection',
  329. value: function handleSection(ctx, line) {
  330. line = line.trim();
  331. if (!this.isSection(line)) {
  332. return false;
  333. }
  334. var section = this.getSection(line);
  335. if (REGEXP_IGNORE_KEYS.test(section)) {
  336. return false;
  337. }
  338. this.createSection(ctx, section);
  339. if (this.isInheritedSection(line)) {
  340. var parentSection = this.getParentSection(line);
  341. ctx.current = _extends(ctx.current, JSON.parse(JSON.stringify(ctx.ini[parentSection])));
  342. }
  343. return true;
  344. }
  345. }, {
  346. key: 'handleSingleLine',
  347. value: function handleSingleLine(ctx, line) {
  348. line = line.trim();
  349. if (!this.isSingleLine(line)) {
  350. return false;
  351. }
  352. var _getKeyValue = this.getKeyValue(line),
  353. key = _getKeyValue.key,
  354. value = _getKeyValue.value,
  355. status = _getKeyValue.status;
  356. // abort on false of onerror callback if we meet an invalid line
  357. if (status === STATUS_INVALID && !this.options.oninvalid(line)) {
  358. throw new Error('Abort');
  359. }
  360. // skip entry
  361. if (status === STATUS_INVALID && !this.options.ignore_invalid) {
  362. return true;
  363. }
  364. var keys = key.split('.');
  365. this.assignValue(ctx.current, keys, value);
  366. return true;
  367. }
  368. }, {
  369. key: 'createSection',
  370. value: function createSection(ctx, section) {
  371. var sections = (this.options.nested_section_names ? section.split('.') : [section]).map(function (name) {
  372. return name.trim();
  373. });
  374. ctx.current = sections.reduce(function (ini, name) {
  375. if (typeof ini[name] === 'undefined') {
  376. ini[name] = {};
  377. }
  378. return ini[name];
  379. }, ctx.ini);
  380. }
  381. }]);
  382. return Parser;
  383. }();
  384. module.exports = Parser;