mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-27 06:24:05 +00:00
fix: Adjust all entry callback functions to the [key, value] order.
This commit is contained in:
parent
1a1ea21444
commit
829403dc63
|
@ -70,7 +70,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|||
every(predicate: EntryCallback<K, V, boolean>, 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<K = any, V = any> {
|
|||
some(predicate: EntryCallback<K, V, boolean>, 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<K = any, V = any> {
|
|||
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<K = any, V = any> {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -287,7 +287,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
const resultMap = new HashMap<K, VM>();
|
||||
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<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
const filteredMap = new HashMap<K, V>();
|
||||
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<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|||
const filteredMap = new LinkedHashMap<K, V>();
|
||||
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<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|||
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
||||
* function.
|
||||
*/
|
||||
map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
|
||||
const mappedMap = new LinkedHashMap<K, VM>();
|
||||
map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: any): LinkedHashMap<MK, MV> {
|
||||
const mappedMap = new LinkedHashMap<MK, MV>();
|
||||
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;
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue