From ea0d4272dfba400ad6b65e9efdc1ac633b31fa2e Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 19 Oct 2024 19:51:49 +1300 Subject: [PATCH] fix: Fix #100 the return type error in the find method's callback function in IterableEntryBase. Add test cases for HashMap and Stack. --- .../base/iterable-entry-base.ts | 2 +- .../data-structures/hash/hash-map.test.ts | 61 ++++++++++++++++++- test/unit/data-structures/stack/stack.test.ts | 11 ++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/data-structures/base/iterable-entry-base.ts b/src/data-structures/base/iterable-entry-base.ts index 85b1446..f2c84a3 100644 --- a/src/data-structures/base/iterable-entry-base.ts +++ b/src/data-structures/base/iterable-entry-base.ts @@ -197,7 +197,7 @@ export abstract class IterableEntryBase { * the provided callback function. If no element satisfies the callback function, `undefined` is * returned. */ - find(callbackfn: EntryCallback, thisArg?: any): [K, V] | undefined { + find(callbackfn: EntryCallback, thisArg?: any): [K, V] | undefined { let index = 0; for (const item of this) { const [key, value] = item; diff --git a/test/unit/data-structures/hash/hash-map.test.ts b/test/unit/data-structures/hash/hash-map.test.ts index 55326a0..7269577 100644 --- a/test/unit/data-structures/hash/hash-map.test.ts +++ b/test/unit/data-structures/hash/hash-map.test.ts @@ -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', () => { @@ -302,7 +304,7 @@ describe('HashMap', () => { }); describe('HashMap HOF', () => { - let hashMap: HashMap; + let hashMap: HashMap; beforeEach(() => { hashMap = new HashMap(); @@ -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 }); + }); }); }); diff --git a/test/unit/data-structures/stack/stack.test.ts b/test/unit/data-structures/stack/stack.test.ts index ede31ac..a19a2d0 100644 --- a/test/unit/data-structures/stack/stack.test.ts +++ b/test/unit/data-structures/stack/stack.test.ts @@ -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'); + }); });