-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To format number and dates according to the locale. Default locale comes from the navigator language.
- Loading branch information
1 parent
c6323ba
commit d36cdd0
Showing
6 changed files
with
33 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/visualizations/src/components/Table/Cell/Format/DateFormat.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
<script lang="ts"> | ||
import store from '../../store'; | ||
import type { DateColumn } from '../../types'; | ||
import { warn } from './utils'; | ||
export let rawValue: unknown; | ||
export let options: DateColumn['options']; | ||
function getDisplayValue(v: unknown) { | ||
function getDisplayValue(v: unknown, locale: string) { | ||
if ( | ||
(typeof v === 'string' || typeof v === 'number') && | ||
!Number.isNaN(new Date(v).getTime()) | ||
) { | ||
return new Intl.DateTimeFormat(navigator.language, options).format(new Date(v)); | ||
return new Intl.DateTimeFormat(locale, options).format(new Date(v)); | ||
} | ||
warn(v, 'date'); | ||
return v; | ||
} | ||
$: value = getDisplayValue(rawValue); | ||
$: value = getDisplayValue(rawValue, $store.locale); | ||
</script> | ||
|
||
{value} |
8 changes: 5 additions & 3 deletions
8
packages/visualizations/src/components/Table/Cell/Format/NumberFormat.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
<script lang="ts"> | ||
import store from '../../store'; | ||
import type { NumberColumn } from '../../types'; | ||
import { warn } from './utils'; | ||
export let rawValue: unknown; | ||
export let options: NumberColumn['options']; | ||
function getDisplayValue(v: unknown) { | ||
function getDisplayValue(v: unknown, locale: string) { | ||
if (!Number.isFinite(v)) { | ||
warn(v, 'number'); | ||
return v; | ||
} | ||
return new Intl.NumberFormat(navigator.language, options).format(v as number); | ||
return new Intl.NumberFormat(locale, options).format(v as number); | ||
} | ||
$: value = getDisplayValue(rawValue); | ||
$: value = getDisplayValue(rawValue, $store.locale); | ||
</script> | ||
|
||
{value} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { writable } from 'svelte/store'; | ||
|
||
type TableStore = { | ||
locale: string; | ||
}; | ||
|
||
const defaultLocale = navigator.language; | ||
|
||
const store = writable<TableStore>({ locale: defaultLocale }); | ||
|
||
const updateLocale = (locale: string = defaultLocale) => { | ||
store.update((previousValue) => ({ ...previousValue, locale })); | ||
}; | ||
|
||
export { updateLocale }; | ||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters