aboutsummaryrefslogtreecommitdiff
path: root/jb-ui/src/lib/utils.ts
blob: 0c84192aff8bfe50d0b63b39a08584d293a95af5 (plain)
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
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import { Source } from "../api_fsb"

export const routeBasename = import.meta.env.VITE_BASENAME;
export const tagManager = import.meta.env.VITE_TAG_MANAGER
export const bpid = import.meta.env.VITE_BPID;

export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs))
}

export function assert(condition: any, msg?: string): asserts condition {
    if (!condition) {
        throw new Error(msg);
    }
}

export function toCapitalized(word: string): string {
    return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}

export function truncate(originalText: string, maxLength: number = 10, replaceStr = "…"): string {
    if (originalText && originalText.length > maxLength) {
        return originalText.substring(0, maxLength) + replaceStr;
    }
    return originalText;
}

export function usdCentFmt(value: number): string {
    return (value / 100).toLocaleString('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    })
}

export function formatStatus(status: string): string {
    const table = {
        'c': 'Completed',
        't': 'Terminated',
        'f': 'Failed',
        'e': 'Entered',
    }

    return table[status as keyof typeof table] ?? "Unknown";
}

export function formatSource(source: Source): string {
    const table = {
        'g': 'GRS',
        'c': 'Cint',
        'a': 'Dalia',
        'd': 'Dynata',
        'et': 'Etx',
        'f': 'Full Circle',
        'i': 'InnovateMR',
        'l': 'Lucid',
        'm': 'Morning Consult',
        'n': 'Open Labs',
        'o': 'Pollfish',
        'e': 'Precision Sample',
        'r': 'Prodege User',
        'pr': 'Prodege',
        'p': 'Pulley',
        'rd': 'Rep Data',
        'h': 'Sago',
        's': 'Pure Spectrum',
        't': 'Testing',
        'u': 'Testing 2',
        'w': 'WXET'
    }

    return table[source]
}

export function formatSecondsVerbose(seconds: number): string {
    const mins = Math.floor(seconds / 60)
    const secs = seconds % 60

    const parts = []
    if (mins > 0) parts.push(`${mins} min`)
    if (secs > 0 || mins === 0) {
        parts.push(`${secs.toLocaleString('en-US', {
            minimumFractionDigits: 0,
            maximumFractionDigits: 1
        })} sec`)
    }

    return parts.join(" ")
}

export function formatSeconds(seconds: number): string {
    const mins = Math.floor(seconds / 60)
    const secs = seconds % 60
    const paddedSecs = secs.toString().padStart(2, '0')
    return `${mins}:${paddedSecs}`
}

export function formatCentsToUSD(cents: number): string {
    return new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
    }).format(cents / 100)
}

export function titleCase(str: string): string {
    return str
        .split(" ")
        .map(word => word.charAt(0).toUpperCase() + word.slice(1))
        .join(" ");
}