Skip to content

A Variant is a data structure that can be used to represent any other data type.

License

Notifications You must be signed in to change notification settings

staydecent/variant-type

Repository files navigation

variant-type

A Variant is a data structure (tagged union) that can be used to represent any other data type. You define the various potential types and the variant can only represent one of those types at any one time.

A good use for this, is representing parts of your React/Redux app state.

const Any = () => true

const Request = Variant({
  Unloaded: [],
  Loading: [],
  Loaded: [Any],
  Failed: [Error]
})

// ...

componentDidMount () {
  setState({request: Request.Loading})

  fetchSomething
    .then((results) => setState({request: Request.Loaded(results)}))
    .catch(e => setState({request: Request.Failed(e)}))
}

// ...

render () {
  return Request.case({
    Unloaded: () => 'Nothing to see here.',
    Loading: () => 'Please be patient.',
    Loaded: (data) => `Got this data: ${data}`,
    Failed: (error) => `Sorry: ${error}`
  })
}

About

A Variant is a data structure that can be used to represent any other data type.

Resources

License

Stars

Watchers

Forks

Packages

No packages published