diff --git a/lib/js/src/platform.js b/lib/js/src/platform.js index 1c31ac42333c42..0c0444ad477eda 100644 --- a/lib/js/src/platform.js +++ b/lib/js/src/platform.js @@ -1,34 +1,85 @@ // Generated by BUCKLESCRIPT VERSION 2.1.0, PLEASE EDIT WITH CARE 'use strict'; -var ReactNative = require("react-native"); +var Js_exn = require("bs-platform/lib/js/js_exn.js"); +var ReactNative = require("react-native"); +var Caml_exceptions = require("bs-platform/lib/js/caml_exceptions.js"); -var match = ReactNative.Platform.OS; +var UnknownPlatform = Caml_exceptions.create("Platform-BsReactNative.UnknownPlatform"); -var os = match === "ios" ? /* IOS */0 : /* Android */1; +function os() { + var x = ReactNative.Platform.OS; + switch (x) { + case "android" : + return /* Android */0; + case "ios" : + var match = +ReactNative.Platform.isPad; + if (match !== 0) { + return /* IOS */[/* Pad */1]; + } else { + var match$1 = +ReactNative.Platform.isTVOS; + if (match$1 !== 0) { + return /* IOS */[/* TV */2]; + } else { + return /* IOS */[/* Phone */0]; + } + } + break; + default: + throw [ + UnknownPlatform, + x + ]; + } +} function equals(targetOs) { - if (os !== 0) { - if (targetOs !== 0) { - return /* true */1; + var exit = 0; + var val; + var val$1; + try { + val = os(/* () */0); + val$1 = targetOs; + exit = 1; + } + catch (raw_exn){ + var exn = Js_exn.internalToOCamlException(raw_exn); + if (exn[0] === UnknownPlatform) { + return /* false */0; } else { + throw exn; + } + } + if (exit === 1) { + if (val) { + if (val$1) { + return /* true */1; + } else { + return /* false */0; + } + } else if (val$1) { return /* false */0; + } else { + return /* true */1; } - } else if (targetOs !== 0) { - return /* false */0; - } else { - return /* true */1; } + } -function select(ios, android) { - return ReactNative.Platform.select({ - ios: ios, - android: android - }); +var UnknownVersion = Caml_exceptions.create("Platform-BsReactNative.UnknownVersion"); + +function version() { + var match = ReactNative.Platform.Version; + if (match !== undefined) { + return match; + } else { + throw UnknownVersion; + } } -exports.os = os; -exports.equals = equals; -exports.select = select; -/* match Not a pure module */ +exports.UnknownPlatform = UnknownPlatform; +exports.os = os; +exports.equals = equals; +exports.UnknownVersion = UnknownVersion; +exports.version = version; +/* react-native Not a pure module */ diff --git a/src/platform.re b/src/platform.re index 0d98336f23f332..ce66d3e51a295f 100644 --- a/src/platform.re +++ b/src/platform.re @@ -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) + }; diff --git a/src/platform.rei b/src/platform.rei index 74ae45b717c0f4..f2d609005f4599 100644 --- a/src/platform.rei +++ b/src/platform.rei @@ -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;