diffStrings.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _diffSequences = _interopRequireDefault(require('diff-sequences'));
  7. var _cleanupSemantic = require('./cleanupSemantic');
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. /**
  12. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  13. *
  14. * This source code is licensed under the MIT license found in the
  15. * LICENSE file in the root directory of this source tree.
  16. */
  17. const diffStrings = (a, b) => {
  18. const isCommon = (aIndex, bIndex) => a[aIndex] === b[bIndex];
  19. let aIndex = 0;
  20. let bIndex = 0;
  21. const diffs = [];
  22. const foundSubsequence = (nCommon, aCommon, bCommon) => {
  23. if (aIndex !== aCommon) {
  24. diffs.push(
  25. new _cleanupSemantic.Diff(
  26. _cleanupSemantic.DIFF_DELETE,
  27. a.slice(aIndex, aCommon)
  28. )
  29. );
  30. }
  31. if (bIndex !== bCommon) {
  32. diffs.push(
  33. new _cleanupSemantic.Diff(
  34. _cleanupSemantic.DIFF_INSERT,
  35. b.slice(bIndex, bCommon)
  36. )
  37. );
  38. }
  39. aIndex = aCommon + nCommon; // number of characters compared in a
  40. bIndex = bCommon + nCommon; // number of characters compared in b
  41. diffs.push(
  42. new _cleanupSemantic.Diff(
  43. _cleanupSemantic.DIFF_EQUAL,
  44. b.slice(bCommon, bIndex)
  45. )
  46. );
  47. };
  48. (0, _diffSequences.default)(a.length, b.length, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items.
  49. if (aIndex !== a.length) {
  50. diffs.push(
  51. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, a.slice(aIndex))
  52. );
  53. }
  54. if (bIndex !== b.length) {
  55. diffs.push(
  56. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, b.slice(bIndex))
  57. );
  58. }
  59. return diffs;
  60. };
  61. var _default = diffStrings;
  62. exports.default = _default;