Line data Source code
1 2356 : const DEFAULT_NUMBER_FORMAT: Intl.NumberFormatOptions = {
2 : minimumFractionDigits: 2,
3 : maximumFractionDigits: 2
4 : }
5 :
6 2356 : const formatNumber = ({
7 : value,
8 : locale,
9 : options
10 : }: {
11 : value: number
12 : locale?: string
13 : options?: Intl.NumberFormatOptions
14 : }): string => {
15 50 : const merged: Intl.NumberFormatOptions = {
16 : ...DEFAULT_NUMBER_FORMAT,
17 : ...options
18 : }
19 :
20 50 : if (
21 : typeof merged.maximumFractionDigits === 'number' &&
22 : typeof merged.minimumFractionDigits === 'number' &&
23 : merged.maximumFractionDigits < merged.minimumFractionDigits
24 : ) {
25 1 : merged.minimumFractionDigits = merged.maximumFractionDigits
26 : }
27 :
28 50 : return new Intl.NumberFormat(locale, merged).format(value)
29 : }
30 :
31 : export const CREDITS_PER_USD = 211
32 : export const COMFY_CREDIT_RATE_CENTS = CREDITS_PER_USD / 100 // credits per cent
33 :
34 : export const usdToCents = (usd: number): number => Math.round(usd * 100)
35 :
36 2356 : export const centsToCredits = (cents: number): number =>
37 : Math.round((cents * CREDITS_PER_USD) / 100)
38 :
39 : export const creditsToCents = (credits: number): number =>
40 : Math.round(credits / COMFY_CREDIT_RATE_CENTS)
41 :
42 2356 : export const usdToCredits = (usd: number): number =>
43 : Math.round(usd * CREDITS_PER_USD)
44 :
45 2356 : export const microUsdToCredits = (microUsd: number): number =>
46 : (microUsd * CREDITS_PER_USD) / 1_000_000
47 :
48 : /**
49 : * True for a real charge below one whole credit. Rounding would report it as 0
50 : * (a free turn) or 1 (more than was charged), so charge displays show "<1".
51 : */
52 2356 : export const isSubCreditCharge = (credits: number): boolean =>
53 : credits > 0 && credits < 1
54 :
55 2356 : export const creditsToUsd = (credits: number): number =>
56 : Math.round((credits / CREDITS_PER_USD) * 100) / 100
57 :
58 : export type FormatOptions = {
59 : value: number
60 : locale?: string
61 : numberOptions?: Intl.NumberFormatOptions
62 : }
63 :
64 : export type FormatFromCentsOptions = {
65 : cents: number
66 : locale?: string
67 : numberOptions?: Intl.NumberFormatOptions
68 : }
69 :
70 : export type FormatFromUsdOptions = {
71 : usd: number
72 : locale?: string
73 : numberOptions?: Intl.NumberFormatOptions
74 : }
75 :
76 2356 : export const formatCredits = ({
77 : value,
78 : locale,
79 : numberOptions
80 : }: FormatOptions): string =>
81 : formatNumber({ value, locale, options: numberOptions })
82 :
83 2356 : export const formatCreditsFromCents = ({
84 : cents,
85 : locale,
86 : numberOptions
87 : }: FormatFromCentsOptions): string =>
88 : formatCredits({
89 : value: centsToCredits(cents),
90 : locale,
91 : numberOptions
92 : })
93 :
94 : export const formatCreditsFromUsd = ({
95 : usd,
96 : locale,
97 : numberOptions
98 : }: FormatFromUsdOptions): string =>
99 : formatCredits({
100 : value: usdToCredits(usd),
101 : locale,
102 : numberOptions
103 : })
104 :
105 2356 : export const formatUsd = ({
106 : value,
107 : locale,
108 : numberOptions
109 : }: FormatOptions): string =>
110 : formatNumber({
111 : value,
112 : locale,
113 : options: numberOptions
114 : })
115 :
116 2356 : export const formatUsdFromCents = ({
117 : cents,
118 : locale,
119 : numberOptions
120 : }: FormatFromCentsOptions): string =>
121 : formatUsd({
122 : value: cents / 100,
123 : locale,
124 : numberOptions
125 : })
126 :
127 : /**
128 : * Clamps a USD value to the allowed range for credit purchases
129 : * @param value - The USD amount to clamp
130 : * @returns The clamped value between $1 and $1000, or 0 if NaN
131 : */
132 : export const clampUsd = (value: number): number => {
133 : if (Number.isNaN(value)) return 0
134 : return Math.min(1000, Math.max(1, value))
135 : }
|