Skip to content

Instantly share code, notes, and snippets.

@jimthedev
Created February 17, 2016 22:36
Show Gist options
  • Save jimthedev/d4bd0626da6d7cb3d5d4 to your computer and use it in GitHub Desktop.
Save jimthedev/d4bd0626da6d7cb3d5d4 to your computer and use it in GitHub Desktop.
Reduce orientation events coming from react-native-orientation npm module
function _reduceOrientation(lastOrientation, orientation) {
if(orientation==='UNKNOWN' && lastOrientation==='UNKNOWN') {
return 'UNKNOWN';
}
switch(orientation) {
case 'LANDSCAPE':
case 'PORTRAIT':
return orientation;
case 'UNKNOWN':
return lastOrientation;
}
}
@jimthedev
Copy link
Author

Expected output:

console.log(_reduceOrientation('UNKNOWN', 'UNKNOWN')); // UNKNOWN
console.log(_reduceOrientation('PORTRAIT', 'UNKNOWN')); // PORTRAIT 
console.log(_reduceOrientation('LANDSCAPE', 'UNKNOWN')); // LANDSCAPE

console.log(_reduceOrientation('UNKNOWN', 'PORTRAIT')); // PORTRAIT
console.log(_reduceOrientation('LANDSCAPE', 'PORTRAIT')); // PORTRAIT
console.log(_reduceOrientation('PORTRAIT', 'PORTRAIT')); // PORTRAIT

console.log(_reduceOrientation('UNKNOWN', 'LANDSCAPE')); // LANDSCAPE
console.log(_reduceOrientation('PORTRAIT', 'LANDSCAPE')); // LANDSCAPE
console.log(_reduceOrientation('LANDSCAPE', 'LANDSCAPE')); // LANDSCAPE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment