Skip to content

Commit

Permalink
[format] Pass additional arguments to underlying formatDate() and `…
Browse files Browse the repository at this point in the history
…formatNumberAsStyle()` functions
  • Loading branch information
techniq committed Sep 28, 2023
1 parent 4438954 commit ebdb2c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-otters-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

[format] Pass additional arguments to underlying `formatDate()` and `formatNumberAsStyle()` functions
6 changes: 3 additions & 3 deletions src/lib/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export type FormatType = FormatNumberStyle | PeriodType | ((value: any, ...extra
/**
* Generic format which can handle Dates, Numbers, or custom format function
*/
export function format(value: any, format?: FormatType, ...extraFuncArgs) {
export function format(value: any, format?: FormatType, ...extraFuncArgs: any[]) {
let formattedValue = value ?? ''; // Do not render `null`

if (format) {
if (isFunction(format)) {
formattedValue = format(value, ...extraFuncArgs);
} else if (format in PeriodType) {
formattedValue = formatDate(value, format as PeriodType);
formattedValue = formatDate(value, format as PeriodType, ...extraFuncArgs);
} else if (typeof value === 'number') {
formattedValue = formatNumberAsStyle(value, format as FormatNumberStyle);
formattedValue = formatNumberAsStyle(value, format as FormatNumberStyle, ...extraFuncArgs);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/routes/docs/utils/format/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<div>{format(.5678, 'percentRound')}</div>
<div>{format(1_234_567, 'metric')}</div>
<div>{format(1_200_000, 'metric')}</div>
<div>{format(.5678, 'percent', 1)}</div>
</Preview>

<h2>Period formats</h2>
Expand All @@ -34,4 +35,5 @@
<div>{format(date, PeriodType.Day)}</div>
<div>{format(date, PeriodType.Month)}</div>
<div>{format(date, PeriodType.CalendarYear)}</div>
<div>{format(date, PeriodType.Day, 'short')}</div>
</Preview>

1 comment on commit ebdb2c8

@vercel
Copy link

@vercel vercel bot commented on ebdb2c8 Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.