0.1.0-86e70c11 • Published 3 years ago
@bigtest/matcher v0.1.0-86e70c11
@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)
0.1.0-56d2bd28
3 years ago
0.1.0-d6e3ed15
3 years ago
0.1.0-0af01937
3 years ago
0.1.0-4dc614d5
3 years ago
0.1.0-cf18250b
3 years ago
0.1.0-632f2868
3 years ago
0.1.0-86e70c11
3 years ago