fix: accept nullable marketplace prices in formatter
This commit is contained in:
@@ -5,9 +5,19 @@
|
||||
* @returns Formatted currency string
|
||||
*/
|
||||
export function formatCentsToCurrency(
|
||||
cents: number,
|
||||
cents: number | string | null | undefined,
|
||||
locale: string = "en-CA",
|
||||
): string {
|
||||
if (cents == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const numericCents =
|
||||
typeof cents === "string" ? Number.parseFloat(cents) : cents;
|
||||
if (!Number.isFinite(numericCents)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: "currency",
|
||||
@@ -15,10 +25,10 @@ export function formatCentsToCurrency(
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
return formatter.format(cents / 100);
|
||||
return formatter.format(numericCents / 100);
|
||||
} catch {
|
||||
// Fallback if locale is not supported
|
||||
const dollars = (cents / 100).toFixed(2);
|
||||
const dollars = (numericCents / 100).toFixed(2);
|
||||
return `$${dollars}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
||||
import { formatCookiesForHeader } from "../src/utils/cookies";
|
||||
import { formatCentsToCurrency } from "../src/utils/format";
|
||||
import {
|
||||
extractFacebookItemData,
|
||||
extractFacebookMarketplaceData,
|
||||
fetchFacebookItem,
|
||||
formatCentsToCurrency,
|
||||
formatCookiesForHeader,
|
||||
parseFacebookAds,
|
||||
parseFacebookCookieString,
|
||||
parseFacebookItem,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
||||
import { fetchFacebookItems } from "../src/scrapers/facebook";
|
||||
import fetchFacebookItems from "../src/scrapers/facebook";
|
||||
|
||||
// Mock fetch globally
|
||||
const originalFetch = global.fetch;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { formatCentsToCurrency, slugify } from "../src/scrapers/kijiji";
|
||||
import { slugify } from "../src/scrapers/kijiji";
|
||||
import { formatCentsToCurrency } from "../src/utils/format";
|
||||
|
||||
describe("Utility Functions", () => {
|
||||
describe("slugify", () => {
|
||||
|
||||
Reference in New Issue
Block a user