SPB Git

Repository Public

format-golden.mjs 3.8 KB 55 lines javascript

1// Generates Tests/VQuantKitTests/Fixtures/format_golden.json from the web formatter (client/src/lib/format.ts),2// so the Swift formatter is tested against the real thing. Run from the web repo root (read-only for it):3//   TZ=America/Toronto npx tsx <path>/Scripts/format-golden.mjs <path>/Tests/VQuantKitTests/Fixtures/format_golden.json4import { writeFileSync } from "node:fs";5import { resolve } from "node:path";6const F = await import(resolve(process.cwd(), "client/src/lib/format.ts"));7const nums = [0, 1, -1, 0.5, 2.5, -2.5, 1.005, 0.125, 1234.5, -1234.567, 999.995, 1e6, 2.5e9, 3.21e12, 12345678.9, 0.0025, -0.00004, 0.1234, 950, 12400, 1.2e6, 1e-7, 123456789012, -0.001, 999, 1000, 999999, 4978150432000, 0.07802157184239356, 79470713];8const out = { tz: process.env.TZ ?? null, node: process.version, cases: [] };9const add = (locale, fn, args, result) => out.cases.push({ locale, fn, args, result });10for (const locale of ["fr-CA", "en-CA"]) {11  F.setNumberLocale(locale, false);12  for (const v of [...nums, null]) {13    for (const d of [0, 2, 3]) {14      add(locale, "number", [v, d, false], F.formatNumber(v, { decimals: d }));15      add(locale, "number", [v, d, true], F.formatNumber(v, { decimals: d, signed: true }));16      add(locale, "percent", [v, d, false], F.formatPercent(v, { decimals: d }));17      add(locale, "compact", [v, d, false], F.formatCompact(v, { decimals: d }));18    }19    add(locale, "percent", [v, 2, true], F.formatPercent(v, { signed: true }));20    add(locale, "percentValue", [v, 2, true], F.formatPercentValue(v, { signed: true }));21    add(locale, "bps", [v, 0, false], F.formatBps(v));22    add(locale, "bps", [v, 1, true], F.formatBps(v, { decimals: 1, signed: true }));23    add(locale, "delta", [v, 2], F.formatDelta(v));24    add(locale, "int", [v], F.formatInt(v));25    add(locale, "compact", [v, 2, true], F.formatCompact(v, { signed: true }));26    if ([1234.5, -1234.567, 2.5e9, 0.5, 1, null].includes(v)) for (const c of ["USD", "CAD", "EUR", "GBP", "JPY", "CHF", "AUD", "MXN", "HKD", "CNY", "NZD", "INR", "KRW", "ILS", "BRL", "TWD", "VND", "SEK", "NOK", "DKK", "SGD", "ZAR", "PLN", "TRY", "XAF", "XOF", "XPF", "XCD", "PHP", "usd", "XYZ", "GBX", "notacode"]) {27      add(locale, "currency", [v, c, 2, false, false], F.formatCurrency(v, c));28      add(locale, "currency", [v, c, 2, true, false], F.formatCurrency(v, c, { signed: true }));29      add(locale, "currency", [v, c, 2, false, true], F.formatCurrency(v, c, { compact: true }));30    }31    add(locale, "currency", [v, "USD", 0, false, false], F.formatCurrency(v, "USD", { decimals: 0 }));32    for (const u of ["ratio", "percent", "currency", "bps", "days", "count", "per_share", null]) {33      add(locale, "byUnit", [v, u, null, false], F.formatByUnit(v, u));34      add(locale, "byUnit", [v, u, null, true], F.formatByUnit(v, u, { compact: true }));35      add(locale, "byUnit", [v, u, 1, false], F.formatByUnit(v, u, { decimals: 1, signed: true }));36    }37    add(locale, "direction", [v, 2], F.direction(v));38    add(locale, "duration", [v], F.formatDuration(v));39    add(locale, "tokens", [v], F.formatTokens(v));40    add(locale, "cost", [v], F.formatCost(v));41  }42}43F.setNumberLocale("fr-CA", false);44const now = new Date("2026-11-20T19:05:00Z").getTime();45for (const ms of [now, now - 30_000, now - 5 * 60_000, now - 3 * 3600_000, now - 20 * 3600_000, now - 2 * 86_400_000, now - 30 * 86_400_000, now - 400 * 86_400_000]) {46  add("fr-CA", "time", [ms], F.formatTime(ms));47  add("fr-CA", "dateIso", [ms], F.formatDateIso(ms));48  add("fr-CA", "date", [ms], F.formatDate(ms));49  add("fr-CA", "dateTime", [ms], F.formatDateTime(ms));50  add("fr-CA", "asOf", [ms, now], F.formatAsOf(ms, new Date(now)));51  add("fr-CA", "relative", [ms, now], F.formatRelative(ms, new Date(now)));52}53writeFileSync(process.argv[2], JSON.stringify(out) + "\n");54console.log(out.cases.length, "cases");55
Files main
Menu

Theme