Commit 07cca1d8 authored by Tobinsk's avatar Tobinsk
Browse files

Resolve "Add StreamFilterTransformer"

parent 2c5e9723
/// <reference types="node" />
import { Transform, TransformOptions } from 'stream';
/**
* Options interface
*/
export interface IStreamFilterOptions extends TransformOptions {
filter: (chunk: any, encode: string) => [null | string, any];
}
/**
* Filter a stream of data with a simple function
*/
declare class StreamFilterTransformer extends Transform {
private readonly filter;
constructor(options: IStreamFilterOptions);
/**
* @inheritDoc
* @param chunk
* @param encoding
* @param callback
* @private
*/
_transform(chunk: any, encoding: any, callback: any): void;
}
export default StreamFilterTransformer;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
/**
* Filter a stream of data with a simple function
*/
class StreamFilterTransformer extends stream_1.Transform {
constructor(options) {
super(options);
this.filter = options.filter;
}
/**
* @inheritDoc
* @param chunk
* @param encoding
* @param callback
* @private
*/
_transform(chunk, encoding, callback) {
callback(...this.filter(chunk, encoding));
}
}
exports.default = StreamFilterTransformer;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RyZWFtLWZpbHRlci10cmFuc2Zvcm1lci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zdHJlYW0vc3RyZWFtLWZpbHRlci10cmFuc2Zvcm1lci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLG1DQUFtRDtBQVNuRDs7R0FFRztBQUNILDZCQUE4QixTQUFRLGtCQUFTO0lBRTNDLFlBQVksT0FBNkI7UUFDckMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ2YsSUFBSSxDQUFDLE1BQU0sR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDO0lBQ2pDLENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSSxVQUFVLENBQUMsS0FBSyxFQUFFLFFBQVEsRUFBRSxRQUFRO1FBQ3ZDLFFBQVEsQ0FBQyxHQUFHLElBQUksQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUFFLFFBQVEsQ0FBQyxDQUFDLENBQUM7SUFDOUMsQ0FBQztDQUNKO0FBRUQsa0JBQWUsdUJBQXVCLENBQUMifQ==
\ No newline at end of file
......@@ -38,4 +38,4 @@ exports.default = UrlBuilder;
// More configurable. We like to generate those links from a small interface. So we cann simply add and delete them
// we wana have a file from which we can load stuff into memory. We do not want to have DB overhead
// perhaps e need a rest interface. Otherwise we will have problem to "live" reload the config
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXJpLWJ1aWxkZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdXJpLWJ1aWxkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxnQ0FBaUM7QUFDakMseUJBQTRCO0FBc0I1QjtJQVFFLFlBQVksTUFBTTtRQUNoQixJQUFJLENBQUMsTUFBTSxHQUFHLE1BQU0sQ0FBQztRQUNyQix1Q0FBdUM7UUFDdkMsSUFBSTtZQUNGLElBQUksQ0FBQyxTQUFTLEdBQUcsSUFBSSxDQUFDLFFBQVEsQ0FBQyxFQUFFLENBQUMsWUFBWSxDQUFDLFNBQVMsR0FBQyxrQkFBa0IsRUFBRSxNQUFNLENBQUMsQ0FBQyxDQUFDO1NBQ3ZGO1FBQUMsT0FBTyxDQUFDLEVBQUU7WUFDVixNQUFNLENBQUMsS0FBSyxDQUFDLHFDQUFxQyxDQUFDLENBQUE7U0FDcEQ7SUFDSCxDQUFDO0lBQ0Q7Ozs7O09BS0c7SUFDSCxXQUFXLENBQUMsUUFBZ0IsRUFBRSxVQUFrQjtRQUM5Qyw2QkFBNkI7UUFDN0IsTUFBTSxLQUFLLEdBQUcsSUFBSSxDQUFDLFNBQVMsQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDLEVBQUUsRUFBRTtZQUMzQyxPQUFPLENBQUMsQ0FBQyxJQU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bkgsbUdBQW1HO0FBQ25HLDhGQUE4RiJ9
\ No newline at end of file
import {Transform, TransformOptions} from 'stream';
/**
* Options interface
*/
export interface IStreamFilterOptions extends TransformOptions {
filter: (chunk: any, encode: string) => [null | string, any];
}
/**
* Filter a stream of data with a simple function
*/
class StreamFilterTransformer extends Transform {
private readonly filter;
constructor(options: IStreamFilterOptions ) {
super(options);
this.filter = options.filter;
}
/**
* @inheritDoc
* @param chunk
* @param encoding
* @param callback
* @private
*/
public _transform(chunk, encoding, callback) {
callback(...this.filter(chunk, encoding));
}
}
export default StreamFilterTransformer;
......@@ -44,7 +44,7 @@ export default class UrlBuilder {
* @param {string} identifier
* @returns {string | null}
*/
urlResolver(provider: string, identifier: string): string | null {
public urlResolver(provider: string, identifier: string): string | null {
// check for a the identifier
const index = this.providers.findIndex((p) => {
return p.slug === provider || p.alias.indexOf(provider) > -1;
......@@ -61,7 +61,6 @@ export default class UrlBuilder {
}
}
// More configurable. We like to generate those links from a small interface. So we cann simply add and delete them
// we wana have a file from which we can load stuff into memory. We do not want to have DB overhead
// perhaps e need a rest interface. Otherwise we will have problem to "live" reload the config
import chai = require('chai');
import {describe, it} from 'mocha';
import StreamFilterTransformer from '../../src/stream/stream-filter-transformer';
process.env.NODE_ENV = 'test';
describe('Test stream-filter-transformer', () => {
before((done) => {
chai.should();
done();
});
it('Try to transform a stream with a function', (done) => {
const filter = (chunk: any, encode: string): [null, any] => {
chunk.total++;
return [null, chunk];
};
const stream = new StreamFilterTransformer({objectMode: true, filter});
// listener to check output
stream.on('data', (data: any) => {
data.total.should.be.equal(1);
done();
});
stream.write({total: 0});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment