Skip to content

Commit

Permalink
ref: move getOperatingSystem to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
kubabutkiewicz committed Sep 15, 2023
1 parent 446f29b commit 3fdd36b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Platform} from 'react-native';
import CONST from '../../CONST';
import GetOperatingSystem from './types';

/**
* Reads the current operating system for native platforms.
* @return {String | null}
*/
export default () => {
const getOperatingSystem: GetOperatingSystem = () => {
switch (Platform.OS) {
case 'ios':
return CONST.OS.IOS;
Expand All @@ -15,3 +15,5 @@ export default () => {
return CONST.OS.NATIVE;
}
};

export default getOperatingSystem;
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import _ from 'underscore';
import CONST from '../../CONST';
import GetOperatingSystem from './types';

/**
* Reads the current operating system when running on Web/Mobile-Web/Desktop
* @return {String | null}
*/
export default () => {
const getOperatingSystem: GetOperatingSystem = () => {
const {userAgent, platform} = window.navigator;
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
const iosPlatforms = ['iPhone', 'iPad', 'iPod'];

let os = null;
if (_.contains(macosPlatforms, platform)) {
if (macosPlatforms.includes(platform)) {
os = CONST.OS.MAC_OS;
} else if (_.contains(iosPlatforms, platform)) {
} else if (iosPlatforms.includes(platform)) {
os = CONST.OS.IOS;
} else if (_.contains(windowsPlatforms, platform)) {
} else if (windowsPlatforms.includes(platform)) {
os = CONST.OS.WINDOWS;
} else if (/Android/.test(userAgent)) {
os = CONST.OS.ANDROID;
Expand All @@ -25,3 +24,5 @@ export default () => {
}
return os;
};

export default getOperatingSystem;
7 changes: 7 additions & 0 deletions src/libs/getOperatingSystem/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ValueOf} from 'type-fest';
import CONST from '../../CONST';

type OS = ValueOf<typeof CONST.OS> | null;
type GetOperatingSystem = () => OS;

export default GetOperatingSystem;

0 comments on commit 3fdd36b

Please sign in to comment.