Created
February 13, 2019 13:18
-
-
Save jdmunro/b7bfe10dac089eab50d9ca43949d7b35 to your computer and use it in GitHub Desktop.
selectOnPlatform.js
This file contains 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
/* @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