Effect CommunityEC
Effect Community3y ago
6 replies
joystick

Improving the split function in TypeScript using fp-ts/Either

Hello, how are you?

I'm looking for advice how to improve this:

import { right, left, chain } from 'fp-ts/Either'
import { flow } from 'fp-ts/lib/function'

// XXX refactor input.split
// split :: String -> Right([String, String]) Left(Error)
export const split = (input: any) => typeof input !== 'string'
  ? left('not string input')
  : input.split(',').length !== 2
    ? left('not two arguments')
    : input.split(',')[0].length === 0 || input.split(',')[1].length === 0
      ? left('no empty arguments')
      : right(input.split(','))

I feel like I need to use State monad here just not sure how 🤔
Was this page helpful?