Skip to content

Commit

Permalink
Use a helper for formatting numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenwerk committed Dec 11, 2024
1 parent 1a20911 commit 415eb6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/host/app/components/with-subscription-data.gts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IconHexagon } from '@cardstack/boxel-ui/icons';
import BillingService from '../services/billing-service';

import type { ComponentLike } from '@glint/template';
import { formatNumber } from '../helpers/format-number';

interface ValueSignature {
Args: {
Expand Down Expand Up @@ -102,7 +103,9 @@ export default class WithSubscriptionData extends Component<WithSubscriptionData
private get monthlyCreditText() {
return this.creditsAvailableInPlanAllowance != null &&
this.creditsIncludedInPlanAllowance != null
? `${this.creditsAvailableInPlanAllowance} of ${this.creditsIncludedInPlanAllowance} left`
? `${formatNumber(
this.creditsAvailableInPlanAllowance,
)} of ${formatNumber(this.creditsIncludedInPlanAllowance)} left`
: null;
}

Expand Down Expand Up @@ -149,7 +152,7 @@ export default class WithSubscriptionData extends Component<WithSubscriptionData
additionalCredit=(component
Value
tag='additional-credit'
value=this.extraCreditsAvailableInBalance
value=(formatNumber this.extraCreditsAvailableInBalance)
isLoading=this.isLoading
isOutOfCredit=this.isOutOfCredit
displayCreditIcon=true
Expand Down
20 changes: 20 additions & 0 deletions packages/host/app/helpers/format-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { helper } from '@ember/component/helper';

type PositionalArgs = number;

interface Signature {
Args: {
Positional: PositionalArgs;
};
Return: string;
}

export function formatNumber(number?: number | null): string {
if (number == undefined || number == null) {
return '';
}

return number.toLocaleString();
}

export default helper<Signature>(formatNumber);
8 changes: 5 additions & 3 deletions packages/host/app/services/billing-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
encodeWebSafeBase64,
} from '@cardstack/runtime-common';

import { formatNumber } from '../helpers/format-number';

import NetworkService from './network';
import RealmServerService from './realm-server';
import ResetService from './reset';
Expand Down Expand Up @@ -89,9 +91,9 @@ export default class BillingService extends Service {
.sort((a, b) => a.creditReloadAmount - b.creditReloadAmount)
.map((link) => ({
...link,
amountFormatted: `${link.creditReloadAmount.toLocaleString(
'en-US',
)} credits for $${link.price.toLocaleString('en-US')}`,
amountFormatted: `${formatNumber(
link.creditReloadAmount,
)} credits for $${formatNumber(link.price)}`,
}));
}

Expand Down

0 comments on commit 415eb6c

Please sign in to comment.