Created
August 16, 2020 03:59
-
-
Save Lucifier129/798e9c8240290daedf6164e4f4c2dd4b to your computer and use it in GitHub Desktop.
procedural abstraction
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
// data version | |
type DraftPost = { | |
kind: 'DraftPost'; | |
content: string; | |
}; | |
type ReviewingPost = { | |
kind: 'ReviewingPost'; | |
content: string; | |
}; | |
type RejectedPost = { | |
kind: 'RejectedPost'; | |
content: string; | |
reason: string; | |
}; | |
type ApprovedPost = { | |
kind: 'ApprovedPost'; | |
content: string; | |
reason: string; | |
}; | |
const createPost = (content: string = ''): DraftPost => { | |
return { | |
kind: 'DraftPost', | |
content, | |
}; | |
}; | |
const appendPostContent = (draftPost: DraftPost, content: string = '') => { | |
return { | |
...draftPost, | |
content: draftPost.content + content | |
} | |
} | |
const reviewPost = (draft: DraftPost): ReviewingPost => { | |
return { | |
kind: 'ReviewingPost', | |
content: draft.content, | |
}; | |
}; | |
const approvePost = ( | |
reviewingPost: ReviewingPost, | |
reason: string = '' | |
): ApprovedPost => { | |
return { | |
kind: 'ApprovedPost', | |
content: reviewingPost.content, | |
reason, | |
}; | |
}; | |
const rejectPost = ( | |
reviewingPost: ReviewingPost, | |
reason: string = '' | |
): RejectedPost => { | |
return { | |
kind: 'RejectedPost', | |
content: reviewingPost.content, | |
reason, | |
}; | |
}; | |
const rejectToDraftPost = (rejectedPost: RejectedPost): DraftPost => { | |
return { | |
kind: 'DraftPost', | |
content: rejectedPost.content, | |
}; | |
}; | |
const test = () => { | |
let draftPost = createPost('hello') | |
let newDraftPost = appendPostContent(draftPost, ' world') | |
let reviewingPost = reviewPost(newDraftPost) | |
let rejectedPost = rejectPost(reviewingPost, 'too bad') | |
let approvedPost = approvePost(reviewingPost, 'so good') | |
// uncomment to see the type error | |
// appendPostContent(reviewingPost, 'adsfasdf') | |
// reviewPost(approvedPost) | |
// approvePost(rejectedPost) | |
} |
Author
Lucifier129
commented
Aug 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment