os-detect 1.0.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.mjs ADDED
@@ -0,0 +1,162 @@
1
+ // src/react.ts
2
+ import { useState, useEffect } from "react";
3
+
4
+ // src/index.ts
5
+ var cachedIsIOS;
6
+ var cachedIsMacOS;
7
+ var cachedIsAndroid;
8
+ var cachedIsWindows;
9
+ var cachedIsLinux;
10
+ var cachedIsChromeOS;
11
+ function getUADataPlatform() {
12
+ if (typeof navigator === "undefined") return null;
13
+ const uaData = navigator.userAgentData;
14
+ if (uaData && typeof uaData.platform === "string") {
15
+ return uaData.platform.toLowerCase();
16
+ }
17
+ return null;
18
+ }
19
+ function getNodePlatform() {
20
+ if (typeof navigator !== "undefined") return null;
21
+ if (typeof process === "undefined" || typeof process.platform !== "string") return null;
22
+ return process.platform;
23
+ }
24
+ function detectIsIOS() {
25
+ if (typeof cachedIsIOS === "boolean") return cachedIsIOS;
26
+ if (typeof navigator === "undefined" || typeof window === "undefined") {
27
+ return cachedIsIOS = false;
28
+ }
29
+ const platform = getUADataPlatform();
30
+ if (platform !== null) {
31
+ cachedIsIOS = platform === "ios";
32
+ return cachedIsIOS;
33
+ }
34
+ const isClassicIOSUA = /iPhone|iPod/.test(navigator.userAgent);
35
+ const isIPadUA = /iPad/.test(navigator.userAgent);
36
+ const isIPadOS = /Macintosh/.test(navigator.userAgent) && typeof navigator.maxTouchPoints === "number" && navigator.maxTouchPoints > 1;
37
+ cachedIsIOS = isClassicIOSUA || isIPadUA || isIPadOS;
38
+ return cachedIsIOS;
39
+ }
40
+ function detectIsMacOS() {
41
+ if (typeof cachedIsMacOS === "boolean") return cachedIsMacOS;
42
+ const nodePlatform = getNodePlatform();
43
+ if (nodePlatform !== null) {
44
+ return cachedIsMacOS = nodePlatform === "darwin";
45
+ }
46
+ if (typeof navigator === "undefined") return cachedIsMacOS = false;
47
+ const platform = getUADataPlatform();
48
+ if (platform !== null) {
49
+ cachedIsMacOS = platform === "macos";
50
+ return cachedIsMacOS;
51
+ }
52
+ const isMacUA = /Macintosh|MacIntel|MacPPC|Mac68K/.test(navigator.userAgent);
53
+ cachedIsMacOS = isMacUA && !detectIsIOS();
54
+ return cachedIsMacOS;
55
+ }
56
+ function detectIsAndroid() {
57
+ if (typeof cachedIsAndroid === "boolean") return cachedIsAndroid;
58
+ const nodePlatform = getNodePlatform();
59
+ if (nodePlatform !== null) {
60
+ return cachedIsAndroid = nodePlatform === "android";
61
+ }
62
+ if (typeof navigator === "undefined") return cachedIsAndroid = false;
63
+ const platform = getUADataPlatform();
64
+ if (platform !== null) {
65
+ cachedIsAndroid = platform === "android";
66
+ return cachedIsAndroid;
67
+ }
68
+ cachedIsAndroid = /Android/.test(navigator.userAgent);
69
+ return cachedIsAndroid;
70
+ }
71
+ function detectIsWindows() {
72
+ if (typeof cachedIsWindows === "boolean") return cachedIsWindows;
73
+ const nodePlatform = getNodePlatform();
74
+ if (nodePlatform !== null) {
75
+ return cachedIsWindows = nodePlatform === "win32";
76
+ }
77
+ if (typeof navigator === "undefined") return cachedIsWindows = false;
78
+ const platform = getUADataPlatform();
79
+ if (platform !== null) {
80
+ cachedIsWindows = platform === "windows";
81
+ return cachedIsWindows;
82
+ }
83
+ cachedIsWindows = /Win32|Win64|Windows|WinCE/.test(navigator.userAgent);
84
+ return cachedIsWindows;
85
+ }
86
+ function detectIsChromeOS() {
87
+ if (typeof cachedIsChromeOS === "boolean") return cachedIsChromeOS;
88
+ if (typeof navigator === "undefined") return cachedIsChromeOS = false;
89
+ const platform = getUADataPlatform();
90
+ if (platform !== null) {
91
+ cachedIsChromeOS = platform === "chrome os" || platform === "chromeos";
92
+ return cachedIsChromeOS;
93
+ }
94
+ cachedIsChromeOS = /CrOS/.test(navigator.userAgent);
95
+ return cachedIsChromeOS;
96
+ }
97
+ function detectIsLinux() {
98
+ if (typeof cachedIsLinux === "boolean") return cachedIsLinux;
99
+ const nodePlatform = getNodePlatform();
100
+ if (nodePlatform !== null) {
101
+ return cachedIsLinux = nodePlatform === "linux";
102
+ }
103
+ if (typeof navigator === "undefined") return cachedIsLinux = false;
104
+ const platform = getUADataPlatform();
105
+ if (platform !== null) {
106
+ cachedIsLinux = platform === "linux";
107
+ return cachedIsLinux;
108
+ }
109
+ cachedIsLinux = /Linux/.test(navigator.userAgent) && !detectIsAndroid() && !detectIsChromeOS();
110
+ return cachedIsLinux;
111
+ }
112
+ async function detectIsWindows11() {
113
+ var _a, _b;
114
+ if (!detectIsWindows()) return false;
115
+ if (typeof navigator === "undefined" && typeof process !== "undefined") {
116
+ try {
117
+ const { release } = await import("os");
118
+ const buildNumber = parseInt((_a = release().split(".")[2]) != null ? _a : "0", 10);
119
+ return buildNumber >= 22e3;
120
+ } catch (e) {
121
+ return false;
122
+ }
123
+ }
124
+ try {
125
+ const nav = navigator;
126
+ if (!((_b = nav.userAgentData) == null ? void 0 : _b.getHighEntropyValues)) return false;
127
+ const { platformVersion } = await nav.userAgentData.getHighEntropyValues([
128
+ "platformVersion"
129
+ ]);
130
+ if (!platformVersion) return false;
131
+ const majorVersion = parseInt(platformVersion.split(".")[0], 10);
132
+ return majorVersion >= 13;
133
+ } catch (e) {
134
+ return false;
135
+ }
136
+ }
137
+ function getOS() {
138
+ if (detectIsIOS()) return "ios";
139
+ if (detectIsAndroid()) return "android";
140
+ if (detectIsChromeOS()) return "chromeos";
141
+ if (detectIsLinux()) return "linux";
142
+ if (detectIsMacOS()) return "macos";
143
+ if (detectIsWindows()) return "windows";
144
+ return "unknown";
145
+ }
146
+
147
+ // src/react.ts
148
+ function useOS() {
149
+ const [os] = useState(() => getOS());
150
+ return os;
151
+ }
152
+ function useIsWindows11() {
153
+ const [isWin11, setIsWin11] = useState(null);
154
+ useEffect(() => {
155
+ detectIsWindows11().then(setIsWin11);
156
+ }, []);
157
+ return isWin11;
158
+ }
159
+ export {
160
+ useIsWindows11,
161
+ useOS
162
+ };