0.1.0-86e70c11 • Published 3 years ago

@bigtest/matcher v0.1.0-86e70c11

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@bigtest/matcher

Switches bring no riches.

In typescript, Algebraic Data Types are encoded using union types with a discriminator field.

Example:

export type BundlerState =
  | { type: 'UNBUNDLED' }
  | { type: 'BUILDING'; warnings: BundlerWarning[] }
  | { type: 'GREEN'; path: string;  warnings: BundlerWarning[] }
  | { type: 'ERRORED'; error: BundlerError }

Pattern matching is a common usecase when dealing with ADTs. You'll often find yourself in a position where you need to default to certain values depending on what the underlying value is.

Switches

switch (event.code) {
  case 'START':
    return { type: 'START' } as const;
  case 'END':
    return { type: 'UPDATE' } as const;
  case 'ERROR':
    return { type: 'ERROR', error: event.error } as const;
  default:
    throw new Error(`unexpect event ${event.code}`);
}

Riches

match('code')<RollupWatcherEvent, BundlerMessage>({
  START: () => ({ type: 'START' }) as const,
  END: () => ({ type: 'UPDATE' } as const),
  ERROR: ({ error }) => ({ type: 'ERROR', error } as const),
})(event)