Implementation:Apache Druid Formatting And Array Manipulation
| Knowledge Sources | |
|---|---|
| Domains | Web Console, Utilities |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Provides a comprehensive collection of general-purpose utility functions used throughout the Apache Druid web console.
Description
This module exports a wide range of utility functions covering type checking, number and duration formatting, array manipulation, string operations, clipboard interaction, and UI helpers. These functions form the foundational utility layer of the web console, used by views, components, and other utilities. The module avoids external state and focuses on pure functions for data transformation and formatting.
Usage
Import individual utility functions as needed from ../../utils or directly from ../../utils/general. These functions are used pervasively across the web console codebase for formatting display values, manipulating arrays, and performing common operations.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/utils/general.tsx
- Lines: 1-743
Signature
// Type checking
export type NumberLike = number | bigint;
export function isNumberLike(x: unknown): x is NumberLike
export function isNumberLikeNaN(x: NumberLike): boolean
export function nonEmptyString(s: unknown): s is string
export function nonEmptyArray(a: unknown): a is unknown[]
export function isSimpleArray(a: any): a is (string | number | boolean)[]
// Array operations
export function arraysEqualByElement<T>(xs: T[], ys: T[]): boolean
export function addOrUpdate<T>(xs: readonly T[], x: T, keyFn: (x: T) => string | number): T[]
export function countBy<T>(array: readonly T[], fn?: (x: T, index: number) => string | number): Record<string, number>
export function lookupBy<T, Q = T>(array: readonly T[], keyFn?: ..., valueFn?: ...): Record<string, Q>
export function groupBy<T, Q>(array: readonly T[], keyFn: ..., aggregateFn: ...): Q[]
export function uniq<T>(array: readonly T[], by?: (t: T) => string): T[]
export function partition<T>(xs: T[], predicate: ...): [T[], T[]]
export function filterMap<T, Q>(xs: readonly T[], f: ...): Q[]
export function compact<T>(xs: (T | undefined | false | null | '')[]): T[]
export function without<T>(xs: readonly T[], x: T | undefined): T[]
// Number formatting
export function formatInteger(n: NumberLike): string
export function formatNumber(n: NumberLike): string
export function formatBytes(n: NumberLike, useBinaryBytes?: boolean): string
export function formatPercent(n: NumberLike): string
export function formatDuration(ms: NumberLike): string
export function formatMillions(n: NumberLike): string
// String operations
export function caseInsensitiveEquals(str1: string | undefined, str2: string | undefined): boolean
export function caseInsensitiveContains(testString: string, searchString: string): boolean
export function capitalizeFirst(str: string): string
export function pluralIfNeeded(n: NumberLike, singular: string, plural?: string): string
// Utility
export function oneOf<T>(value: T, ...options: T[]): boolean
export function wait(ms: number, signal?: AbortSignal): Promise<void>
export function clamp(n: number, min?: number, max?: number): number
export function copyAndAlert(copyString: string, alertMessage: string): void
export function hashJoaat(str: string): number
// Constants
export const EMPTY_OBJECT: any;
export const EMPTY_ARRAY: any[];
export const EXPERIMENTAL_ICON: JSX.Element;
Import
import {
formatBytes,
formatInteger,
pluralIfNeeded,
oneOf,
compact,
filterMap,
// ... other needed utilities
} from '../../utils';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (varies) | (varies) | (varies) | Each exported function has its own parameters. See Signature section for individual function signatures. |
Outputs
| Name | Type | Description |
|---|---|---|
| (varies) | (varies) | Each function returns its own type. Formatting functions return strings; type guards return booleans; array functions return new arrays. |
Usage Examples
Formatting Numbers and Durations
import { formatBytes, formatDuration, formatPercent, formatInteger } from '../../utils';
formatBytes(1048576); // "1.00 MB"
formatDuration(3661000); // "1:01:01"
formatPercent(0.856); // "85.60%"
formatInteger(1234567); // "1,234,567"
Array Manipulation
import { compact, filterMap, uniq, countBy, oneOf } from '../../utils';
compact([1, null, 2, false, 3, '']); // [1, 2, 3]
filterMap([1, 2, 3, 4], x => x > 2 ? x * 10 : undefined); // [30, 40]
uniq(['a', 'b', 'a', 'c']); // ['a', 'b', 'c']
countBy(['a', 'b', 'a', 'c']); // { a: 2, b: 1, c: 1 }
oneOf('foo', 'foo', 'bar', 'baz'); // true
Clipboard with Toast Notification
import { copyAndAlert } from '../../utils';
copyAndAlert(segmentId, 'Segment ID copied to clipboard');
Function Categories
Type Guards
isNumberLike-- checks if a value isnumberorbigintnonEmptyString-- checks if a value is a non-empty stringnonEmptyArray-- checks if a value is a non-empty arrayisSimpleArray-- checks if an array contains only primitives
Formatting
formatInteger,formatNumber,formatNumberAbbreviated-- number displayformatBytes,formatBytesCompact,formatMegabytes-- byte sizesformatRate,formatByteRate,formatByteRateCompact-- ratesformatPercent,formatPercentClapped-- percentagesformatDuration,formatDurationWithMs,formatDurationHybrid-- time durationsformatMillions-- large number displaypluralIfNeeded-- pluralization with counttimezoneOffsetInMinutesToString-- timezone offset formatting
Array/Collection Utilities
countBy,lookupBy,groupBy,groupByAsMap-- aggregationmapRecord,mapRecordOrReturn-- record transformationpartition,filterMap,filterMapOrReturn,filterOrReturn-- filteringcompact,assemble-- falsy value removaluniq,allSameValue-- deduplicationwithout,change,changeByIndex-- element replacementswapElements,moveElement,moveToIndex,moveToEnd-- reorderingarrangeWithPrefixSuffix-- ordered arrangementminBy,findMap-- searching
Misc Utilities
wait,delay-- async delays with optional abortclamp-- number clampingcopyAndAlert-- clipboard with toasthashJoaat-- Jenkins one-at-a-time hashparseCsvLine-- CSV line parsingstringifyValue-- generic value to stringoffsetToRowColumn-- text offset to row/column conversionxor,toggle-- boolean/set operationshasOverlayOpen-- Blueprint.js overlay detectiongenerate8HexId-- random ID generation