All files / src/api/analytics util.ts

100% Statements 114/114
97.95% Branches 48/49
100% Functions 8/8
100% Lines 114/114

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 1141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 67x 67x 22x 22x 22x 10x 10x 1x 1x 1x 1x 1x 1x 1x 48x 48x 1x 21x 21x 1x 1x 1x 1x 1x 40x 72x 72x 72x 72x 9x 9x 1x 1x 1x 1x 1x 54x 129x 129x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 8x 4x 4x 8x 8x 8x 4x 4x 4x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x
/**
* @file util.ts
* @module api/analytics/util
*
* @summary
* Internal helpers for safe coercions, param normalization, and tolerant parsing.
* These utilities are internal to the API layer and are not exported to UI code directly.
* @enterprise
* - Defensive type coercion for robust data handling
* - Tolerant parsing of backend responses to prevent UI breakage
* - Parameter normalization for consistent API requests
* - TypeDoc documentation for utility functions
*/
import type { AnalyticsParams } from './validation';
import type { ItemRef } from './types';
import { getTodayIso, getDaysAgoIso } from '../../utils/formatters';
 
/** Defensive number coercion (NaN → 0). 
 * Handles numbers and numeric strings; returns 0 for others.
 * Useful for parsing backend data with uncertain types.
 * Examples:
 * - asNumber(42) → 42
 * - asNumber("3.14") → 3.14
 * - asNumber("foo") → 0
 * - asNumber(null) → 0
 * - asNumber(undefined) → 0
 * - asNumber([]) → 0
 * - asNumber({}) → 0
 * 
*/
export function asNumber(v: unknown): number {
    if (typeof v === 'number') return Number.isFinite(v) ? v : 0;
    if (typeof v === 'string' && v.trim() !== '') {
        const n = Number(v);
        return Number.isFinite(n) ? n : 0;
    }
    return 0;
}
 
/** Narrow unknown to record. 
 * Returns false for null/arrays.
 * Use isArrayOfRecords to check for arrays of records.
*/
export type Rec = Record<string, unknown>;
export function isRecord(x: unknown): x is Rec {
    return !!x && typeof x === 'object' && !Array.isArray(x);
}
export function isArrayOfRecords(x: unknown): x is Rec[] {
    return Array.isArray(x) && x.every(isRecord);
}
 
/** Try multiple keys; return first string/number encountered as string. 
 * Returns empty string if none found.
*/
export function pickString(r: Rec, keys: string[]): string {
    for (const k of keys) {
        const v = r[k];
        if (typeof v === 'string') return v;
        if (typeof v === 'number') return String(v);
    }
    return '';
}
 
/** Try multiple keys; return first numeric-like value. 
 * Returns 0 if none found or non-numeric.
*/
export function pickNumber(r: Rec, keys: string[]): number {
    for (const k of keys) {
        if (k in r) return asNumber(r[k]);
    }
    return 0;
}
 
/** Normalize backend rows into `{ id, name }[]` safely. 
 * Filters out invalid entries.
 * Accepts either `id` or `itemId` for the identifier,
 * and `name` or `itemName` for the display name.
 * Optionally includes `supplierId` if present.
*/
export function normalizeItemsList(data: unknown): ItemRef[] {
    if (!Array.isArray(data)) return [];
    return (data as Array<{ id?: string | number; itemId?: string | number; name?: string; itemName?: string; supplierId?: string | number | null }>)
    .map((d) => ({
        id: String(d.id ?? d.itemId ?? ''),
        name: String(d.name ?? d.itemName ?? ''),
        supplierId: typeof d.supplierId === 'string' || typeof d.supplierId === 'number' ? String(d.supplierId) : undefined,
    }))
    .filter((it) => it.id && it.name);
}
 
/** Client-side filter as a safety net if the BE ignores search params. 
 * Returns at most `limit` items whose names include `q` (case-insensitive).
 * If `q` is empty/whitespace, returns the first `limit` items.
*/
export function clientFilter(items: ItemRef[], q: string, limit: number): ItemRef[] {
    const needle = q.trim().toLowerCase();
    if (!needle) return items.slice(0, limit);
    return items.filter((it) => it.name.toLowerCase().includes(needle)).slice(0, limit);
}
 
/**
 * Normalize FE filter parameters to BE query params.
 * BE expects `start` / `end` (LocalDate), optional `supplierId`.
 * If the caller omits dates, default to the last 180 days.
*/
export function paramClean(p?: AnalyticsParams): Record<string, string> {
    const out: Record<string, string> = {};
    const from = p?.from ?? getDaysAgoIso(180);
    const to = p?.to ?? getTodayIso();
    out.start = from;
    out.end = to;
    if (p?.supplierId) out.supplierId = p.supplierId;
    return out;
}