Skip to content

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.

Notifications You must be signed in to change notification settings

divinciest/ts-platform-aware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

ts-platform-aware

A TypeScript utility library for managing cross-platform code targeting web and Node.js environments.

Repository URL

https://github.com/divinciest/ts-platform-aware

Overview

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.

Features

  • 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)

Usage

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';

Examples

  1. Creating a platform-specific method:
class MyClass {
  @platformSpecificMethod('web')
  webOnlyMethod() {
    console.log('This method only runs in web environments');
  }
}
  1. Creating a platform-specific property:
class MyClass {
  @platformSpecificProperty('node')
  nodeOnlyProperty: string = 'This property is only accessible in Node.js';
}
  1. Creating a platform-specific class:
@platformSpecificClass('web')
class WebOnlyClass {
  // This class can only be instantiated in web environments
}
  1. 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

Contributing

Contributions to improve the platform.ts file or expand its functionality are welcome. Please submit issues or pull requests through the GitHub repository.

License

MIT License

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published