DrKillshot / unwrap

A functional programming library for typescript developers
https://drkillshot.github.io/unwrap/
1 stars 1 forks source link

5 add data type #15

Closed DrKillshot closed 5 days ago

DrKillshot commented 6 days ago

Use case example

const data = Data({
    Success: (id: number, username: string) => ({id, username}),
    Failure: (error: string) => error,
    Pending: Empty
})

const success = data.Success(10, "john.doe")
// or
const error = data.Failure("There has been a network failure")
// or
const pending = data.Pending

success.match({
    Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`),
    Failure: (error) => console.log(`There has been an error. Error: ${error}`),
    Pending: () => console.log("The request is still pending, please wait.")
})

// or
error.match({
    Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`),
    Failure: (error) => console.log(`There has been an error. Error: ${error}`),
    Pending: () => console.log("The request is still pending, please wait.")
})

// or
pending.match({
    Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`),
    Failure: (error) => console.log(`There has been an error. Error: ${error}`),
    Pending: () => console.log("The request is still pending, please wait.")
})

// or if we don't want to define all cases
success.match({
    Pending: () => console.log("The request is still pending, please wait."),
    _: () => console.log("Default case")
})