Formulaic Docs

Data (Either)

The simplest example of @formulaic/fp will provide the functionality of an Either, seen in Haskell, and JavaScript libraries such as fp-ts, Folktale, and more.

@formulaic/fp provides Data<T> and NoData<T>.

Data is intended to be used directly, but NoData is abstract and usually implemented by more detailed classes, like NotFound.

Data<T> Interface

class Data<T> {
  public readonly status: 200 | 201;
  public readonly kind: "Data";
  public readonly hasData: true;
  public readonly noValue: false;
  public readonly data: T;

  public constructor(data: T) {}
}

NoData<T> Interface

abstract class NoData<T> extends FP<T> {
  public readonly hasData: false;
  public readonly noValue: true;
}

NotFound<T, EntityName> Interface

class NotFound<T, EntityName extends string> extends NoData<T> {
  public readonly kind: "NotFound";
  public readonly permissionError: boolean;
  public readonly status: 403 | 404;
  public readonly entityName?: EntityName;

  public constructor(
    entityName: EntityName,
    permissionError: boolean,
  ) {}
}

Usage

Use Data<T> when you may or may not have a return value:

function getLetterInString(haystack: string, index: number): Data<string> | EntityNotFound<string, "letter"> {
  if(haystack.length < index) {
    return new Data(haystack[index]);
  }
  return new EntityNotFound("letter", false);
}

You can check hasData to determine the result:

const letter = getLetterInString("Hello World", 2);
if(letter.hasData) {
  console.log(`Message contains '${letter.data}'`);
} else {
  console.log("Could not extract the second letter from the message.");
}

For most cases, there’s better ways to interact with FP data than directly accessing fields, which will be covered next.