hybrid-webcache
Version:
Hybrid WebCache - A library that combines `localStorage`, `IndexedDB`, `SessionStorage` and `Memory` to provide a high-performance hybrid cache with multi-instance synchronization support.
81 lines (80 loc) • 3.12 kB
TypeScript
import type { KeyPath, TTL } from "../types";
/**
* Class Helper
* @internal
* @ignore
*/
export declare const Utils: {
/**
* Extracts the primary key from a given KeyPath.
*
* If the KeyPath is an array, the method returns the first element as a string.
* If the KeyPath is a string with indexing format (e.g., "keyName[0].name"),
* the method returns the first part before a "." or "[".
*
* @param keyPath - The KeyPath from which to extract the primary key.
* @returns The extracted primary key as a string.
*/
getKey(keyPath: KeyPath): string;
/**
* Converts a TTL to milliseconds.
*
* If the TTL is a number, it is returned as is.
* If the TTL is an object, the method sums up the milliseconds from the
* following properties: seconds, minutes, hours, and days.
*
* @param ttl - The TTL to convert.
* @returns The TTL converted to milliseconds.
*/
convertTTLToMilliseconds(ttl: TTL): number;
/**
* Checks if a given expiresAt timestamp has expired.
*
* If the expiresAt timestamp is greater than 0, the method checks if the
* current time is greater than or equal to the expiresAt timestamp. If the
* expiresAt timestamp is 0 or less, the method returns false.
*
* @param expiresAt - The timestamp to check.
* @returns True if the timestamp has expired, false otherwise.
*/
isExpired(expiresAt: number): boolean;
/**
* Checks if the sessionStorage is available.
*
* This method tries to set and remove an item from the sessionStorage.
* If the operation is successful, it returns true. Otherwise, it returns false.
*
* @returns true if the sessionStorage is available, false otherwise.
*/
isSessionStorageAvailable(): boolean;
/**
* Checks if the localStorage is available.
*
* This method tries to set and remove an item from the localStorage.
* If the operation is successful, it returns true. Otherwise, it returns false.
*
* @returns true if the localStorage is available, false otherwise.
*/
isLocalStorageAvailable(): boolean;
/**
* Checks if the IndexedDB is available.
*
* This method simply checks if the IndexedDB is available in the window object.
* If the IndexedDB is available, it returns true. Otherwise, it returns false.
*
* @returns true if the IndexedDB is available, false otherwise.
*/
isIndexedDBAvailable(): boolean;
/**
* Calculates the size of the given bytes as a human-readable string.
*
* If the bytes is 0 or less, the method returns "0b".
* If the bytes is less than 1024, the method returns the size in bytes.
* Otherwise, the method calculates the size in kilobytes, megabytes, or gigabytes
* and returns the result as a string with the corresponding unit.
*
* @param bytes - The number of bytes to calculate the size for.
* @returns The calculated size as a human-readable string.
*/
calculateStorageSize(bytes: number): string;
};