Skip to content

Instantly share code, notes, and snippets.

@jdmunro
Created February 13, 2019 13:18
Show Gist options
  • Save jdmunro/b7bfe10dac089eab50d9ca43949d7b35 to your computer and use it in GitHub Desktop.
Save jdmunro/b7bfe10dac089eab50d9ca43949d7b35 to your computer and use it in GitHub Desktop.
selectOnPlatform.js
/* @flow */
import { Platform } from 'react-native'
const PLATFORM_PREFERENCES = Object.freeze({
ios: ['ios', 'mobile'],
android: ['android', 'mobile'],
web: ['web'],
})
type SelectOnPlatformValues<T> =
| {|
mobile: T,
web: T,
|}
| {|
ios: T,
android: T,
web: T,
|}
export default <T>(values: SelectOnPlatformValues<T>): T => {
const possibleKeys = PLATFORM_PREFERENCES[Platform.OS]
const key = possibleKeys.find(key => values.hasOwnProperty(key))
if (key === undefined) {
throw new Error(`Cannot find value for current platform (${Platform.OS})`)
}
// $FlowFixMe
const value: T = values[key]
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment