-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): add nve-format-number component #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jguzanvidia
wants to merge
1
commit into
main
Choose a base branch
from
topic/format-number
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+791
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { define } from '@nvidia-elements/core/internal'; | ||
| import { FormatNumber } from '@nvidia-elements/core/format-number'; | ||
|
|
||
| define(FormatNumber); | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'nve-format-number': FormatNumber; | ||
| } | ||
| } |
This file contains hidden or 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,14 @@ | ||
| /* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. */ | ||
| /* SPDX-License-Identifier: Apache-2.0 */ | ||
|
|
||
| :host { | ||
| display: inline; | ||
| } | ||
|
|
||
| [internal-host] { | ||
| color: var(--nve-sys-text-color, inherit); | ||
| } | ||
|
|
||
| slot { | ||
| display: none; | ||
| } |
120 changes: 120 additions & 0 deletions
120
projects/core/src/format-number/format-number.examples.ts
This file contains hidden or 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,120 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { html } from 'lit'; | ||
| import '@nvidia-elements/core/format-number/define.js'; | ||
|
|
||
| export default { | ||
| title: 'Elements/FormatNumber', | ||
| component: 'nve-format-number' | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Basic decimal formatting with localized grouping separators. Use for inline counts and metrics. | ||
| */ | ||
| export const Default = { | ||
| render: () => html` | ||
| <nve-format-number>1234567.89</nve-format-number> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Currency formatting for monetary values with locale-aware symbols and separators. Use for prices, budgets, and financial totals. | ||
| */ | ||
| export const Currency = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number format-style="currency" currency="USD">1234.56</nve-format-number> | ||
| <nve-format-number format-style="currency" currency="EUR" currency-display="code">1234.56</nve-format-number> | ||
| <nve-format-number format-style="currency" currency="JPY" currency-display="name">1234</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Percent formatting for ratios and completion values. Source values should already represent a fraction (such as 0.85 for 85 percent). | ||
| */ | ||
| export const Percent = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number format-style="percent">0.85</nve-format-number> | ||
| <nve-format-number format-style="percent">0.126</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Unit formatting for measurements and quantities. Use for distances, storage sizes, or other numeric labels that need a localized unit suffix. | ||
| */ | ||
| export const Unit = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number format-style="unit" unit="kilometer">1234.56</nve-format-number> | ||
| <nve-format-number format-style="unit" unit="byte" unit-display="long">2048</nve-format-number> | ||
| <nve-format-number format-style="unit" unit="celsius" unit-display="narrow">22</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Notation presets for scientific, engineering, and compact display. Use compact notation in dashboards or cards where space matters. | ||
| */ | ||
| export const Notation = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number notation="compact" compact-display="short">1234567</nve-format-number> | ||
| <nve-format-number notation="compact" compact-display="long">1234567</nve-format-number> | ||
| <nve-format-number notation="scientific">1234567</nve-format-number> | ||
| <nve-format-number notation="engineering">1234567</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Sign display options for controlling positive and negative indicators. Use 'always' for delta values or 'exceptZero' for change indicators. | ||
| */ | ||
| export const SignDisplay = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number sign-display="always">42</nve-format-number> | ||
| <nve-format-number sign-display="always">-42</nve-format-number> | ||
| <nve-format-number sign-display="exceptZero">0</nve-format-number> | ||
| <nve-format-number sign-display="never">-42</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Fraction digit control for tuning decimal precision. Use to enforce fixed decimal places in financial or scientific contexts. | ||
| */ | ||
| export const FractionDigits = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number minimum-fraction-digits="4">1.5</nve-format-number> | ||
| <nve-format-number maximum-fraction-digits="0">1.567</nve-format-number> | ||
| <nve-format-number minimum-fraction-digits="2" maximum-fraction-digits="2">3</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Explicit locale settings for internationalized number output. Use when the target audience locale differs from the document language or browser default. | ||
| */ | ||
| export const Locale = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-number locale="de-DE" format-style="currency" currency="EUR">1234.56</nve-format-number> | ||
| <nve-format-number locale="ja-JP" format-style="currency" currency="JPY">1234</nve-format-number> | ||
| <nve-format-number locale="fr-FR">1234567.89</nve-format-number> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Number attribute input for values supplied by JavaScript or bound data. By default, the component formats text content, which also serves as the SSR fallback, and `number` wins when both are present. | ||
| */ | ||
| export const NumberAttribute = { | ||
| render: () => html` | ||
| <nve-format-number number="1234.56" format-style="currency" currency="USD"></nve-format-number> | ||
| ` | ||
| }; |
This file contains hidden or 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,27 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { html } from 'lit'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createFixture, elementIsStable, removeFixture } from '@internals/testing'; | ||
| import { runAxe } from '@internals/testing/axe'; | ||
| import { FormatNumber } from '@nvidia-elements/core/format-number'; | ||
| import '@nvidia-elements/core/format-number/define.js'; | ||
|
|
||
| describe(FormatNumber.metadata.tag, () => { | ||
| it('should pass axe check', async () => { | ||
| const fixture = await createFixture(html` | ||
| <nve-format-number locale="en-US">1234567</nve-format-number> | ||
| <nve-format-number locale="en-US" format-style="currency" currency="USD">1234.56</nve-format-number> | ||
| <nve-format-number locale="de-DE">1234.56</nve-format-number> | ||
| `); | ||
|
|
||
| try { | ||
| await elementIsStable(fixture.querySelector(FormatNumber.metadata.tag)); | ||
| const results = await runAxe([FormatNumber.metadata.tag]); | ||
| expect(results.violations.length).toBe(0); | ||
| } finally { | ||
| removeFixture(fixture); | ||
| } | ||
| }); | ||
| }); | ||
21 changes: 21 additions & 0 deletions
21
projects/core/src/format-number/format-number.test.lighthouse.ts
This file contains hidden or 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,21 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { expect, test, describe } from 'vitest'; | ||
| import { lighthouseRunner } from '@internals/vite'; | ||
|
|
||
| describe('format-number lighthouse report', () => { | ||
| test('format-number should meet lighthouse benchmarks', async () => { | ||
| const report = await lighthouseRunner.getReport('nve-format-number', /* html */` | ||
| <nve-format-number format-style="currency" currency="USD">1234.56</nve-format-number> | ||
| <script type="module"> | ||
| import '@nvidia-elements/core/format-number/define.js'; | ||
| </script> | ||
| `); | ||
|
|
||
| expect(report.scores.performance).toBe(100); | ||
| expect(report.scores.accessibility).toBe(100); | ||
| expect(report.scores.bestPractices).toBe(100); | ||
| expect(report.payload.javascript.kb).toBeLessThan(12); | ||
| }); | ||
| }); |
This file contains hidden or 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,18 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { html } from 'lit'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { ssrRunner } from '@internals/vite'; | ||
| import { FormatNumber } from '@nvidia-elements/core/format-number'; | ||
| import '@nvidia-elements/core/format-number/define.js'; | ||
|
|
||
| describe(FormatNumber.metadata.tag, () => { | ||
| it('should pass baseline ssr check', async () => { | ||
| const result = await ssrRunner.render( | ||
| html`<nve-format-number format-style="currency" currency="USD">1234.56</nve-format-number>` | ||
| ); | ||
| expect(result.includes('shadowroot="open"')).toBe(true); | ||
| expect(result.includes('nve-format-number')).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.