Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Druid Formatting And Array Manipulation

From Leeroopedia
Revision as of 14:14, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Apache_Druid_Formatting_And_Array_Manipulation.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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 is number or bigint
  • nonEmptyString -- checks if a value is a non-empty string
  • nonEmptyArray -- checks if a value is a non-empty array
  • isSimpleArray -- checks if an array contains only primitives

Formatting

  • formatInteger, formatNumber, formatNumberAbbreviated -- number display
  • formatBytes, formatBytesCompact, formatMegabytes -- byte sizes
  • formatRate, formatByteRate, formatByteRateCompact -- rates
  • formatPercent, formatPercentClapped -- percentages
  • formatDuration, formatDurationWithMs, formatDurationHybrid -- time durations
  • formatMillions -- large number display
  • pluralIfNeeded -- pluralization with count
  • timezoneOffsetInMinutesToString -- timezone offset formatting

Array/Collection Utilities

  • countBy, lookupBy, groupBy, groupByAsMap -- aggregation
  • mapRecord, mapRecordOrReturn -- record transformation
  • partition, filterMap, filterMapOrReturn, filterOrReturn -- filtering
  • compact, assemble -- falsy value removal
  • uniq, allSameValue -- deduplication
  • without, change, changeByIndex -- element replacement
  • swapElements, moveElement, moveToIndex, moveToEnd -- reordering
  • arrangeWithPrefixSuffix -- ordered arrangement
  • minBy, findMap -- searching

Misc Utilities

  • wait, delay -- async delays with optional abort
  • clamp -- number clamping
  • copyAndAlert -- clipboard with toast
  • hashJoaat -- Jenkins one-at-a-time hash
  • parseCsvLine -- CSV line parsing
  • stringifyValue -- generic value to string
  • offsetToRowColumn -- text offset to row/column conversion
  • xor, toggle -- boolean/set operations
  • hasOverlayOpen -- Blueprint.js overlay detection
  • generate8HexId -- random ID generation

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment