Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import { Either, left, right } from 'fp-ts/lib/Either';
  2.  
  3. type File = {
  4. name: string;
  5. ext: string;
  6. size: number;
  7. };
  8.  
  9. const checkName = (f: File): Either<string, File> =>
  10. f.name.length < 20 ? right(f) : left('File length should be less then 20 characters');
  11.  
  12. const checkExt = (f: File): Either<string, File> =>
  13. ['ts', 'tsx'].includes(f.ext) ? right(f) : left('File extension should be either ts or tsx');
  14.  
  15. const checkSize = (f: File): Either<string, File> => (f.size < 1000 ? right(f) : left('Error message about size'));
  16.  
  17.  
  18. const file = {
  19. name: 'file',
  20. ext: 'tss',
  21. size: 123,
  22. };
  23.  
  24. // we'll have Either Right<File> or Left<string> here
  25. const validatedFile = checkName(file)
  26. .chain(checkExt)
  27. .chain(checkSize);
Add Comment
Please, Sign In to add comment