Installation
npm i @teamawesome/transform
Usage
import Pipe from '@teamawesome/transform'
const p = new Pipe({
first: () => {},
});
p.after('first', 'afterFirst', () => {});
p.before('first', 'beforeFirst', () => {});
const value = p.transform();
Class
Constructor
Can be given an iterable with entries, an object, or just a function.
new Pipe({
first: () => {},
second: () => {}
});
new Pipe(function main () {
});
new Pipe(new Map(...));
new Pipe(otherPipe);
insert, before, after
const key = {};
const func = () => {};
pipe.insert('neighbour', key, func);
pipe.insert('neighbour', func, func);
pipe.insert('neighbour', func);
transform
import {Pipe, Arguments} from '@teamwesome/transform';
const pipe = new Pipe([
(a, b) => a + b,
(sum) => sum * 2,
(product) => product / 2,
]);
pipe.transform(2, 4);
const pipe = new Pipe([
(obj) => {
const keys = Object.keys(obj);
const values = Object.values(obj);
return new Arguments(keys, values);
},
(keys, values) => {
return new Arguments(
keys.includes('hello'),
values.includes('world')
)
},
]);
pipe.transform({
hello: 'planet',
});
Callback
A handy utility is pipe
to easily generate callbacks. The following are roughly equivalant.
import {pipe} from '@teamwesome/transform';
const callback = pipe();
const p = new Pipe();
const callback = p.transform.bind(pipe)
However, the callbacks from pipe are pipes themselves.
import {pipe} from '@teamwesome/transform';
const callback = pipe({
main: () => {}
});
callback.insert('main', () => {});
callback.transform('sure', 'why not');
callback instanceof Pipe;
callback instanceof Function;
typeof callback === 'function'
All methods
export default class Pipe implements Iterable<[any, Hook]> {
protected order: any[];
protected readonly hooks: Map<any, Hook>;
constructor(entries?: Entries | object | Hook);
get(key: any): Hook | undefined;
set(key: any, value: Hook): this;
has(key: any): boolean;
delete(key: any): boolean;
clear(): void;
insert(neighbour: any, key: any, value?: Hook, insertAfter?: boolean): this;
before(neighbour: any, key: any, value?: Hook): this;
after(neighbour: any, key: any, value?: Hook): this;
transform(...args: any[]): Promise<any> | any;
forEach(callback: ForEachCallback, thisArg?: any): void;
[Symbol.iterator](): IterableIterator<[any, Hook]>;
entries(): Iterable<[any, Hook]>;
keys(): Iterable<any>;
values(): Iterable<Hook>;
get size(): number;
}
export interface Entries {
entries(): IterableIterator<[any, Hook]>;
}
export declare type Hook = (...args: any[]) => any;
export declare type ForEachCallback = (key: any, hook: Hook, pipe: Pipe) => void;