mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-13 01:04:03 +00:00
fix: Fix #100 the return type error in the find method's callback function in IterableEntryBase.
Add test cases for HashMap and Stack.
This commit is contained in:
parent
07dd75de70
commit
ea0d4272df
|
@ -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;
|
||||
|
|
|
@ -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<string, string>;
|
||||
|
||||
beforeEach(() => {
|
||||
hashMap = new HashMap<string, string>();
|
||||
|
@ -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 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue