@erect/hook
v0.0.2
Published
A hook pattern for your project
Downloads
12
Maintainers
Readme
Erect Hook Utility
A hook utility for your project bootstrap
Key aspects
- Type definitions
- Easy to use
- Multiple purposes
HookFilterSync<T, P>
Filter a value synchronously
import { HookFilterSync } from '@erect/hook';
const hook = new HookFilterSync<number, { factor: number }>();
hook.addFilter('multiply', (value, { factor }) => value * factor);
hook.addFilter('divide', (value, { factor }) => value / 2);
const value = hook.filter(3, { factor: 4 });
console.log(value); // 6
HookFilter<T, P>
Filter a value asynchronously
import { HookFilter } from '@erect/hook';
const hook = new HookFilter<number, { factor: number }>();
hook.addFilter('multiply', (value, { factor }) => value * factor);
hook.addFilter('divide', (value, { factor }) => Promise.resolve(value / 2));
hook.filter(3, { factor: 4 })
.then(value => {
console.log(value); // 6
});
HookFilterListSync<T, P>
Filter a lsit of values synchronously
import { HookFilterList } from '@erect/hook';
const hook = new HookFilterList<string, {}>();
hook.append('b', () => 'b');
hook.prepend('c', () => 'c');
hook.addFilter('d', values => values.concat(['d']);
const values = hook.filter(['a'], {});
console.log(value); // ['c', 'a', 'b', 'd'];
HookFilterList<T, P>
Filter a lsit of values asynchronously
import { HookFilterListSync } from '@erect/hook';
const hook = new HookFilterListSync<string, {}>();
hook.append('b', () => 'b');
hook.prepend('c', () => 'c');
hook.addFilter('d', values => values.concat(['d']);
hook.filter(['a'], {})
.then(values => {
console.log(values); // ['c', 'a', 'b', 'd']
});
HookActionSync<T, P>
Execute an action against defined params synchronously
import { HookActionSync } from '@erect/hook';
const hook = new HookActionSync<number, { value: number, factor: number }>();
hook.addAction('multiply', ({ value, factor }) => value * factor);
hook.addAction('divide', ({ value, factor }) => value / factor);
const results = hook.do({ value: 4, factor: 2 });
console.log(results.multiply); // 12
console.log(results.divide); // 2
HookAction<T, P>
Execute an action against defined params asynchronously in parallel
import { HookAction } from '@erect/hook';
const hook = new HookAction<number, { value: number, factor: number }>();
hook.addAction('multiply', ({ value, factor }) => value * factor);
hook.addAction('divide', ({ value, factor }) => Promise.resolve(value / factor));
const results = await hook.do({ value: 4, factor: 2 });
console.log(results.multiply); // 12
console.log(results.divide); // 2
HookActionSeries<T, P>
Execute an action against defined params asynchronously in series
import { HookActionSeries } from '@erect/hook';
const hook = new HookActionSeries<number, { value: number, factor: number }>();
hook.addAction('multiply', ({ value, factor }) => value * factor);
hook.addAction('divide', ({ value, factor }) => Promise.resolve(value / factor));
const results = await hook.do({ value: 4, factor: 2 });
console.log(results.multiply); // 12
console.log(results.divide); // 2