Skip to content

parseysharp/ParseyScript-Effect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@parseysharp/parseyscript-effect

Effect-friendly helpers for @parseysharp/parseyscript.

This package exports a small ParseEffect helper object that:

  • Lifts optional values from Maybe<A> to Option.Option<A>.
  • Converts parse results from ParseResult<A, E> to Either.Either<A, E>.
  • Provides pipeable combinators that match the core library's pipeable style.

For details on Parse, Navigator, Maybe, and ParseResult, see the core ParseyScript README.

Installation

Install the core library, this extension, and effect:

npm install @parseysharp/parseyscript @parseysharp/parseyscript-effect effect

@parseysharp/parseyscript and effect are peer dependencies of this package; your application should depend on them directly.

What this package adds

The main export is a ParseEffect helper object composed of pipeable functions:

  • ParseEffect.option

    ParseEffect.option: <A>(p: Parse<A>) => Parse<Option.Option<A>>

    Lifts a Parse<A> into Parse<Option.Option<A>> by delegating to Parse.maybe and converting the Maybe result into an Option.

  • ParseEffect.optionAt(head, tail)

    ParseEffect.optionAt: (head: string | number, tail: (string | number)[]) =>
      <A>(p: Parse<A>) => Parse<Option.Option<A>>

    Like calling p.maybeAt(head, tail) and then mapping Maybe<A> to Option.Option<A>, but as a pipeable combinator.

  • ParseEffect.parseEither(nav, x)

    ParseEffect.parseEither:
      <B>(nav: Navigator<B>, x: Maybe<B>) =>
      <A>(p: Parse<A>) => Either.Either<A, FmtParseError[]>

    Runs a Parse<A> and converts the ParseResult<A, FmtParseError[]> into an Either from effect.

All helpers are designed to be used with the pipeable Parse API in the core library.

Basic usage

import * as P from "@parseysharp/parseyscript";
import { ParseEffect } from "@parseysharp/parseyscript-effect";

const parser = P.Parse.zip4(
  P.Parse.string().pipe(ParseEffect.optionAt("some", ["nested", "path"])),
  P.Parse.dateTimeOffsetFlex({ epochIsMilliseconds: true }).at("some", [
    "other",
    "path",
  ]),
  P.Parse.bool().at("the", ["flag"]),
  P.Parse.floatFlex().at("the", ["number"])
);

const input = {
  the: {
    number: 23,
    flag: "false",
  },
  some: {
    other: { path: "1762912054467" },
    nested: { path: "hello" },
  },
};

const result = parser.pipe(
  ParseEffect.parseEither(P.Navigator.unknown(), P.Maybe.fromNullable(input))
);

You can also use these helpers directly on smaller parsers and then compose with the rest of the fluent Parse API.

About

Helpers for parsing into Effect-ts types (Option/Either)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published