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
1 lines • 3.64 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/universal/utils/index.ts"],"names":[],"mappings":";;;AAQO,SAAS,iBAAA,GAA6B;AAC3C,EAAA,OAAO,OAAO,YAAY,WAAA,IACnB,OAAA,CAAQ,YAAY,IAAA,IACpB,OAAA,CAAQ,SAAS,IAAA,IAAQ,IAAA;AAClC;AAMO,SAAS,oBAAA,GAAgC;AAC9C,EAAA,OAAO,OAAO,MAAA,KAAW,WAAA,IAClB,OAAO,QAAA,KAAa,WAAA;AAC7B;AAMO,SAAS,sBAAA,GAAkC;AAChD,EAAA,OAAO,OAAQ,UAAA,CAAmB,aAAA,KAAkB,UAAA,IAC7C,OAAO,MAAA,KAAW,WAAA;AAC3B;AAMO,SAAS,cAAA,GAA+D;AAC7E,EAAA,IAAI,iBAAA,IAAqB,OAAO,MAAA;AAChC,EAAA,IAAI,oBAAA,IAAwB,OAAO,SAAA;AACnC,EAAA,IAAI,sBAAA,IAA0B,OAAO,WAAA;AACrC,EAAA,OAAO,SAAA;AACT;AAKO,IAAM,gBAAA,GAAN,cAA+B,KAAA,CAAM;AAAA,EAC1C,WAAA,CACE,OAAA,EACgB,mBAAA,EACA,kBAAA,EAChB;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AAHG,IAAA,IAAA,CAAA,mBAAA,GAAA,mBAAA;AACA,IAAA,IAAA,CAAA,kBAAA,GAAA,kBAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,kBAAA;AAAA;AAEhB;AAMO,SAAS,qBAAA,GAA8B;AAC5C,EAAA,IAAI,CAAC,mBAAkB,EAAG;AACxB,IAAA,MAAM,IAAI,gBAAA;AAAA,MACR,iDAAA;AAAA,MACA,MAAA;AAAA,MACA,cAAA;AAAe,KACjB;AAAA;AAEJ;AAMO,SAAS,wBAAA,GAAiC;AAC/C,EAAA,IAAI,CAAC,sBAAqB,EAAG;AAC3B,IAAA,MAAM,IAAI,gBAAA;AAAA,MACR,iDAAA;AAAA,MACA,SAAA;AAAA,MACA,cAAA;AAAe,KACjB;AAAA;AAEJ","file":"index.cjs","sourcesContent":["/**\r\n * Environment detection utilities\r\n */\r\n\r\n/**\r\n * Determines if the current environment is Node.js\r\n * @returns true if running in Node.js, false otherwise\r\n */\r\nexport function isNodeEnvironment(): boolean {\r\n return typeof process !== 'undefined' && \r\n process.versions != null && \r\n process.versions.node != null;\r\n}\r\n\r\n/**\r\n * Determines if the current environment is a browser\r\n * @returns true if running in a browser, false otherwise\r\n */\r\nexport function isBrowserEnvironment(): boolean {\r\n return typeof window !== 'undefined' && \r\n typeof document !== 'undefined';\r\n}\r\n\r\n/**\r\n * Determines if the current environment is a web worker\r\n * @returns true if running in a web worker, false otherwise\r\n */\r\nexport function isWebWorkerEnvironment(): boolean {\r\n return typeof (globalThis as any).importScripts === 'function' && \r\n typeof window === 'undefined';\r\n}\r\n\r\n/**\r\n * Gets the current runtime environment\r\n * @returns 'node' | 'browser' | 'webworker' | 'unknown'\r\n */\r\nexport function getEnvironment(): 'node' | 'browser' | 'webworker' | 'unknown' {\r\n if (isNodeEnvironment()) return 'node';\r\n if (isBrowserEnvironment()) return 'browser';\r\n if (isWebWorkerEnvironment()) return 'webworker';\r\n return 'unknown';\r\n}\r\n\r\n/**\r\n * Error thrown when functionality is not supported in the current environment\r\n */\r\nexport class EnvironmentError extends Error {\r\n constructor(\r\n message: string,\r\n public readonly requiredEnvironment: string,\r\n public readonly currentEnvironment: string\r\n ) {\r\n super(message);\r\n this.name = 'EnvironmentError';\r\n }\r\n}\r\n\r\n/**\r\n * Asserts that the current environment is Node.js\r\n * @throws {EnvironmentError} if not running in Node.js\r\n */\r\nexport function assertNodeEnvironment(): void {\r\n if (!isNodeEnvironment()) {\r\n throw new EnvironmentError(\r\n 'This functionality requires Node.js environment',\r\n 'node',\r\n getEnvironment()\r\n );\r\n }\r\n}\r\n\r\n/**\r\n * Asserts that the current environment is a browser\r\n * @throws {EnvironmentError} if not running in a browser\r\n */\r\nexport function assertBrowserEnvironment(): void {\r\n if (!isBrowserEnvironment()) {\r\n throw new EnvironmentError(\r\n 'This functionality requires browser environment',\r\n 'browser',\r\n getEnvironment()\r\n );\r\n }\r\n}\r\n"]}