npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

io-ts-parser-types

v2.3.1

Published

io-ts types for making parsers

Downloads

213

Readme

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