feat(collections): add new collection APIs, iterator support, and tree serialization utilities

This commit is contained in:
2026-03-22 08:44:49 +00:00
parent 20182a00f8
commit f4db131ede
23 changed files with 2251 additions and 2657 deletions

View File

@@ -4,20 +4,78 @@ import { expect, tap } from '@push.rocks/tapbundle';
// import the module
import * as lik from '../ts/index.js';
let testTimedAggregator: lik.TimedAggregtor<string>;
tap.test('should create a timed aggregaotor', async (tools) => {
testTimedAggregator = new lik.TimedAggregtor<string>({
aggregationIntervalInMillis: 1000,
tap.test('should create a timed aggregator and aggregate items', async (tools) => {
const batches: string[][] = [];
const aggregator = new lik.TimedAggregtor<string>({
aggregationIntervalInMillis: 200,
functionForAggregation: (aggregation) => {
console.log(aggregation);
batches.push(aggregation);
},
});
testTimedAggregator.add('This');
testTimedAggregator.add('is a whole sentence.');
await tools.delayFor(1001);
testTimedAggregator.add('This one is another.');
await tools.delayFor(2000);
aggregator.add('first');
aggregator.add('second');
await tools.delayFor(300);
expect(batches.length).toEqual(1);
expect(batches[0]).toContain('first');
expect(batches[0]).toContain('second');
aggregator.add('third');
await tools.delayFor(300);
expect(batches.length).toEqual(2);
expect(batches[1]).toContain('third');
aggregator.stop();
});
tap.test('stop() prevents further aggregation', async (tools) => {
const batches: number[][] = [];
const aggregator = new lik.TimedAggregtor<number>({
aggregationIntervalInMillis: 100,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add(1);
aggregator.stop();
aggregator.add(2);
await tools.delayFor(200);
expect(batches.length).toEqual(0);
});
tap.test('stop(true) flushes remaining items', async () => {
const batches: number[][] = [];
const aggregator = new lik.TimedAggregtor<number>({
aggregationIntervalInMillis: 5000,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add(10);
aggregator.add(20);
aggregator.stop(true);
expect(batches.length).toEqual(1);
expect(batches[0]).toEqual([10, 20]);
});
tap.test('restart allows adding again after stop', async (tools) => {
const batches: string[][] = [];
const aggregator = new lik.TimedAggregtor<string>({
aggregationIntervalInMillis: 100,
functionForAggregation: (items) => {
batches.push(items);
},
});
aggregator.add('a');
aggregator.stop();
aggregator.restart();
aggregator.add('b');
await tools.delayFor(200);
expect(batches.length).toEqual(1);
expect(batches[0]).toContain('b');
aggregator.stop();
});
tap.test('TimedAggregator alias exists', async () => {
expect(lik.TimedAggregator).toEqual(lik.TimedAggregtor);
});
export default tap.start();