/** * @packageDocumentation * @module utils */ import assert from 'assert'; export { clampToViewport, makeDraggable } from './draggable'; export { Warnings, WarningHandler } from './warnings'; /** Callback type to position an HTML element. * * The callback gets the current placement of the settings as a * [DOMRect](https://developer.mozilla.org/en-US/docs/Web/API/DOMRect), and * should return top and left positions in pixels, used with `position: * fixed`. */ export type PositioningCallback = (rect: DOMRect) => { top: number; left: number }; export type Bounds = { x: [number, number]; y: [number, number]; z?: [number, number]; }; const STANDARD_COLORS = [ 'red', 'yellow', 'green', 'blue', 'orange', 'aqua', 'purple', 'teal', 'silver', ]; export function getNextColor(alreadyUsedColors: string[]): string { for (const color of STANDARD_COLORS) { if (!alreadyUsedColors.includes(color)) { return color; } } throw Error('required more colors than available'); } /** Type to separate generic strings from GUID */ declare const tag: unique symbol; export type GUID = string & { readonly [tag]: 'guid' }; /** Generate a new GUID */ export function generateGUID(): GUID { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }) as GUID; } /** * Get an HTML element by id, looking inside the `root` (by default the whole * `document`). * * The generic parameter `HTMLType` can be used to cast the element to a given * type (e.g. `getById("foo")`) . The element type is not * checked by this function. * * @throws if there is not element with the given id. */ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters export function getByID( id: string, root: Document | HTMLElement | ShadowRoot = document ): HTMLType { let e; if (root instanceof HTMLElement) { e = root.querySelector(`#${id}`); } else { e = root.getElementById(id); } if (e === null) { throw Error(`unable to get element with id ${id}`); } return e as unknown as HTMLType; } export function enumerate(iterable: Iterable): Iterable<[number, T]> { const iterator = function* () { let index = 0; for (const element of iterable) { yield [index, element]; index++; } }; return { [Symbol.iterator]: iterator, } as Iterable<[number, T]>; } /** * Get the first key of a Map, potentially excluding a specific key value. * * If `excluding` is specified and the first key is equal to `excluding`; the * second key will be returned. * * @param map the map where to look for keys * @param excluding do not use this specific value if it is the first key * @return the first or second key depending on `excluding` */ export function getFirstKey(map: Map, excluding?: K): K { assert(map.size >= 1); const keys = map.keys(); const first = keys.next().value as K; if (excluding !== undefined) { if (first === excluding) { assert(map.size >= 2); return keys.next().value as K; } } return first; } // get the max/min of an array. Math.min(...array) fails with very large arrays export function arrayMaxMin(values: Array): { max: number; min: number } { let max = Number.NEGATIVE_INFINITY; let min = Number.POSITIVE_INFINITY; for (const value of values) { if (value === null) { continue; } if (value > max && isFinite(value)) { max = value; } if (value < min && isFinite(value)) { min = value; } } assert(isFinite(min) && isFinite(max)); return { max, min }; } /** unreachable marker for cases that REALLY should not happen */ export function unreachable(): never { throw Error('unreachable code entered, this is a bug'); } /** Returns the element based on ID or the element itself*/ export function getElement(element: string | HTMLElement): HTMLElement { if (typeof element !== 'string') { assert(element instanceof HTMLElement); return element; } else { return getByID(element); } } /** * check for the position of `value` in `array` using binary search. * The array must be sorted for this to work */ export function binarySearch(array: number[], value: number): number { let min = 0; let max = array.length - 1; let mid; while (min <= max) { mid = (min + max) >>> 1; if (array[mid] === value) { return mid; } else if (array[mid] < value) { min = mid + 1; } else { max = mid - 1; } } return -1; } /** Try to format a float to ensure it fits in a fixed characters limit */ export function fixedWidthFloat(value: number, width: number = 4): string { if (value === 0) { return '0'; } else if (value > 0) { // we can use the full 'width' characters for the number if (value < Math.pow(10, width) && value > 0.9 * Math.pow(0.1, width - 2)) { if (Number.isInteger(value)) { return value.toString(); } else { // convert to fixed format with width-1 decimals, then truncate to fit // in width characters let result = value.toFixed(width - 1).substring(0, width); if (result[3] === '.') { result = result.substring(0, width - 1); } return result; } } else { // convert to exponential format with no decimal part return value.toExponential(0).replace('+', ''); } } else { // we only have 3 characters, we need one for the '-' sign if (value > -Math.pow(10, width - 1) && value < -0.9 * Math.pow(0.1, width - 3)) { if (Number.isInteger(value)) { return value.toString(); } else { // convert to fixed format with width-2 decimals (minus!) // then truncate to fit in width characters let result = value.toFixed(width - 1).substring(0, width); if (result[3] === '.') { result = result.substring(0, width); } return result; } } else { // convert to exponential format with no decimal part return value.toExponential(0).replace('+', ''); } } } /** * Generates a formatted label for a property with a given transformation. * * @param property The name of the property. * @param transform The transformation applied ('inverse', 'log', 'sqrt', 'linear'). * @param html_format If true, uses HTML tags (e.g., ). If false, uses Unicode characters. * @returns The formatted label string. */ export function transformedAxisLabel( property: string, transform: string, html_format: boolean ): string { let title = property; switch (transform) { case 'inverse': title = html_format ? `(${title})-1` : `(${title})⁻¹`; break; case 'log': title = html_format ? `log10(${title})` : `log₁₀(${title})`; break; case 'sqrt': title = html_format ? `√(${title})` : `√(${title})`; break; case 'linear': break; default: break; } return title; }