A TypeScript utility library for managing cross-platform code targeting web and Node.js environments.
https://github.com/divinciest/ts-platform-aware
This repository contains a single file, platform.ts
, which provides a set of decorators and utility functions to help manage code that needs to run on different platforms (web browsers and Node.js). It allows developers to write platform-specific code and ensure it's only executed in the appropriate environment.
- Platform detection (web, node, or unknown)
- Decorators for platform-specific methods, properties, and classes
- Utility functions for platform-specific code execution
- Cross-platform method and property decorators
- Platform assertion functionality
- Support for simulating different platforms (useful for testing)
To use this library in your TypeScript project, you can import the necessary functions and decorators from the platform.ts
file:
import {
platformSpecificMethod,
platformSpecificProperty,
platformSpecificClass,
crossPlatformMethod,
crossPlatformProperty,
isWeb,
isNode,
assertPlatform,
// ... other imports as needed
} from './platform';
- Creating a platform-specific method:
class MyClass {
@platformSpecificMethod('web')
webOnlyMethod() {
console.log('This method only runs in web environments');
}
}
- Creating a platform-specific property:
class MyClass {
@platformSpecificProperty('node')
nodeOnlyProperty: string = 'This property is only accessible in Node.js';
}
- Creating a platform-specific class:
@platformSpecificClass('web')
class WebOnlyClass {
// This class can only be instantiated in web environments
}
- Using utility functions:
if (isWeb()) {
// Web-specific code
} else if (isNode()) {
// Node.js-specific code
}
assertPlatform('web'); // Throws an error if not running in a web environment
Contributions to improve the platform.ts
file or expand its functionality are welcome. Please submit issues or pull requests through the GitHub repository.