molstar
Version:
A comprehensive macromolecular library.
70 lines (69 loc) • 3.6 kB
TypeScript
/**
* Copyright (c) 2023-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Adam Midlik <midlik@gmail.com>
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { StateObject } from '../../../mol-state';
import { Color } from '../../../mol-util/color';
import { ColorNames } from '../../../mol-util/color/names';
/** Represents either the result or the reason of failure of an operation that might have failed */
export type Maybe<T> = {
ok: true;
value: T;
} | {
ok: false;
error: any;
};
/** Try to await a promise and return an object with its result (if resolved) or with the error (if rejected) */
export declare function safePromise<T>(promise: T): Promise<Maybe<Awaited<T>>>;
/** A map where values are arrays. Handles missing keys when adding values. */
export declare class MultiMap<K, V> implements Mapping<K, V[]> {
private _map;
/** Return the array of values assidned to a key (or `undefined` if no such values) */
get(key: K): V[] | undefined;
/** Append value to a key (handles missing keys) */
add(key: K, value: V): void;
}
/** Basic subset of `Map<K, V>`, only needs to have `get` method */
export type Mapping<K, V> = Pick<Map<K, V>, 'get'>;
/** Implementation of `Map` where keys are integers
* and most keys are expected to be from interval `[0, limit)`.
* For the keys within this interval, performance is better than `Map` (implemented by array).
* For the keys out of this interval, performance is slightly worse than `Map`. */
export declare class NumberMap<K extends number, V> implements Mapping<K, V> {
readonly limit: K;
private array;
private map;
constructor(limit: K);
get(key: K): V | undefined;
set(key: K, value: V): void;
}
/** Return `true` if `value` is not `undefined` or `null`.
* Prefer this over `value !== undefined`
* (for maybe if we want to allow `null` in `AnnotationRow` in the future) */
export declare function isDefined<T>(value: T | undefined | null): value is T;
/** Return `true` if at least one of `values` is not `undefined` or `null`. */
export declare function isAnyDefined(...values: any[]): boolean;
/** Return filtered array containing all original elements except `undefined` or `null`. */
export declare function filterDefined<T>(elements: (T | undefined | null)[]): T[];
/** Create an 16-hex-character hash for a given input string, e.g. 'spanish inquisition' -> '7f9ac4be544330be'*/
export declare function stringHash(input: string): string;
/** Return type of elements in a set */
export type ElementOfSet<S> = S extends Set<infer T> ? T : never;
/** Convert `colorString` (either X11 color name like 'magenta' or hex code like '#ff00ff') to Color.
* Return `undefined` if `colorString` cannot be converted. */
export declare function decodeColor(colorString: string | undefined | null): Color | undefined;
/** Hexadecimal color string, e.g. '#FF1100' (the type matches more than just valid HexColor strings) */
export type HexColor = `#${string}`;
export declare const HexColor: {
/** Decide if a string is a valid hexadecimal color string (6-digit or 3-digit, e.g. '#FF1100' or '#f10') */
is(str: any): str is HexColor;
};
/** Named color string, e.g. 'red' */
export type ColorName = keyof ColorNames;
export declare const ColorName: {
/** Decide if a string is a valid named color string */
is(str: any): str is ColorName;
};
export declare function collectMVSReferences<T extends StateObject.Ctor>(type: T[], dependencies: Record<string, StateObject>): Record<string, StateObject.From<T>['data']>;