io-ts-parser-types
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

io-ts-parser-types

NPM Version NPM Downloads

io-ts codec types for parsing data

Install

npm i io-ts-parser-types

Note. fp-ts and io-ts are peer dependencies for io-ts-parser-types

Usage

typeFromRegexp(regexp, codec)

returns a codec that matches string with regexp and the validates all named capture groups with codec.

Example

const dataCodec = t.type({
  prop: NumberFromString,
  value: BooleanFromString,
});

const codec = typeFromRegexp(/p(?<prop>\d+)\/v(?<value>.+)/, dataCodec);
expect(decode(codec, "p123/vtrue")).toEqual({
  prop: 123,
  value: true,
});

Both following codecs accept the same parameters schema and name. The name is just a codec name. for Schema look for example

typeFromString(schema, name)

returns a codec that extracts fields for the position in the string and returns an object. encode returns the object with encoded fields

codecTypeFromString(schema, name)

returns a codec that extracts fields for the position in the string and returns an object. encode returns the string with field values in respective positions

Example

const schema = {
  prop1: {
    position: [1, 2],
    codec: NumberFromString,
  },
  prop2: {
    position: [5, 9],
    codec: BooleanFromString,
  },
};
const typeC = typeFromString(schema, "string to object");
const codecTypeC = codecTypeFromString(schema, "string to object to string");

const result = {
  prop1: 2,
  prop2: true,
};

expect(decode(typeC, "12345true78")).toEqual(result);
expect(decode(codecTypeC, "12345true78")).toEqual(result);

expect(typeC.encode(result)).toEqual({
  prop1: "2",
  prop2: "true",
});
expect(codecTypeC.encode(result)).toEqual(" 2   true");

Number

  • integerFrom(options) - generic codec creation function for integer values
  • decimalFrom(options) - generic codec creation function for decimal values. It uses decimal.js
  • floatFrom(options) - generic codec creation function for float values
    • regexp - optional, RegExp, matches string and returns first group as a number, ignores commas as a thousand separator
    • name - optional, name of the codec
  • ZeroFromNull - returns 0 for null
  • DecimalFromPercentString - returns a percent value from string like e.g., 58% -> 0.58

Boolean

  • booleanFrom(options) - generic boolean codec creator function.
    • true - true value, false - false value
    • true - true value, strict - optional, boolean, if set to true only true value matches
    • false - false value, strict - optional, boolean, if set to true only false value matches

Null

  • nullFrom(options) - generic null codec creator function
    • match - array of values to be valid as null
    • caseSensitive - optional, default true, matches values from match exactly. If you want to treat strings from the match array as non case-sensitive set them to false

String

  • TrimmedString - validates that value is a string and trims white space.

License

MIT

Package Sidebar

Install

npm i io-ts-parser-types

Weekly Downloads

49

Version

2.3.1

License

MIT

Unpacked Size

96.2 kB

Total Files

35

Last publish

Collaborators

  • velocityzen