Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.