Skip to content

Commit 829403d

Browse files
committed
fix: Adjust all entry callback functions to the [key, value] order.
1 parent 1a1ea21 commit 829403d

File tree

10 files changed

+54
-54
lines changed

10 files changed

+54
-54
lines changed

src/data-structures/base/iterable-entry-base.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
7070
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
7171
let index = 0;
7272
for (const item of this) {
73-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
73+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
7474
return false;
7575
}
7676
}
@@ -95,7 +95,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
9595
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
9696
let index = 0;
9797
for (const item of this) {
98-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
98+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
9999
return true;
100100
}
101101
}
@@ -119,7 +119,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
119119
let index = 0;
120120
for (const item of this) {
121121
const [key, value] = item;
122-
callbackfn.call(thisArg, value, key, index++, this);
122+
callbackfn.call(thisArg, key, value, index++, this);
123123
}
124124
}
125125

@@ -144,7 +144,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
144144
let index = 0;
145145
for (const item of this) {
146146
const [key, value] = item;
147-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
147+
if (callbackfn.call(thisArg, key, value, index++, this)) return item;
148148
}
149149
return;
150150
}

src/data-structures/hash/hash-map.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
287287
const resultMap = new HashMap<K, VM>();
288288
let index = 0;
289289
for (const [key, value] of this) {
290-
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
290+
resultMap.set(key, callbackfn.call(thisArg, key, value, index++, this));
291291
}
292292
return resultMap;
293293
}
@@ -312,7 +312,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
312312
const filteredMap = new HashMap<K, V>();
313313
let index = 0;
314314
for (const [key, value] of this) {
315-
if (predicate.call(thisArg, value, key, index++, this)) {
315+
if (predicate.call(thisArg, key, value, index++, this)) {
316316
filteredMap.set(key, value);
317317
}
318318
}
@@ -826,7 +826,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
826826
const filteredMap = new LinkedHashMap<K, V>();
827827
let index = 0;
828828
for (const [key, value] of this) {
829-
if (predicate.call(thisArg, value, key, index, this)) {
829+
if (predicate.call(thisArg, key, value, index, this)) {
830830
filteredMap.set(key, value);
831831
}
832832
index++;
@@ -851,12 +851,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
851851
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
852852
* function.
853853
*/
854-
map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
855-
const mappedMap = new LinkedHashMap<K, VM>();
854+
map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: any): LinkedHashMap<MK, MV> {
855+
const mappedMap = new LinkedHashMap<MK, MV>();
856856
let index = 0;
857857
for (const [key, value] of this) {
858-
const newValue = callback.call(thisArg, value, key, index, this);
859-
mappedMap.set(key, newValue);
858+
const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
859+
mappedMap.set(newKey, newValue);
860860
index++;
861861
}
862862
return mappedMap;

test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,14 @@ describe('AVLTreeMultiMap iterative methods test', () => {
626626

627627
it('forEach should iterate over all elements', () => {
628628
const mockCallback = jest.fn();
629-
treeMM.forEach((value, key) => {
630-
mockCallback(value, key);
629+
treeMM.forEach((key, value) => {
630+
mockCallback(key, value);
631631
});
632632

633633
expect(mockCallback.mock.calls.length).toBe(3);
634-
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
635-
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
636-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
634+
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
635+
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
636+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
637637
});
638638

639639
it('filter should return a new tree with filtered elements', () => {

test/unit/data-structures/binary-tree/avl-tree.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,14 @@ describe('AVLTree iterative methods test', () => {
386386

387387
it('forEach should iterate over all elements', () => {
388388
const mockCallback = jest.fn();
389-
avl.forEach((value, key) => {
390-
mockCallback(value, key);
389+
avl.forEach((key, value) => {
390+
mockCallback(key, value);
391391
});
392392

393393
expect(mockCallback.mock.calls.length).toBe(3);
394-
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
395-
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
396-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
394+
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
395+
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
396+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
397397
});
398398

399399
it('filter should return a new tree with filtered elements', () => {

test/unit/data-structures/binary-tree/binary-tree.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,14 +1342,14 @@ describe('BinaryTree iterative methods test', () => {
13421342

13431343
it('forEach should iterate over all elements', () => {
13441344
const mockCallback = jest.fn();
1345-
binaryTree.forEach((value, key) => {
1346-
mockCallback(value, key);
1345+
binaryTree.forEach((key, value) => {
1346+
mockCallback(key, value);
13471347
});
13481348

13491349
expect(mockCallback.mock.calls.length).toBe(3);
1350-
expect(mockCallback.mock.calls[0]).toEqual(['b', 2]);
1351-
expect(mockCallback.mock.calls[1]).toEqual(['a', 1]);
1352-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
1350+
expect(mockCallback.mock.calls[0]).toEqual([2, 'b']);
1351+
expect(mockCallback.mock.calls[1]).toEqual([1, 'a']);
1352+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
13531353
});
13541354

13551355
it('filter should return a new tree with filtered elements', () => {

test/unit/data-structures/binary-tree/bst.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,14 +1110,14 @@ describe('BST iterative methods test', () => {
11101110

11111111
it('forEach should iterate over all elements', () => {
11121112
const mockCallback = jest.fn();
1113-
bst.forEach((value, key) => {
1114-
mockCallback(value, key);
1113+
bst.forEach((key, value) => {
1114+
mockCallback(key, value);
11151115
});
11161116

11171117
expect(mockCallback.mock.calls.length).toBe(3);
1118-
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
1119-
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
1120-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
1118+
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
1119+
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
1120+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
11211121
});
11221122

11231123
it('filter should return a new tree with filtered elements', () => {

test/unit/data-structures/binary-tree/red-black-tree.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ describe('RedBlackTree 2', () => {
648648

649649
it('forEach should iterate over all elements', () => {
650650
const mockCallback = jest.fn();
651-
rbTree.forEach((value, key) => {
652-
mockCallback(value, key);
651+
rbTree.forEach((key, value) => {
652+
mockCallback(key, value);
653653
});
654654

655655
expect(mockCallback.mock.calls.length).toBe(3);
656-
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
657-
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
658-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
656+
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
657+
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
658+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
659659
});
660660

661661
it('filter should return a new rbTree with filtered elements', () => {

test/unit/data-structures/binary-tree/tree-multi-map.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,14 @@ describe('TreeMultiMap iterative methods test', () => {
764764

765765
it('forEach should iterate over all elements', () => {
766766
const mockCallback = jest.fn();
767-
treeMM.forEach((value, key) => {
768-
mockCallback(value, key);
767+
treeMM.forEach((key, value) => {
768+
mockCallback(key, value);
769769
});
770770

771771
expect(mockCallback.mock.calls.length).toBe(3);
772-
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
773-
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
774-
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
772+
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
773+
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
774+
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
775775
});
776776

777777
it('filter should return a new tree with filtered elements', () => {

test/unit/data-structures/graph/directed-graph.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ describe('DirectedGraph iterative Methods', () => {
680680

681681
it('forEach should apply a function to each vertex', () => {
682682
const result: VertexKey[] = [];
683-
graph.forEach((value, key) => key && result.push(key));
683+
graph.forEach(key => key && result.push(key));
684684
expect(result).toEqual(vertexMap);
685685
});
686686

test/unit/data-structures/hash/hash-map.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('HashMap', () => {
328328
});
329329

330330
it('some() returns true if any element matches the condition', () => {
331-
expect(hashMap.some((value, key) => key === 'key1')).toBe(true);
331+
expect(hashMap.some(key => key === 'key1')).toBe(true);
332332
});
333333

334334
it('forEach() should execute a function for each element', () => {
@@ -338,12 +338,12 @@ describe('HashMap', () => {
338338
});
339339

340340
it('map() should transform each element', () => {
341-
const newHashMap = hashMap.map(value => value.toUpperCase());
341+
const newHashMap = hashMap.map((key, value) => value.toUpperCase());
342342
expect(newHashMap.get('key1')).toBe('VALUE1');
343343
});
344344

345345
it('filter() should remove elements that do not match the condition', () => {
346-
const filteredHashMap = hashMap.filter((value, key) => key !== 'key1');
346+
const filteredHashMap = hashMap.filter((key, value) => key !== 'key1');
347347
expect(filteredHashMap.has('key1')).toBe(false);
348348
});
349349

@@ -361,28 +361,28 @@ describe('HashMap', () => {
361361
});
362362

363363
it('should find', () => {
364-
const found = hashMap.find(value => value === 'value1');
364+
const found = hashMap.find((key, value) => value === 'value1');
365365
expect(found).toEqual(['key1', 'value1']);
366366
const notFound = hashMap.find(value => value === 'value6');
367367
expect(notFound).toEqual(undefined);
368368
});
369369

370370
it('should every', () => {
371-
const isEvery = hashMap.every(value => value.substring(0, 5) === 'value');
371+
const isEvery = hashMap.every((key, value) => value.substring(0, 5) === 'value');
372372
expect(isEvery).toEqual(true);
373-
const isEvery4 = hashMap.every(value => value.substring(0, 4) === 'value');
373+
const isEvery4 = hashMap.every((key, value) => value.substring(0, 4) === 'value');
374374
expect(isEvery4).toEqual(false);
375375
});
376376

377377
it('should some', () => {
378-
const isSome = hashMap.some(value => value.substring(5, 6) === '2');
378+
const isSome = hashMap.some((key, value) => value.substring(5, 6) === '2');
379379
expect(isSome).toEqual(true);
380-
const isSome4 = hashMap.some(value => value.substring(0, 5) === 'value');
380+
const isSome4 = hashMap.some((key, value) => value.substring(0, 5) === 'value');
381381
expect(isSome4).toEqual(true);
382382
});
383383

384384
it('should forEach', () => {
385-
hashMap.forEach((value, key, index) => expect(value.substring(5, 6)).toBe(String(index + 1)));
385+
hashMap.forEach((key, value, index) => expect(value.substring(5, 6)).toBe(String(index + 1)));
386386
});
387387

388388
it('should entries', () => {
@@ -817,7 +817,7 @@ describe('LinkedHashMap', () => {
817817
});
818818

819819
it('some() returns true if any element matches the condition', () => {
820-
expect(hashMap.some((value, key) => key === 'key1')).toBe(true);
820+
expect(hashMap.some(key => key === 'key1')).toBe(true);
821821
});
822822

823823
it('forEach() should execute a function for each element', () => {
@@ -827,12 +827,12 @@ describe('LinkedHashMap', () => {
827827
});
828828

829829
it('map() should transform each element', () => {
830-
const newHashMap = hashMap.map(value => value.toUpperCase());
830+
const newHashMap = hashMap.map((key, value) => [key, value.toUpperCase()]);
831831
expect(newHashMap.get('key1')).toBe('VALUE1');
832832
});
833833

834834
it('filter() should remove elements that do not match the condition', () => {
835-
const filteredHashMap = hashMap.filter((value, key) => key !== 'key1');
835+
const filteredHashMap = hashMap.filter(key => key !== 'key1');
836836
expect(filteredHashMap.has('key1')).toBe(false);
837837
});
838838

0 commit comments

Comments
 (0)