An independent SwitchMap, inspired by the switch map concept in RxJs.
This independent switch map is all purpose, composable, performant router.
// examples/version.ts
// $ npx ts-node examples/version.ts
import { SwitchMap } from '../dist';
interface Message {
version?: string;
data: string;
}
type Handler = (message: Message) => void;
const versions: Record<string, Handler> = {
init: ({ data }) => console.log(`${data}`),
pre1: ({ version, data }) => console.log(`got ${version}: ${data}`),
v1x: ({ version, data }) => console.log(`${version}: ${data}`),
v2x: ({ version, data }) => console.log(`received ${version}: ${data}`),
unrecognized: ({ version }) => console.log(`unsupported: ${version}`),
};
const dispatch = new SwitchMap((m: Message) => m.version || '0')
.value('0', versions.init)
.value('0.7', versions.pre1)
.match(/^1\.[0-7]$/, versions.v1x)
.value(['2.0', '2.1'], versions.v2x)
.default(versions.unrecognized);
dispatch.push({ data: 'blah blah' });
// blah blah
dispatch.push({ version: '1.1', data: 'blah blah' });
// 1.1: blah blah
dispatch.push({ version: '2.1', data: 'blah blah' });
// received 2.1: blah blah
dispatch.push({ version: '3.1', data: 'blah blah' });
// unsupported: 3.1
In your shell:
npm install switchmap
In your module:
import { SwitchMap } from 'switchmap';
The API documentation is generated from code by typedoc and hosted here. Read the docs.
Documentation is always a work in progress, let us know by creating an issue if you need a scenario documented.