Skip to content

Commit ac8860f

Browse files
committed
feat(history): improving getSelectiveOf to have the index of each command
1 parent 2fc298e commit ac8860f

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/api/history/LinearHistory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ export abstract class LinearHistory implements LinearHistoryBase {
9393
* done using the === operator.
9494
* @returns Two arrays. The first one contains the matching commands in the undo stack.
9595
* The second one contains the matching commands in the redo stack.
96+
* Each undoable is associated with its index in its undo/redo stack.
9697
*/
97-
public abstract getSelectiveOf<T extends object | string | number>
98-
(key: T, eqFn?: (v1: T, v2: T) => boolean): [undos: Array<Undoable>, redos: Array<Undoable>];
98+
public abstract getSelectiveOf<T extends object | string | number> (key: T, eqFn?: (v1: T, v2: T) => boolean):
99+
[undos: Array<[undoable: Undoable, index: number]>, redos: Array<[undoable: Undoable, index: number]>];
99100

100101
/**
101102
* Undoes the last undoable objects up to go to the provided index.

src/impl/history/LinearHistoryImpl.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,17 @@ export class LinearHistoryImpl extends LinearHistory {
201201
this.sizePublisher.next(this.size());
202202
}
203203

204-
public getSelectiveOf<T extends object | string | number>
205-
(obj: T, eqFn?: (v1: T, v2: T) => boolean): [undos: Array<Undoable>, redos: Array<Undoable>] {
206-
const fn = (undoable: Undoable): boolean => hasSelectiveValue(undoable, obj, eqFn);
204+
public getSelectiveOf<T extends object | string | number> (obj: T, eqFn?: (v1: T, v2: T) => boolean):
205+
[undos: Array<[undoable: Undoable, index: number]>, redos: Array<[undoable: Undoable, index: number]>] {
206+
const fnFilter = (undoable: Undoable): boolean => hasSelectiveValue(undoable, obj, eqFn);
207+
const fnMap = (undoable: Undoable, index: number): [undoable: Undoable, index: number] => [undoable, index];
207208
return [
208-
this.undos.filter(undoable => fn(undoable)),
209-
this.redos.filter(undoable => fn(undoable))
209+
this.undos
210+
.map((undoable, index) => fnMap(undoable, index))
211+
.filter(undoable => fnFilter(undoable[0])),
212+
this.redos
213+
.map((undoable, index) => fnMap(undoable, index))
214+
.filter(undoable => fnFilter(undoable[0]))
210215
];
211216
}
212217

test/command/selective.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("using a selective command", () => {
120120
expect(res).toHaveLength(2);
121121
expect(res[0]).toHaveLength(2);
122122
expect(res[1]).toHaveLength(0);
123-
expect(res[0]).toStrictEqual([cmd1, cmd2]);
123+
expect(res[0]).toStrictEqual([[cmd1, 0], [cmd2, 2]]);
124124
});
125125

126126
test("can find a selective command having inheritance", () => {
@@ -130,7 +130,7 @@ describe("using a selective command", () => {
130130
expect(res).toHaveLength(2);
131131
expect(res[0]).toHaveLength(1);
132132
expect(res[1]).toHaveLength(0);
133-
expect(res[0]).toStrictEqual([cmd3]);
133+
expect(res[0]).toStrictEqual([[cmd3, 1]]);
134134
});
135135

136136
test("can find a selective command having several keys", () => {
@@ -140,7 +140,7 @@ describe("using a selective command", () => {
140140
expect(res).toHaveLength(2);
141141
expect(res[0]).toHaveLength(1);
142142
expect(res[1]).toHaveLength(0);
143-
expect(res[0]).toStrictEqual([cmd4]);
143+
expect(res[0]).toStrictEqual([[cmd4, 0]]);
144144
});
145145

146146
test("works when the key is modified", () => {
@@ -153,7 +153,7 @@ describe("using a selective command", () => {
153153
expect(res).toHaveLength(2);
154154
expect(res[0]).toHaveLength(3);
155155
expect(res[1]).toHaveLength(0);
156-
expect(res[0]).toStrictEqual([cmd2, cmd1, cmd3]);
156+
expect(res[0]).toStrictEqual([[cmd2, 0], [cmd1, 1], [cmd3, 2]]);
157157
});
158158

159159
test("works when keys are different", () => {
@@ -170,7 +170,7 @@ describe("using a selective command", () => {
170170
const res = history.getSelectiveOf(key);
171171
expect(res).toHaveLength(2);
172172
expect(res[1]).toHaveLength(0);
173-
expect(res[0]).toStrictEqual([cmd1]);
173+
expect(res[0]).toStrictEqual([[cmd1, 0]]);
174174
});
175175

176176
test("works when undo", () => {
@@ -184,8 +184,8 @@ describe("using a selective command", () => {
184184
expect(res).toHaveLength(2);
185185
expect(res[0]).toHaveLength(2);
186186
expect(res[1]).toHaveLength(1);
187-
expect(res[0]).toStrictEqual([cmd2, cmd1]);
188-
expect(res[1]).toStrictEqual([cmd3]);
187+
expect(res[0]).toStrictEqual([[cmd2, 0], [cmd1, 1]]);
188+
expect(res[1]).toStrictEqual([[cmd3, 0]]);
189189
});
190190

191191
test("works when full undo", () => {
@@ -196,7 +196,7 @@ describe("using a selective command", () => {
196196
const res = history.getSelectiveOf(key);
197197
expect(res).toHaveLength(2);
198198
expect(res[0]).toHaveLength(0);
199-
expect(res[1]).toStrictEqual([cmd1, cmd2]);
199+
expect(res[1]).toStrictEqual([[cmd1, 0], [cmd2, 1]]);
200200
});
201201

202202
test("selective works with string keys", () => {
@@ -205,7 +205,7 @@ describe("using a selective command", () => {
205205
history.add(cmd1);
206206
const res = history.getSelectiveOf("foo");
207207
expect(res).toHaveLength(2);
208-
expect(res[0]).toStrictEqual([cmd1]);
208+
expect(res[0]).toStrictEqual([[cmd1, 1]]);
209209
expect(res[1]).toHaveLength(0);
210210
});
211211

@@ -215,7 +215,7 @@ describe("using a selective command", () => {
215215
history.add(cmd1);
216216
const res = history.getSelectiveOf(42);
217217
expect(res).toHaveLength(2);
218-
expect(res[0]).toStrictEqual([cmd2]);
218+
expect(res[0]).toStrictEqual([[cmd2, 0]]);
219219
expect(res[1]).toHaveLength(0);
220220
});
221221

@@ -301,7 +301,7 @@ describe("using a selective command", () => {
301301
expect(res).toHaveLength(2);
302302
expect(res[0]).toHaveLength(3);
303303
expect(res[1]).toHaveLength(0);
304-
expect(res[0]).toStrictEqual([cmd1, cmd4, cmd5]);
304+
expect(res[0]).toStrictEqual([[cmd1, 0], [cmd4, 3], [cmd5, 4]]);
305305
});
306306
});
307307
});

0 commit comments

Comments
 (0)