Created
January 8, 2019 09:01
-
-
Save baetheus/abcdec4a8076b6d6c006e98a6220742d to your computer and use it in GitHub Desktop.
Method Overload Issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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