advanced-js-kit
Version:
Modern TypeScript utility library with tree-shaking support - Array, String, Number, Network, Sleep, and JWT utilities for JavaScript and TypeScript projects
46 lines (45 loc) • 1.52 kB
JavaScript
// src/universal/utils/index.ts
function isNodeEnvironment() {
return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
}
function isBrowserEnvironment() {
return typeof window !== "undefined" && typeof document !== "undefined";
}
function isWebWorkerEnvironment() {
return typeof globalThis.importScripts === "function" && typeof window === "undefined";
}
function getEnvironment() {
if (isNodeEnvironment()) return "node";
if (isBrowserEnvironment()) return "browser";
if (isWebWorkerEnvironment()) return "webworker";
return "unknown";
}
var EnvironmentError = class extends Error {
constructor(message, requiredEnvironment, currentEnvironment) {
super(message);
this.requiredEnvironment = requiredEnvironment;
this.currentEnvironment = currentEnvironment;
this.name = "EnvironmentError";
}
};
function assertNodeEnvironment() {
if (!isNodeEnvironment()) {
throw new EnvironmentError(
"This functionality requires Node.js environment",
"node",
getEnvironment()
);
}
}
function assertBrowserEnvironment() {
if (!isBrowserEnvironment()) {
throw new EnvironmentError(
"This functionality requires browser environment",
"browser",
getEnvironment()
);
}
}
export { EnvironmentError, assertBrowserEnvironment, assertNodeEnvironment, getEnvironment, isBrowserEnvironment, isNodeEnvironment, isWebWorkerEnvironment };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map