utils.js 2.5 KB

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