UNPKG

universal-common

Version:

Library that provides useful missing base class library functionality.

63 lines (60 loc) 2.17 kB
/** * Provides information about the execution environment. */ export default class Environment { /** * Gets the newline string for the current environment. * @returns {string} The platform-specific newline string. */ static get newLine() { if (Environment.isNode) { const os = require('os'); return os.EOL; } else if (Environment.isBrowser) { return "\n"; } else { return "\n"; } } /** * Determines if the code is running in a browser environment. * @returns {boolean} True if running in a browser, false otherwise. */ static get isBrowser() { return typeof window !== "undefined" && typeof document !== "undefined"; } /** * Determines if the code is running in a Node.js environment. * @returns {boolean} True if running in Node.js, false otherwise. */ static get isNode() { return typeof process !== "undefined" && process.versions != null && process.versions.node != null && typeof process.on === "function"; } /** * Gets the operating system platform when in Node.js environment. * @returns {string|null} The platform identifier or null if not in Node.js. */ static get platform() { if (Environment.isNode) { return process.platform; } if (Environment.isDeno) { return Deno.build.os; } if (Environment.isBrowser) { // Rough browser OS detection const userAgent = navigator.userAgent; if (userAgent.indexOf("Win") !== -1) return "win32"; if (userAgent.indexOf("Mac") !== -1) return "darwin"; if (userAgent.indexOf("Linux") !== -1) return "linux"; if (userAgent.indexOf("Android") !== -1) return "android"; if (userAgent.indexOf("iOS") !== -1 || (userAgent.indexOf("iPhone") !== -1) || (userAgent.indexOf("iPad") !== -1)) return "ios"; } return null; } }