• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

YuJianrong / fast-array-diff / 8136596112

04 Mar 2024 06:43AM UTC coverage: 100.0%. Remained the same
8136596112

push

github

web-flow
Update dependabot.yml

143 of 147 branches covered (97.28%)

187 of 187 relevant lines covered (100.0%)

2074.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/diff/patch.ts
1
import bestSubSequence from './lcs';
1✔
2

3
export interface PatchItem<T> {
4
  type: 'add' | 'remove';
5
  oldPos: number;
6
  newPos: number;
7
  items: T[];
8
}
9

10
export type Patch<T> = PatchItem<T>[];
11

12
export function getPatch<T>(
2✔
13
  a: T[],
14
  b: T[],
15
  compareFunc: (ia: T, ib: T) => boolean = (ia: T, ib: T) => ia === ib,
3,247!
16
): Patch<T> {
17
  const patch: Patch<T> = [];
20✔
18
  let lastAdd: PatchItem<T> | null = null;
20✔
19
  let lastRemove: PatchItem<T> | null = null;
20✔
20

21
  function pushChange(
1✔
22
    type: 'add' | 'remove' | 'same',
23
    oldArr: T[],
24
    oldStart: number,
25
    oldEnd: number,
26
    newArr: T[],
27
    newStart: number,
28
    newEnd: number,
29
  ) {
30
    if (type === 'same') {
114✔
31
      if (lastRemove) {
65✔
32
        patch.push(lastRemove);
22✔
33
      }
34
      if (lastAdd) {
65✔
35
        patch.push(lastAdd);
18✔
36
      }
37
      lastRemove = null;
65✔
38
      lastAdd = null;
65✔
39
    } else if (type === 'remove') {
49✔
40
      if (!lastRemove) {
22!
41
        lastRemove = {
22✔
42
          type: 'remove',
43
          oldPos: oldStart as number,
44
          newPos: newStart as number,
45
          items: [],
46
        };
47
      }
48
      for (let i = oldStart; i < oldEnd; ++i) {
22✔
49
        lastRemove.items.push(oldArr[i]);
28✔
50
      }
51
      if (lastAdd) {
22✔
52
        lastAdd.oldPos += oldEnd - oldStart;
2✔
53
        if (lastRemove.oldPos === oldStart) {
2!
54
          lastRemove.newPos -= oldEnd - oldStart;
2✔
55
        }
56
      }
57
    } else if (type === 'add') {
27!
58
      if (!lastAdd) {
27✔
59
        lastAdd = {
18✔
60
          type: 'add',
61
          oldPos: oldStart,
62
          newPos: newStart,
63
          items: [],
64
        };
65
      }
66
      for (let i = newStart; i < newEnd; ++i) {
27✔
67
        lastAdd.items.push(newArr[i]);
45✔
68
      }
69
    }
70
  }
71

72
  bestSubSequence(a, b, compareFunc, pushChange);
20✔
73

74
  pushChange('same', [], 0, 0, [], 0, 0);
20✔
75

76
  return patch;
20✔
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc