gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

add `ArrayFromString` codec #164

Open mantas-janulionis opened 3 years ago

mantas-janulionis commented 3 years ago

Generalised implementation of converting string to Array. It is essentially utilising string.split and decoder for each item. It accepts both string and RegExp as split separator. It is compatible with other codecs, thus usage like ArrayFromString(IntFromString) is accepted

mantas-janulionis commented 3 years ago

IMHO the codec should do only the transformation from string to Array<string> then you can easily pipe to a Array<string> -> Array<number> codec e.g. arrayFromString.pipe(t.array(NumberFromString))

This is probably a right idea. Thinking about a use case where I would need to convert from string to Array<string>, it would require another codec such as StringFromString which is redundant

EricCrosson commented 3 years ago

This is probably a right idea. Thinking about a use case where I would need to convert from string to Array<string>, it would require another codec such as StringFromString which is redundant

Would t.string not work? string -> string is a subset of unknown -> string