# Utility Functions The `Tools.ts` module provides a comprehensive collection of utility functions for common programming tasks. It includes functions for array manipulation, object handling, type checking, math operations, and more. ## Types ```typescript type Nil = null | undefined type Maybe = T | undefined type Obj = Record ``` ## Categories ### Type Checking & Basic Operations ```typescript // Check if value is null or undefined isNil(x: any): boolean // Execute function if value exists ifLet(x: T | undefined, f: (x: T) => void) // Return value unchanged identity(x: T): T // Create function that always returns same value always(x: T): () => T // Logical NOT not(x: any): boolean // Create complement of a predicate function complement(f: (...args: T) => any): (...args: T) => boolean ``` ### Array Operations ```typescript // Get first element first(xs: T[]): T | undefined // Get first element of first array ffirst(xs: T[][]): T | undefined // Get last element last(xs: T[]): T | undefined // Drop first n elements drop(n: number, xs: T[]): T[] // Take first n elements take(n: number, xs: T[]): T[] // Remove duplicates uniq(xs: T[]): T[] // Remove duplicates by key function uniqBy(f: (x: T) => any, xs: T[]): T[] // Create array of n items using generator function initArray(n: number, f: () => T): T[] // Split array into chunks chunk(chunkLength: number, xs: T[]): T[][] // Split array into n chunks chunks(n: number, xs: T[]): T[][] ``` ### Object Operations ```typescript // Create object excluding specified keys omit(ks: string[], x: T): T // Create object excluding entries with specified values omitVals(xs: any[], x: T): T // Create object with only specified keys pick(ks: string[], x: T): T // Transform object keys mapKeys(f: (v: string) => string, x: T): T // Transform object values mapVals(f: (v: V) => U, x: Record): Record // Merge objects (left priority) mergeLeft(a: T, b: T): T // Merge objects (right priority) mergeRight(a: T, b: T): T // Deep merge objects deepMergeLeft(a: Obj, b: Obj): Obj deepMergeRight(a: Obj, b: Obj): Obj ``` ### Number Operations ```typescript // Convert Maybe to number num(x: Maybe): number // Basic arithmetic with Maybe add(x: Maybe, y: Maybe): number sub(x: Maybe, y: Maybe): number mul(x: Maybe, y: Maybe): number div(x: Maybe, y: number): number // Increment/Decrement inc(x: Maybe): number dec(x: Maybe): number // Comparisons lt(x: Maybe, y: Maybe): boolean lte(x: Maybe, y: Maybe): boolean gt(x: Maybe, y: Maybe): boolean gte(x: Maybe, y: Maybe): boolean // Array number operations max(xs: Maybe[]): number min(xs: Maybe[]): number sum(xs: Maybe[]): number avg(xs: Maybe[]): number ``` ### String Operations ```typescript // Truncate string with ellipsis ellipsize(s: string, l: number, suffix = "..."): string // URL operations stripProtocol(url: string): string displayUrl(url: string): string displayDomain(url: string): string // Bech32 encoding/decoding hexToBech32(prefix: string, hex: string): string bech32ToHex(b32: string): string ``` ### Collection Operations ```typescript // Create union of arrays union(a: T[], b: T[]): T[] // Get intersection of arrays intersection(a: T[], b: T[]): T[] // Get difference of arrays difference(a: T[], b: T[]): T[] // Remove element from array remove(a: T, xs: T[]): T[] // Filter array by another array without(a: T[], b: T[]): T[] // Toggle element in array toggle(x: T, xs: T[]): T[] // Group array by key function groupBy(f: (x: T) => K, xs: T[]): Map // Create map from array indexBy(f: (x: T) => K, xs: T[]): Map ``` ### Time Constants ```typescript const MINUTE = 60 const HOUR = 60 * MINUTE const DAY = 24 * HOUR const WEEK = 7 * DAY const MONTH = 30 * DAY const QUARTER = 90 * DAY const YEAR = 365 * DAY // Get current timestamp in seconds now(): number // Get timestamp from ago in seconds ago(unit: number, count = 1): number // Convert seconds to milliseconds ms(seconds: number): number ``` ### Function Utilities ```typescript // Create function that executes once once(f: (...args: any) => void): (...args: any) => void // Memoize function results memoize(f: (...args: any[]) => T): (...args: any[]) => T // Create throttled function throttle any>( ms: number, f: F ): F // Create batching function batch( t: number, f: (xs: T[]) => void ): (x: T) => void ``` ### Network Utilities ```typescript // Fetch JSON with options fetchJson(url: string, opts?: FetchOpts): Promise // Post JSON data postJson(url: string, data: T, opts?: FetchOpts): Promise // Upload file uploadFile(url: string, file: File): Promise ``` ## Usage Examples ```typescript // Array operations const nums = [1, 2, 2, 3, 3, 3] uniq(nums) // => [1, 2, 3] // Object operations const obj = {a: 1, b: 2, c: 3} omit(['a', 'b'], obj) // => {c: 3} // Number operations add(5, undefined) // => 5 inc(undefined) // => 1 // Time operations ago(DAY, 2) // => timestamp from 2 days ago // URL operations displayUrl('https://www.example.com/') // => 'example.com' // Collection operations const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}] indexBy(u => u.id, users) // => Map(1 => {id: 1, name: 'Alice'}, ...) // Function utilities const throttledFn = throttle(1000, () => console.log('throttled')) ```