Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
feat: pass this reference to transform methods
Browse files Browse the repository at this point in the history
Allows for deeper integration in implementations
  • Loading branch information
trs committed Jun 28, 2019
1 parent df647e2 commit 74335d5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/methods/chunkMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import { transform } from '..';

export function chunkMap<T, R>(
size: number,
method: (chunk: T[], index: number) => OrPromiseLike<R>,
method: (this: TransformTyped<T, R>, chunk: T[], index: number) => OrPromiseLike<R>,
options: TransformTypedOptions<T, R> = {}
): TransformTyped<T, R> {
let index = 0;
let chunk: T[] = [];

return transform(
async (item, encoding, push) => {
async function (item, encoding, push) {
chunk.push(item);
if (chunk.length >= size) {
const result = await method(chunk, index++);
const result = await method.call(this, chunk, index++);
push(result);
chunk = [];
}
},
async (push) => {
async function (push) {
if (chunk.length > 0) {
const result = await method(chunk, index++);
const result = await method.call(this, chunk, index++);
push(result);
chunk = [];
}
Expand Down
7 changes: 4 additions & 3 deletions src/methods/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { TransformTyped, TransformTypedOptions } from '../types';
import { transform } from '..';

export function filter<T>(
method: (chunk: T, encoding: string) => OrPromiseLike<boolean>,
method: (this: TransformTyped<T, T>, chunk: T, encoding: string, index: number) => OrPromiseLike<boolean>,
options: TransformTypedOptions<T, T> = {}
): TransformTyped<T, T> {
return transform(async (chunk, encoding, push) => {
const take = await method(chunk, encoding);
let index = 0;
return transform(async function (chunk, encoding, push) {
const take = await method.call(this, chunk, encoding, index++);
if (take) push(chunk, encoding);
}, options);
}
6 changes: 3 additions & 3 deletions src/methods/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { TransformTyped, TransformTypedOptions } from '../types';
import { transform } from '..';

export function map<T, R>(
method: (chunk: T, encoding: string, index: number) => OrPromiseLike<R>,
method: (this: TransformTyped<T, R>, chunk: T, encoding: string, index: number) => OrPromiseLike<R>,
options: TransformTypedOptions<T, R> = {}
): TransformTyped<T, R> {
let index = 0;
return transform(async (chunk, encoding, push) => {
const result = await method(chunk, encoding, index++);
return transform(async function (chunk, encoding, push) {
const result = await method.call(this, chunk, encoding, index++);
push(result);
}, options);
}
6 changes: 3 additions & 3 deletions src/methods/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { TransformTyped, TransformTypedOptions } from '../types';
import { transform } from '..';

export function reduce<T, R>(
method: (previousValue: R, nextValue: T, encoding: string, index: number) => OrPromiseLike<R>,
method: (this: TransformTyped<T, R>, previousValue: R, nextValue: T, encoding: string, index: number) => OrPromiseLike<R>,
initialValue: R,
options: TransformTypedOptions<T, R> = {}
): TransformTyped<T, R> {
let index = 0;
return transform<T, R>(async (chunk, encoding) => {
initialValue = await method(initialValue, chunk, encoding, index++);
return transform<T, R>(async function (chunk, encoding) {
initialValue = await method.call(this, initialValue, chunk, encoding, index++);
}, (push) => {
push(initialValue);
}, options);
Expand Down
14 changes: 7 additions & 7 deletions src/methods/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { TransformTyped, TransformTypedOptions } from "../types";
type Push<T> = (chunk: T, encoding?: string) => boolean;

export function transform<In, Out>(
transform: (chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
flush: (push: Push<Out>) => OrPromiseLike<void>,
transform: (this: TransformTyped<In, Out>, chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
flush: (this: TransformTyped<In, Out>, push: Push<Out>) => OrPromiseLike<void>,
options?: TransformTypedOptions<In, Out>
): TransformTyped<In, Out>
export function transform<In, Out>(
transform: (chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
transform: (this: TransformTyped<In, Out>, chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
options?: TransformTypedOptions<In, Out>
): TransformTyped<In, Out>
export function transform<In, Out>(
transform: (chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
flush?: ((push: Push<Out>) => OrPromiseLike<void> )| TransformTypedOptions<In, Out>,
transform: (this: TransformTyped<In, Out>, chunk: In, encoding: string, push: Push<Out>) => OrPromiseLike<void | undefined | Out>,
flush?: ((this: TransformTyped<In, Out>, push: Push<Out>) => OrPromiseLike<void>) | TransformTypedOptions<In, Out>,
options?: TransformTypedOptions<In, Out>
): TransformTyped<In, Out> {
if (!options && typeof flush !== 'function') {
Expand All @@ -34,7 +34,7 @@ export function transform<In, Out>(
async transform(chunk, encoding, callback) {
try {
const push = this.push.bind(this);
const result = await transform(chunk, encoding, push);
const result = await transform.call(this, chunk, encoding, push);
if (result !== undefined)
this.push(result);
} catch (err) {
Expand All @@ -47,7 +47,7 @@ export function transform<In, Out>(
try {
if (typeof flush === 'function') {
const push = this.push.bind(this);
const result = await flush(push);
const result = await flush.call(this, push);
if (result !== undefined)
this.push(result);
}
Expand Down

0 comments on commit 74335d5

Please sign in to comment.