• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TypeScript code generation from a haskell-like syntax for ADT. Playground:

TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)

Playground

A playground with a few examples

Installation

To install the stable version:

npm install fp-ts-codegen

Usage

Signature

function run(input: string, options: Options = defaultOptions): Either<string, string>

Example

import { run } from 'fp-ts-codegen'

console.log(run('data Option A = None | Some A'))

Output

/** type definition */

export type Option<A> =
  | {
      readonly type: 'None'
    }
  | {
      readonly type: 'Some'
      readonly value0: A
    }

/** constructors */

export const none: Option<never> = { type: 'None' }

export function some<A>(value0: A): Option<A> {
  return { type: 'Some', value0 }
}

/** pattern matching */

export function fold<A, R>(onNone: () => R, onSome: (value0: A) => R): (fa: Option<A>) => R {
  return fa => {
    switch (fa.type) {
      case 'None':
        return onNone()
      case 'Some':
        return onSome(fa.value0)
    }
  }
}

/** prisms */

import { Prism } from 'monocle-ts'

export function _none<A>(): Prism<Option<A>, Option<A>> {
  return Prism.fromPredicate(s => s.type === 'None')
}

export function _some<A>(): Prism<Option<A>, Option<A>> {
  return Prism.fromPredicate(s => s.type === 'Some')
}

/** Eq instance */

import { Eq } from 'fp-ts/lib/Eq'

export function getEq<A>(eqSomeValue0: Eq<A>): Eq<Option<A>> {
  return {
    equals: (x, y) => {
      if (x === y) {
        return true
      }
      if (x.type === 'None' && y.type === 'None') {
        return true
      }
      if (x.type === 'Some' && y.type === 'Some') {
        return eqSomeValue0.equals(x.value0, y.value0)
      }
      return false
    }
  }
}

Records

Syntax: { name :: type }

Example

Source

--     record ---v
data User = User { name :: string, surname :: string }

Output

export type User = {
  readonly type: 'User'
  readonly name: string
  readonly surname: string
}

export function user(name: string, surname: string): User {
  return { type: 'User', name, surname }
}

Tuples

Syntax: (type1, type2, ...types)

Example

Source

--              tuple ---v
data Tuple2 A B = Tuple2 (A, B)

Output

export type Tuple2<A, B> = {
  readonly type: 'Tuple2'
  readonly value0: [A, B]
}

export function tuple2<A, B>(value0: [A, B]): Tuple2<A, B> {
  return { type: 'Tuple2', value0 }
}

Constraints

Syntax: (<name> :: <constraint>)

Example

Source

--    constraint ---v
data Constrained (A :: string) = Fetching | GotData A

Output

export type Constrained<A extends string> =
  | {
      readonly type: 'Fetching'
    }
  | {
      readonly type: 'GotData'
      readonly value0: A
    }

Options

// fp-ts-codegen/lib/ast module

export interface Options {
  /** the name of the field used as tag */
  tagName: string
  /** the name prefix used for pattern matching functions */
  foldName: string
  /**
   * the pattern matching handlers can be expressed as positional arguments
   * or a single object literal `tag -> handler`
   */
  handlersStyle: { type: 'positional' } | { type: 'record'; handlersName: string }
}

export const defaultOptions: Options = {
  tagName: '_tag',
  foldName: 'fold',
  handlersStyle: { type: 'positional' }
}

Options management

Example

import { lenses, defaultOptions } from 'fp-ts-codegen/lib/ast'

lenses.tagName.set('tag')(defaultOptions)
/*
{
  tagName: 'tag',
  foldName: 'fold',
  ...
}
*/

Modules

  • ast module: internal model -> TypeScript AST
  • model module: internal model
  • printer module: internal model -> TypeScript code
  • haskell module: haskell-like syntax -> internal model
  • index module: haskell-like syntax -> TypeScript code

Roadmap

  • derive type class instances? (Functor, Foldable, etc...)
  • ???

More Repositories

1

fp-ts

Functional programming in TypeScript
TypeScript
10,811
star
2

io-ts

Runtime type system for IO decoding/encoding
TypeScript
6,694
star
3

tcomb-form-native

Forms library for react-native
JavaScript
3,148
star
4

tcomb

Type checking and DDD for JavaScript
JavaScript
1,897
star
5

tcomb-form

Forms library for react
JavaScript
1,163
star
6

monocle-ts

Functional optics: a (partial) porting of Scala monocle
TypeScript
1,001
star
7

newtype-ts

Implementation of newtypes in TypeScript
TypeScript
553
star
8

babel-plugin-tcomb

Babel plugin for static and runtime type checking using Flow and tcomb
JavaScript
482
star
9

flow-static-land

[DEPRECATED, please check out fp-ts] Implementation of common algebraic types in JavaScript + Flow
JavaScript
408
star
10

functional-programming

Introduction to Functional Programming (Italian)
TypeScript
401
star
11

tcomb-validation

Validation library based on type combinators
JavaScript
400
star
12

typelevel-ts

Type level programming in TypeScript
TypeScript
354
star
13

io-ts-types

A collection of codecs and combinators for use with io-ts
TypeScript
311
star
14

elm-ts

A porting to TypeScript featuring fp-ts, rxjs6 and React
TypeScript
301
star
15

redux-tcomb

Immutable and type-checked state and actions for Redux
JavaScript
211
star
16

tcomb-react

Alternative syntax for PropTypes
JavaScript
202
star
17

fp-ts-contrib

A community driven utility package for fp-ts
TypeScript
199
star
18

parser-ts

String parser combinators for TypeScript
TypeScript
194
star
19

fp-ts-rxjs

fp-ts bindings for RxJS
TypeScript
188
star
20

prop-types-ts

Alternative syntax for prop types providing both static and runtime type safety, powered by io-ts
TypeScript
170
star
21

fp-ts-routing

A type-safe bidirectional routing library for TypeScript
TypeScript
168
star
22

retry-ts

Retry combinators for monadic actions that may fail
TypeScript
162
star
23

io-ts-codegen

Code generation for io-ts
TypeScript
152
star
24

tcomb-json-schema

Transforms a JSON Schema to a tcomb type
JavaScript
144
star
25

flowcheck

[DEPRECATED, use babel-plugin-tcomb instead] Runtime type checking for Flow
JavaScript
116
star
26

docs-ts

A zero-config documentation tool for my TypeScript projects
TypeScript
100
star
27

logging-ts

Composable loggers for TypeScript
TypeScript
99
star
28

flowcheck-loader

[DEPRECATED] A Webpack loader for Flowcheck
JavaScript
89
star
29

tom

Elmish type-safe state and side effect manager using RxJS
JavaScript
87
star
30

money-ts

TypeScript library for type-safe and lossless encoding and manipulation of world currencies and precious metals
TypeScript
87
star
31

fp-ts-laws

fp-ts type class laws for property based testing
TypeScript
80
star
32

flow-io

[DEPRECATED, please check out io-ts] Flow compatible runtime type system for IO validation
JavaScript
73
star
33

react-vdom

Pulls out the VDOM and makes tests easy (also in node and without the DOM)
JavaScript
72
star
34

uvdom

Universal Virtual DOM
JavaScript
53
star
35

fp-ts-fluture

fp-ts bindings for Fluture
TypeScript
50
star
36

fp-ts-local-storage

fp-ts bindings for LocalStorage
TypeScript
49
star
37

graphics-ts

A porting of purescript-{canvas, drawing} featuring fp-ts
TypeScript
44
star
38

tcomb-react-bootstrap

Type checking for react-bootstrap [DEPRECATED]
JavaScript
41
star
39

talks

talks and blog posts
HTML
41
star
40

unknown-ts

A polyfill of unknown that works with legacy typescript versions (before 3.0)
TypeScript
28
star
41

mtl-ts

MTL-style in TypeScript
TypeScript
24
star
42

recursion-schemes-ts

Recursion schemes in TypeScript (POC)
TypeScript
22
star
43

tcomb-doc

Documentation tool for tcomb
JavaScript
14
star
44

babel-plugin-tcomb-boilerplate

Boilerplate showing what you can get in terms of type safety with babel-plugin-tcomb
JavaScript
14
star
45

fp-ts-node

TypeScript
13
star
46

flow-react

Advanced type checking for react using Flow
JavaScript
13
star
47

pantarei

Repository for idiomatic Flowtype definition files
JavaScript
11
star
48

elm-ts-todomvc

todomvc implementation using elm-ts and fp-ts
TypeScript
11
star
49

typescript-course

Esercizi del corso di programmazione avanzata con TypeScript
TeX
11
star
50

tcomb-form-templates-semantic

Semantic UI templates for tcomb-form
JavaScript
11
star
51

tcomb-form-templates-bootstrap

Bootstrap templates for tcomb-form
JavaScript
10
star
52

sanctuary-libdef

Flow definition file for sanctuary
JavaScript
9
star
53

simple-date-picker

A simple clean way to pick dates with React
JavaScript
9
star
54

flow-update

Statically type checked model updates using Flow
JavaScript
8
star
55

profunctor-lenses-ts

Pure profunctor lenses in TypeScript (just a POC)
TypeScript
8
star
56

fp-typed-install

TypeScript
7
star
57

cerebral-tcomb

[No Maintenance Intended] immutable and type checked model layer for cerebral
JavaScript
7
star
58

import-path-rewrite

JavaScript
6
star
59

fractal-trees-ts

Fractal trees with fp-ts
TypeScript
6
star
60

fcomb

Function combinators
JavaScript
5
star
61

uvdom-bootstrap

Bootstrap 3 components built with uvdom
JavaScript
5
star
62

simple-format-number

A simple clean way to format numbers with Javascript
JavaScript
5
star
63

behaviors-ts

A porting of purescript-behaviors
TypeScript
5
star
64

tree-shaking-test

JavaScript
4
star
65

io-ts-benchmarks

TypeScript
3
star
66

fetch-optimizer

[No Maintenance Intended] Optimises dependent data fetchers
JavaScript
2
star
67

simple-format-timeago

A simple clean way to format intervals with Javascript
JavaScript
2
star
68

simple-format-date

A simple clean way to format dates with Javascript
JavaScript
2
star
69

tcomb-i18n

Simple i18n / i17n helpers [DEPRECATED]
JavaScript
2
star
70

simple-parse-number

A simple clean way to parse numbers with Javascript
JavaScript
1
star