utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. exports.__esModule = true;
  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. exports.cleanPatches = cleanPatches;
  5. exports.parsePath = parsePath;
  6. var _array = require('../../helpers/array');
  7. /**
  8. * Clean and extend patches from jsonpatch observer.
  9. *
  10. * @param {Array} patches
  11. * @returns {Array}
  12. */
  13. function cleanPatches(patches) {
  14. var newOrRemovedColumns = [];
  15. /**
  16. * If observeChanges uses native Object.observe method, then it produces patches for length property. Filter them.
  17. * If path can't be parsed. Filter it.
  18. */
  19. patches = (0, _array.arrayFilter)(patches, function (patch) {
  20. if (/[/]length/ig.test(patch.path)) {
  21. return false;
  22. }
  23. if (!parsePath(patch.path)) {
  24. return false;
  25. }
  26. return true;
  27. });
  28. /**
  29. * Extend patches with changed cells coords
  30. */
  31. patches = (0, _array.arrayMap)(patches, function (patch) {
  32. var coords = parsePath(patch.path);
  33. patch.row = coords.row;
  34. patch.col = coords.col;
  35. return patch;
  36. });
  37. /**
  38. * Removing or adding column will produce one patch for each table row.
  39. * Leaves only one patch for each column add/remove operation.
  40. */
  41. patches = (0, _array.arrayFilter)(patches, function (patch) {
  42. if (['add', 'remove'].indexOf(patch.op) !== -1 && !isNaN(patch.col)) {
  43. if (newOrRemovedColumns.indexOf(patch.col) !== -1) {
  44. return false;
  45. }
  46. newOrRemovedColumns.push(patch.col);
  47. }
  48. return true;
  49. });
  50. newOrRemovedColumns.length = 0;
  51. return patches;
  52. }
  53. /**
  54. * Extract coordinates from path where data was changed.
  55. *
  56. * @param {String} path Path describing where data was changed.
  57. * @returns {Object|null} Returns an object with `row` and `col` properties or `null` if path doesn't have necessary information.
  58. */
  59. function parsePath(path) {
  60. var match = path.match(/^\/(\d+)\/?(.*)?$/);
  61. if (!match) {
  62. return null;
  63. }
  64. var _match = _slicedToArray(match, 3),
  65. row = _match[1],
  66. column = _match[2];
  67. return {
  68. row: parseInt(row, 10),
  69. col: /^\d*$/.test(column) ? parseInt(column, 10) : column
  70. };
  71. }