This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Union from ‘@iadvize-oss/opaque-union; | |
type $Text = { | |
text: string; | |
} | |
type $Image = { | |
url: string; | |
description: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// message.ts | |
import { createOpaqueAPI } from ‘@iadvize-oss/opaque-type’; | |
import { createFoldObject } from ‘@iadvize-oss/foldable-helpers’; | |
type $Text = { | |
text: string; | |
} | |
type $Image = { | |
url: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
createText, | |
createImage, | |
createAudio, | |
AudioMessage, | |
description, | |
url, | |
fold, | |
} from ‘fabulous-Message-library’; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type $TextMessage = { | |
text: string; | |
} | |
const { toOpaque, fromOpaque, isOpaque } = createOpaqueAPI< | |
'TextMessage', | |
$TextMessage, | |
>('TextMessage’); | |
export type TextMessage = ReturnType<typeof toOpaque>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createText(text: string): TextMessage | |
text(message: TextMessage): string | |
createImage(url: string, description: string): ImageMessage | |
createAudio(url: string, description: string): AudioMessage | |
url(message: ImageMessage | AudioMessage): string, | |
toText(message: ImageMessage): TextMessage | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createFold } from ‘@iadvize-oss/foldable-helpers’; | |
// fold :: (TextMessage -> R) -> (ImageMessage -> R) -> (AudioMessage -> R) -> Message -> R | |
// “feeding” the fold-creator with our previously created type guards | |
const fold = createFold(isText, isImage, isAudio); | |
// or, passing function with an object to give them names | |
import { createFoldObject } from ‘@iadvize-oss/foldable-helpers’; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TextMessage, ImageMessage, AudioMessage, fold } from ‘./Message’; | |
const renderMessage = fold( | |
(textMessage: TextMessage) => <TextComponent text={message.text} />), | |
(imageMessage: ImageMessage) => <ImageComponent url={message.url} description={message.description} />, | |
(audioMessage: AudioMessage) => <AudioComponent url={message.url} description={message.description} /> | |
) | |
// Somewhere in a component far, far away... | |
return ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Message.ts | |
function fold<R>( | |
onText: (message: TextMessage) => R, | |
onImage: (message: ImageMessage) => R, | |
onAudio: (message: AudioMessage) => R, | |
) => { | |
return (message: Message): R => { | |
switch (message.messageType) { | |
case ‘TEXT’: | |
return onText(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Option, fold } from ‘fp-ts/lib/Option`; | |
const user: Option<User> = … // ie. None | Some<User> | |
return fold( | |
() => <p>oops, no user!</p>, | |
(user: User) => <UserView user={user} />, | |
)(user); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (isText(message)) { | |
return <TextComponent text={message.text} /> | |
} |