diff --git a/src/data-structures/base/iterable-entry-base.ts b/src/data-structures/base/iterable-entry-base.ts index 522fb81..d156731 100644 --- a/src/data-structures/base/iterable-entry-base.ts +++ b/src/data-structures/base/iterable-entry-base.ts @@ -70,7 +70,7 @@ export abstract class IterableEntryBase { every(predicate: EntryCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { - if (!predicate.call(thisArg, item[1], item[0], index++, this)) { + if (!predicate.call(thisArg, item[0], item[1], index++, this)) { return false; } } @@ -95,7 +95,7 @@ export abstract class IterableEntryBase { some(predicate: EntryCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { - if (predicate.call(thisArg, item[1], item[0], index++, this)) { + if (predicate.call(thisArg, item[0], item[1], index++, this)) { return true; } } @@ -119,7 +119,7 @@ export abstract class IterableEntryBase { let index = 0; for (const item of this) { const [key, value] = item; - callbackfn.call(thisArg, value, key, index++, this); + callbackfn.call(thisArg, key, value, index++, this); } } @@ -144,7 +144,7 @@ export abstract class IterableEntryBase { let index = 0; for (const item of this) { const [key, value] = item; - if (callbackfn.call(thisArg, value, key, index++, this)) return item; + if (callbackfn.call(thisArg, key, value, index++, this)) return item; } return; } diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index 5d5858b..66334d0 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -287,7 +287,7 @@ export class HashMap extends IterableEntryBase(); let index = 0; for (const [key, value] of this) { - resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this)); + resultMap.set(key, callbackfn.call(thisArg, key, value, index++, this)); } return resultMap; } @@ -312,7 +312,7 @@ export class HashMap extends IterableEntryBase(); let index = 0; for (const [key, value] of this) { - if (predicate.call(thisArg, value, key, index++, this)) { + if (predicate.call(thisArg, key, value, index++, this)) { filteredMap.set(key, value); } } @@ -826,7 +826,7 @@ export class LinkedHashMap extends IterableEntryBa const filteredMap = new LinkedHashMap(); let index = 0; for (const [key, value] of this) { - if (predicate.call(thisArg, value, key, index, this)) { + if (predicate.call(thisArg, key, value, index, this)) { filteredMap.set(key, value); } index++; @@ -851,12 +851,12 @@ export class LinkedHashMap extends IterableEntryBa * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback * function. */ - map(callback: EntryCallback, thisArg?: any): LinkedHashMap { - const mappedMap = new LinkedHashMap(); + map(callback: EntryCallback, thisArg?: any): LinkedHashMap { + const mappedMap = new LinkedHashMap(); let index = 0; for (const [key, value] of this) { - const newValue = callback.call(thisArg, value, key, index, this); - mappedMap.set(key, newValue); + const [newKey, newValue] = callback.call(thisArg, key, value, index, this); + mappedMap.set(newKey, newValue); index++; } return mappedMap; diff --git a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts index d2128b7..8389c28 100644 --- a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts @@ -626,14 +626,14 @@ describe('AVLTreeMultiMap iterative methods test', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - treeMM.forEach((value, key) => { - mockCallback(value, key); + treeMM.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[1]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[1]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new tree with filtered elements', () => { diff --git a/test/unit/data-structures/binary-tree/avl-tree.test.ts b/test/unit/data-structures/binary-tree/avl-tree.test.ts index 59d09ef..edb1120 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -386,14 +386,14 @@ describe('AVLTree iterative methods test', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - avl.forEach((value, key) => { - mockCallback(value, key); + avl.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[1]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[1]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new tree with filtered elements', () => { diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts index 3dd2a4f..6b7e784 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -1342,14 +1342,14 @@ describe('BinaryTree iterative methods test', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - binaryTree.forEach((value, key) => { - mockCallback(value, key); + binaryTree.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[1]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[1]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new tree with filtered elements', () => { diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index 54aa66e..eb9e537 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -1110,14 +1110,14 @@ describe('BST iterative methods test', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - bst.forEach((value, key) => { - mockCallback(value, key); + bst.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[1]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[1]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new tree with filtered elements', () => { diff --git a/test/unit/data-structures/binary-tree/red-black-tree.test.ts b/test/unit/data-structures/binary-tree/red-black-tree.test.ts index c9247b8..e923205 100644 --- a/test/unit/data-structures/binary-tree/red-black-tree.test.ts +++ b/test/unit/data-structures/binary-tree/red-black-tree.test.ts @@ -648,14 +648,14 @@ describe('RedBlackTree 2', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - rbTree.forEach((value, key) => { - mockCallback(value, key); + rbTree.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[1]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[1]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new rbTree with filtered elements', () => { diff --git a/test/unit/data-structures/binary-tree/tree-multi-map.test.ts b/test/unit/data-structures/binary-tree/tree-multi-map.test.ts index b0b5693..ab43c41 100644 --- a/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multi-map.test.ts @@ -764,14 +764,14 @@ describe('TreeMultiMap iterative methods test', () => { it('forEach should iterate over all elements', () => { const mockCallback = jest.fn(); - treeMM.forEach((value, key) => { - mockCallback(value, key); + treeMM.forEach((key, value) => { + mockCallback(key, value); }); expect(mockCallback.mock.calls.length).toBe(3); - expect(mockCallback.mock.calls[0]).toEqual(['a', 1]); - expect(mockCallback.mock.calls[1]).toEqual(['b', 2]); - expect(mockCallback.mock.calls[2]).toEqual(['c', 3]); + expect(mockCallback.mock.calls[0]).toEqual([1, 'a']); + expect(mockCallback.mock.calls[1]).toEqual([2, 'b']); + expect(mockCallback.mock.calls[2]).toEqual([3, 'c']); }); it('filter should return a new tree with filtered elements', () => { diff --git a/test/unit/data-structures/graph/directed-graph.test.ts b/test/unit/data-structures/graph/directed-graph.test.ts index 44c5f51..a7780b4 100644 --- a/test/unit/data-structures/graph/directed-graph.test.ts +++ b/test/unit/data-structures/graph/directed-graph.test.ts @@ -680,7 +680,7 @@ describe('DirectedGraph iterative Methods', () => { it('forEach should apply a function to each vertex', () => { const result: VertexKey[] = []; - graph.forEach((value, key) => key && result.push(key)); + graph.forEach(key => key && result.push(key)); expect(result).toEqual(vertexMap); }); diff --git a/test/unit/data-structures/hash/hash-map.test.ts b/test/unit/data-structures/hash/hash-map.test.ts index ef29f4b..9123563 100644 --- a/test/unit/data-structures/hash/hash-map.test.ts +++ b/test/unit/data-structures/hash/hash-map.test.ts @@ -328,7 +328,7 @@ describe('HashMap', () => { }); it('some() returns true if any element matches the condition', () => { - expect(hashMap.some((value, key) => key === 'key1')).toBe(true); + expect(hashMap.some(key => key === 'key1')).toBe(true); }); it('forEach() should execute a function for each element', () => { @@ -338,12 +338,12 @@ describe('HashMap', () => { }); it('map() should transform each element', () => { - const newHashMap = hashMap.map(value => value.toUpperCase()); + const newHashMap = hashMap.map((key, value) => value.toUpperCase()); expect(newHashMap.get('key1')).toBe('VALUE1'); }); it('filter() should remove elements that do not match the condition', () => { - const filteredHashMap = hashMap.filter((value, key) => key !== 'key1'); + const filteredHashMap = hashMap.filter((key, value) => key !== 'key1'); expect(filteredHashMap.has('key1')).toBe(false); }); @@ -361,28 +361,28 @@ describe('HashMap', () => { }); it('should find', () => { - const found = hashMap.find(value => value === 'value1'); + const found = hashMap.find((key, value) => value === 'value1'); expect(found).toEqual(['key1', 'value1']); const notFound = hashMap.find(value => value === 'value6'); expect(notFound).toEqual(undefined); }); it('should every', () => { - const isEvery = hashMap.every(value => value.substring(0, 5) === 'value'); + const isEvery = hashMap.every((key, value) => value.substring(0, 5) === 'value'); expect(isEvery).toEqual(true); - const isEvery4 = hashMap.every(value => value.substring(0, 4) === 'value'); + const isEvery4 = hashMap.every((key, value) => value.substring(0, 4) === 'value'); expect(isEvery4).toEqual(false); }); it('should some', () => { - const isSome = hashMap.some(value => value.substring(5, 6) === '2'); + const isSome = hashMap.some((key, value) => value.substring(5, 6) === '2'); expect(isSome).toEqual(true); - const isSome4 = hashMap.some(value => value.substring(0, 5) === 'value'); + const isSome4 = hashMap.some((key, value) => value.substring(0, 5) === 'value'); expect(isSome4).toEqual(true); }); it('should forEach', () => { - hashMap.forEach((value, key, index) => expect(value.substring(5, 6)).toBe(String(index + 1))); + hashMap.forEach((key, value, index) => expect(value.substring(5, 6)).toBe(String(index + 1))); }); it('should entries', () => { @@ -817,7 +817,7 @@ describe('LinkedHashMap', () => { }); it('some() returns true if any element matches the condition', () => { - expect(hashMap.some((value, key) => key === 'key1')).toBe(true); + expect(hashMap.some(key => key === 'key1')).toBe(true); }); it('forEach() should execute a function for each element', () => { @@ -827,12 +827,12 @@ describe('LinkedHashMap', () => { }); it('map() should transform each element', () => { - const newHashMap = hashMap.map(value => value.toUpperCase()); + const newHashMap = hashMap.map((key, value) => [key, value.toUpperCase()]); expect(newHashMap.get('key1')).toBe('VALUE1'); }); it('filter() should remove elements that do not match the condition', () => { - const filteredHashMap = hashMap.filter((value, key) => key !== 'key1'); + const filteredHashMap = hashMap.filter(key => key !== 'key1'); expect(filteredHashMap.has('key1')).toBe(false); });