Skip to content

Commit

Permalink
export currency with correct decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Nov 14, 2024
1 parent 755f959 commit 24ab1a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Updated
- GTFS export currency with correct number of decimal places

## [4.15.7] - 2024-11-14

### Updated
Expand Down
10 changes: 9 additions & 1 deletion src/lib/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as models from '../models/models.ts';
import { openDb } from './db.ts';
import { prepDirectory, generateFolderName } from './file-utils.ts';
import { log, logWarning } from './log-utils.ts';
import { setDefaultConfig } from './utils.ts';
import { formatCurrency, setDefaultConfig } from './utils.ts';

import { Config, Model } from '../types/global_interfaces.ts';

Expand Down Expand Up @@ -117,6 +117,14 @@ export const exportGtfs = async (initialConfig: Config) => {
if (!routesWithAgencyId || routesWithAgencyId.length === 0) {
excludeColumns.push('agency_id');
}
} else if (model.filenameBase === 'fare_attributes') {
for (const line of lines) {
line.price = formatCurrency(line.price, line.currency_type);
}
} else if (model.filenameBase === 'fare_products') {
for (const line of lines) {
line.price = formatCurrency(line.amount, line.currency);
}
}

const columns = without(
Expand Down
25 changes: 25 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,28 @@ export function getDayOfWeekFromDate(date: number): string {

return DAYS_OF_WEEK[dateObj.getDay()];
}

/**
* Formats a numeric value according to the decimal precision rules of the specified currency,
* without any currency symbols or separators.
* @param value The numeric value to format (e.g., 10.5)
* @param currency The ISO 4217 currency code (e.g., 'USD', 'JPY', 'EUR')
* @returns The formatted string with appropriate decimal places
* Examples:
* - formatCurrency(10.5, 'USD') => '10.50' // USD uses 2 decimal places
* - formatCurrency(10.5, 'JPY') => '10' // JPY uses 0 decimal places
* - formatCurrency(10.523, 'BHD') => '10.523' // BHD uses 3 decimal places
*/
export function formatCurrency(value: number, currency: string) {
const parts = new Intl.NumberFormat(undefined, {
style: 'currency',
currency,
}).formatToParts(value);

const integerPart =
parts.find((part) => part.type === 'integer')?.value ?? '0';
const fractionPart =
parts.find((part) => part.type === 'fraction')?.value ?? '';

return `${integerPart}${fractionPart !== '' ? `.${fractionPart}` : ''}`;
}

0 comments on commit 24ab1a3

Please sign in to comment.