Skip to content

Commit

Permalink
Merge pull request facebook#123 from ryanartecona/feature/non-standar…
Browse files Browse the repository at this point in the history
…d-platform-os

Support non-standard Platform.OS identifiers & iOS idioms
  • Loading branch information
wokalski authored Dec 31, 2017
2 parents b1721fc + 6855d50 commit eaea385
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 38 deletions.
89 changes: 70 additions & 19 deletions lib/js/src/platform.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 41 additions & 15 deletions src/platform.re
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
type iosIdiom =
| Phone
| Pad
| TV;

[@bs.module "react-native"] [@bs.scope "Platform"]
external _ios_isPad : bool = "isPad";

[@bs.module "react-native"] [@bs.scope "Platform"]
external _ios_isTVOS : bool = "isTVOS";

type os =
| IOS
| IOS(iosIdiom)
| Android;

[@bs.scope "Platform"] [@bs.module "react-native"] external _os : string = "OS";
[@bs.module "react-native"] [@bs.scope "Platform"]
external _os : string = "OS";

let os =
exception UnknownPlatform(string);

let os = () =>
switch _os {
| "ios" => IOS
| _ => Android
| "ios" =>
switch _ios_isPad {
| true => IOS(Pad)
| _ =>
switch _ios_isTVOS {
| true => IOS(TV)
| _ => IOS(Phone)
}
}
| "android" => Android
| x => raise(UnknownPlatform(x))
};

let equals = (targetOs) =>
switch (os, targetOs) {
| (IOS, IOS) => true
let equals = targetOs =>
switch (os(), targetOs) {
| (IOS(_), IOS(_)) => true
| (Android, Android) => true
| (IOS, Android) => false
| (Android, IOS) => false
| exception (UnknownPlatform(_)) => false
| _ => false
};

[@bs.scope "Platform"] [@bs.module "react-native"] external version : int = "Version";
[@bs.module "react-native"] [@bs.scope "Platform"]
external _version : Js.undefined(int) = "Version";

[@bs.scope "Platform"] [@bs.module "react-native"]
external _select : {. "ios": 'a, "android": 'a} => 'a =
"select";
exception UnknownVersion;

let select = (~ios, ~android) => _select({"ios": ios, "android": android});
let version = () =>
switch (Js.Undefined.to_opt(_version)) {
| Some(v) => v
| None => raise(UnknownVersion)
};
22 changes: 18 additions & 4 deletions src/platform.rei
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
type iosIdiom =
| Phone
| Pad
| TV;

type os =
| IOS
| IOS(iosIdiom)
| Android;

let os: os;
exception UnknownPlatform(string);

/**
Raises UnknownPlatform for non-standard platforms such as "web"
from react-native-web
*/
let os: unit => os;

let equals: os => bool;

[@bs.scope "Platform"] [@bs.module "react-native"] external version : int = "Version";
exception UnknownVersion;

let select: (~ios: 'a, ~android: 'a) => 'a;
/**
Raises UnknownVersion if version is undefined, i.e. in react-native-web
*/
let version: unit => int;

0 comments on commit eaea385

Please sign in to comment.