Skip to content

Instantly share code, notes, and snippets.

@baetheus
Created January 8, 2019 09:01
Show Gist options
  • Select an option

  • Save baetheus/abcdec4a8076b6d6c006e98a6220742d to your computer and use it in GitHub Desktop.

Select an option

Save baetheus/abcdec4a8076b6d6c006e98a6220742d to your computer and use it in GitHub Desktop.
Method Overload Issues
import { Pipe, PipeTransform } from '@angular/core';
import { map } from 'lodash';
import { tryCatchEither } from '@loda/utils';
interface MapTransform {
<T, D>(value: T, iteratee: (t: T) => D): D | T;
<T, D>(value: T[], iteratee: (t: T) => D): D[] | T[];
}
@Pipe({
name: 'map',
})
export class MapPipe implements PipeTransform {
constructor() {}
transform: MapTransform = (value, iteratee) => {
// Kick back if predicate is not a function
if (typeof iteratee !== 'function') {
console.warn(`The map pipe requires a function as its iteratee. Instead, got a ${typeof iteratee}`);
return value;
}
// External tryCatch for optimization
const out = Array.isArray(value)
? tryCatchEither(() => map(value, iteratee))
: tryCatchEither(() => iteratee(value));
if (out.isRight()) {
return out.value;
}
console.error('An error was caught in a map pipe. The map pipe is returning the original value.', out.value);
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment