Files
lik/test/test.fastmap.both.ts

152 lines
4.3 KiB
TypeScript

import { tap, expect } from '@push.rocks/tapbundle';
import * as lik from '../ts/index.js';
tap.test('should create a valid fastmap', async () => {
const fastmap = new lik.FastMap();
expect(fastmap).toBeInstanceOf(lik.FastMap);
expect(fastmap.size).toEqual(0);
});
tap.test('should find an entry', async () => {
const fastmap = new lik.FastMap<{
value1: string;
value2: string;
}>();
fastmap.addToMap('heythere', {
value1: 'heyho',
value2: 'heyho2',
});
fastmap.addToMap('heythere2', {
value1: 'heyho3',
value2: 'heyho4',
});
const result = await fastmap.find(async (itemArg) => {
return itemArg.value2 === 'heyho4';
});
expect(result.value1).toEqual('heyho3');
});
tap.test('isUniqueKey returns true for new key, false for existing', async () => {
const map = new lik.FastMap<string>();
expect(map.isUniqueKey('foo')).toBeTrue();
map.addToMap('foo', 'bar');
expect(map.isUniqueKey('foo')).toBeFalse();
});
tap.test('has() works correctly', async () => {
const map = new lik.FastMap<number>();
expect(map.has('x')).toBeFalse();
map.addToMap('x', 42);
expect(map.has('x')).toBeTrue();
});
tap.test('addToMap with force overwrites existing key', async () => {
const map = new lik.FastMap<string>();
map.addToMap('key1', 'original');
const withoutForce = map.addToMap('key1', 'new');
expect(withoutForce).toBeFalse();
expect(map.getByKey('key1')).toEqual('original');
const withForce = map.addToMap('key1', 'new', { force: true });
expect(withForce).toBeTrue();
expect(map.getByKey('key1')).toEqual('new');
});
tap.test('getByKey returns undefined for missing key', async () => {
const map = new lik.FastMap<string>();
expect(map.getByKey('nonexistent')).toBeUndefined();
});
tap.test('removeFromMap removes and returns item', async () => {
const map = new lik.FastMap<string>();
map.addToMap('a', 'hello');
const removed = map.removeFromMap('a');
expect(removed).toEqual('hello');
expect(map.has('a')).toBeFalse();
expect(map.size).toEqual(0);
});
tap.test('getKeys returns all keys', async () => {
const map = new lik.FastMap<number>();
map.addToMap('a', 1);
map.addToMap('b', 2);
map.addToMap('c', 3);
const keys = map.getKeys();
expect(keys.length).toEqual(3);
expect(keys).toContain('a');
expect(keys).toContain('b');
expect(keys).toContain('c');
});
tap.test('values returns all values', async () => {
const map = new lik.FastMap<number>();
map.addToMap('x', 10);
map.addToMap('y', 20);
const vals = map.values();
expect(vals.length).toEqual(2);
expect(vals).toContain(10);
expect(vals).toContain(20);
});
tap.test('entries returns key-value pairs', async () => {
const map = new lik.FastMap<string>();
map.addToMap('k1', 'v1');
const entries = map.entries();
expect(entries.length).toEqual(1);
expect(entries[0][0]).toEqual('k1');
expect(entries[0][1]).toEqual('v1');
});
tap.test('clean empties the map', async () => {
const map = new lik.FastMap<string>();
map.addToMap('a', 'b');
map.addToMap('c', 'd');
map.clean();
expect(map.size).toEqual(0);
expect(map.getKeys().length).toEqual(0);
});
tap.test('concat merges two maps', async () => {
const map1 = new lik.FastMap<number>();
map1.addToMap('a', 1);
const map2 = new lik.FastMap<number>();
map2.addToMap('b', 2);
const merged = map1.concat(map2);
expect(merged.size).toEqual(2);
expect(merged.getByKey('a')).toEqual(1);
expect(merged.getByKey('b')).toEqual(2);
});
tap.test('addAllFromOther merges in place', async () => {
const map1 = new lik.FastMap<number>();
map1.addToMap('a', 1);
const map2 = new lik.FastMap<number>();
map2.addToMap('b', 2);
map2.addToMap('a', 99);
map1.addAllFromOther(map2);
expect(map1.size).toEqual(2);
expect(map1.getByKey('a')).toEqual(99);
expect(map1.getByKey('b')).toEqual(2);
});
tap.test('Symbol.iterator works with for...of', async () => {
const map = new lik.FastMap<number>();
map.addToMap('x', 1);
map.addToMap('y', 2);
const collected: [string, number][] = [];
for (const entry of map) {
collected.push(entry);
}
expect(collected.length).toEqual(2);
});
tap.test('find returns undefined when no match', async () => {
const map = new lik.FastMap<number>();
map.addToMap('a', 1);
const result = await map.find(async (item) => item === 999);
expect(result).toBeUndefined();
});
export default tap.start();