Skip to content

Commit 339b7cd

Browse files
committed
feat: expand types to allow differently typed arrays
With a comparator function, there's not a hard requirement for the two arrays to be compatible types. The comparator tells us whether they are "equal", and that's enough. This will maintain the integrity between which type corresponds to what in the comparators, as well as the functions where appropriate. This doesn't touch `getPatch` or `applyPatch`, because patching doesn’t make sense if the types don’t match.
1 parent 625d5f0 commit 339b7cd

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/diff/diff.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import bestSubSequence from './lcs';
22

3-
export interface DiffData<T> {
3+
export interface DiffData<T, U = T> {
44
removed: T[];
5-
added: T[];
5+
added: U[];
66
}
77

8-
export function diff<T>(
8+
export function diff<T, U = T>(
99
a: T[],
10-
b: T[],
11-
compareFunc: (ia: T, ib: T) => boolean = (ia: T, ib: T) => ia === ib,
12-
): DiffData<T> {
13-
const ret: DiffData<T> = {
10+
b: U[],
11+
compareFunc: (ia: T, ib: U) => boolean = (ia: T, ib: U) => ia === (ib as unknown as T),
12+
): DiffData<T, U> {
13+
const ret: DiffData<T, U> = {
1414
removed: [],
1515
added: [],
1616
};

src/diff/lcs.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function lcs<T>(a: T[], b: T[], compareFunc: (a: T, b: T) => boolean): number {
1+
function lcs<T, U = T>(a: T[], b: U[], compareFunc: (a: T, b: U) => boolean): number {
22
const M = a.length,
33
N = b.length;
44
const MAX = M + N;
@@ -39,23 +39,23 @@ enum Direct {
3939
all = horizontal | vertical | diagonal,
4040
}
4141

42-
function getSolution<T>(
42+
function getSolution<T, U = T>(
4343
a: T[],
4444
aStart: number,
4545
aEnd: number,
46-
b: T[],
46+
b: U[],
4747
bStart: number,
4848
bEnd: number,
4949
d: number,
5050
startDirect: Direct,
5151
endDirect: Direct,
52-
compareFunc: (a: T, b: T) => boolean,
52+
compareFunc: (a: T, b: U) => boolean,
5353
elementsChanged: (
5454
type: 'add' | 'remove' | 'same',
5555
a: T[],
5656
aStart: number,
5757
aEnd: number,
58-
b: T[],
58+
b: U[],
5959
bStart: number,
6060
bEnd: number,
6161
) => void,
@@ -285,16 +285,16 @@ function getSolution<T>(
285285
);
286286
}
287287

288-
export default function bestSubSequence<T>(
288+
export default function bestSubSequence<T, U = T>(
289289
a: T[],
290-
b: T[],
291-
compareFunc: (a: T, b: T) => boolean,
290+
b: U[],
291+
compareFunc: (a: T, b: U) => boolean,
292292
elementsChanged: (
293293
type: 'add' | 'remove' | 'same',
294294
a: T[],
295295
aStart: number,
296296
aEnd: number,
297-
b: T[],
297+
b: U[],
298298
bStart: number,
299299
bEnd: number,
300300
) => void,

src/diff/same.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import bestSubSequence from './lcs';
22

3-
export default function <T>(
3+
export default function <T, U = T>(
44
a: T[],
5-
b: T[],
6-
compareFunc: (ia: T, ib: T) => boolean = (ia: T, ib: T) => ia === ib,
5+
b: U[],
6+
compareFunc: (ia: T, ib: U) => boolean = (ia: T, ib: U) => ia === (ib as unknown as T),
77
): T[] {
88
const ret: T[] = [];
99
bestSubSequence(a, b, compareFunc, (type, oldArr, oldStart, oldEnd) => {

0 commit comments

Comments
 (0)