@cmchu/device-marker
v0.0.1
Published
This library provides a set of utilities for managing and interacting with device and browser components in a TypeScript-based application. It offers type definitions for devices and browsers, utility functions for converting component configurations to h
Downloads
2
Readme
device-marker Library
Overview
This library provides a set of utilities for managing and interacting with device and browser components in a TypeScript-based application. It offers type definitions for devices and browsers, utility functions for converting component configurations to hashes, and lazy-loaded functions for retrieving device and browser markers.
Key Features
- Device & Browser Types: Clear, strongly-typed definitions for devices and browsers, enabling strict type checking in your codebase.
- Component Configuration: A flexible configuration object (
Components
) that allows partial definitions of device or browser components. - Hash Generation: A function (
components2Hash
) to convert your component configurations into a unique hash, useful for caching or identification purposes. - Lazy Loading: Two lazy evaluation functions (
getDeviceMarker
andgetBrowserMarker
) for retrieving markers associated with devices and browsers respectively, promoting efficient resource usage.
Installation
Assuming this library is published, you can install it using npm or yarn:
npm install --save @cmchu/device-marker
# or
yarn add @cmchu/device-marker
Usage
Importing Types
import {
components,
components2Hash,
getDeviceMarker,
getBrowserMarker,
} from '@cmchu/device-marker';
Working with Types
- Define and use types for devices and browsers in your code.
Configuring Components
import { components } from '@cmchu/device-marker';
import type { Components } from '@cmchu/device-marker';
const { osCpu, timezone } = components;
const mycomponents: Components = {
osCpu,
timezone
};
const hash = await components2Hash(mycomponents);
Generating Hashes
Use components2Hash
to create a unique hash based on your component configuration.
async function generateComponentHash() {
const hash = await components2Hash(myComponents);
console.log(`Generated hash: ${hash}`);
}
Retrieving Markers (Lazy Loaded)
Access device and browser markers without immediate execution.
async function logMarkers() {
try {
const deviceMarker = await getDeviceMarker();
const browserMarker = await getBrowserMarker();
console.log(`Device Marker: ${deviceMarker}`);
console.log(`Browser Marker: ${browserMarker}`);
} catch (error) {
console.error('Error fetching markers:', error);
}
}
API Reference
Types
Devices
,DeviceName
,Device
Browsers
,BrowserName
,Browser
ComponentName
,Component
Components
Functions
components2Hash(components: Components): Promise<string>
Converts aComponents
object to a string hash.getDeviceMarker(): () => Promise<string>
Lazy-loaded function to fetch a device marker.getBrowserMarker(): () => Promise<string>
Lazy-loaded function to fetch a browser marker.
Conclusion
This library simplifies working with device and browser components in TypeScript applications, offering robust typing, configuration flexibility, and optimized retrieval of markers through lazy loading. Integrate it into your project for enhanced maintainability and performance.
Open Source License
This project is licensed under the [MIT License]. Contributions and usage are welcome.