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