Skip to content

Commit

Permalink
fix: Fix #100 the return type error in the find method's callback fun…
Browse files Browse the repository at this point in the history
…ction in IterableEntryBase.

Add test cases for HashMap and Stack.
  • Loading branch information
zrwusa committed Oct 19, 2024
1 parent 07dd75d commit ea0d427
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/data-structures/base/iterable-entry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
* the provided callback function. If no element satisfies the callback function, `undefined` is
* returned.
*/
find(callbackfn: EntryCallback<K, V, [K, V]>, thisArg?: any): [K, V] | undefined {
find(callbackfn: EntryCallback<K, V, boolean>, thisArg?: any): [K, V] | undefined {
let index = 0;
for (const item of this) {
const [key, value] = item;
Expand Down
61 changes: 60 additions & 1 deletion test/unit/data-structures/hash/hash-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ describe('HashMap', () => {
it('should add a key-value pair', () => {
hashMap.set('key1', 'value1');
expect(hashMap.get('key1')).toBe('value1');
expect(hashMap.hasValue('value1')).toBe(true);
expect(hashMap.hasValue('value2')).toBe(false);
});

it('should handle object keys correctly', () => {
Expand Down Expand Up @@ -302,7 +304,7 @@ describe('HashMap', () => {
});

describe('HashMap HOF', () => {
let hashMap: HashMap;
let hashMap: HashMap<string, string>;

beforeEach(() => {
hashMap = new HashMap<string, string>();
Expand Down Expand Up @@ -339,6 +341,63 @@ describe('HashMap', () => {
const result = hashMap.reduce((acc, value) => acc + value, '');
expect(result).toBe('value1value2value3');
});

it('should spread in an array', () => {
expect([...hashMap]).toEqual([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3']
]);
});

it('should find', () => {
const found = hashMap.find(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');
expect(isEvery).toEqual(true);
const isEvery4 = hashMap.every(value => value.substring(0, 4) === 'value');
expect(isEvery4).toEqual(false);
});

it('should some', () => {
const isSome = hashMap.some(value => value.substring(5, 6) === '2');
expect(isSome).toEqual(true);
const isSome4 = hashMap.some(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)));
});

it('should entries', () => {
const entries = hashMap.entries();
expect(entries.next()).toEqual({ done: false, value: ['key1', 'value1'] });
expect(entries.next()).toEqual({ done: false, value: ['key2', 'value2'] });
expect(entries.next()).toEqual({ done: false, value: ['key3', 'value3'] });
expect(entries.next()).toEqual({ done: true, value: undefined });
});

it('should keys', () => {
const keys = hashMap.keys();
expect(keys.next()).toEqual({ done: false, value: 'key1' });
expect(keys.next()).toEqual({ done: false, value: 'key2' });
expect(keys.next()).toEqual({ done: false, value: 'key3' });
expect(keys.next()).toEqual({ done: true, value: undefined });
});

it('should values', () => {
const values = hashMap.values();
expect(values.next()).toEqual({ done: false, value: 'value1' });
expect(values.next()).toEqual({ done: false, value: 'value2' });
expect(values.next()).toEqual({ done: false, value: 'value3' });
expect(values.next()).toEqual({ done: true, value: undefined });
});
});
});

Expand Down
11 changes: 11 additions & 0 deletions test/unit/data-structures/stack/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,15 @@ describe('Stack iterative methods', () => {
expect(stack.size).toBe(4);
expect([...stack]).toEqual([1, 2, 5, 3]);
});

it('should iterable-element-base toElementFn', () => {
const stack = new Stack<{ key: number }>([1, 2, 5, 3], { toElementFn: key => ({ key }) });
expect([...stack]).toEqual([{ key: 1 }, { key: 2 }, { key: 5 }, { key: 3 }]);
expect(() => {
new Stack<{ key: number }>([1, 2, 5, 3], {
// @ts-ignore
toElementFn: {}
});
}).toThrow('toElementFn must be a function type');
});
});

0 comments on commit ea0d427

Please sign in to comment.