-
Notifications
You must be signed in to change notification settings - Fork 9
feat(core): format-bytes #205
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
coryrylan
wants to merge
1
commit into
main
Choose a base branch
from
topic-format-bytes
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.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 { FormatBytes } from '@nvidia-elements/core/format-bytes'; | ||
|
|
||
| define(FormatBytes); | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'nve-format-bytes': FormatBytes; | ||
| } | ||
| } |
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; | ||
| } |
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,94 @@ | ||
| // 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-bytes/define.js'; | ||
|
|
||
| export default { | ||
| title: 'Elements/FormatBytes', | ||
| component: 'nve-format-bytes' | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Automatic decimal conversion for concise file sizes and storage metrics. The component selects the appropriate unit from the byte count. | ||
| */ | ||
| export const Default = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes>1024</nve-format-bytes> | ||
| <nve-format-bytes>1048576</nve-format-bytes> | ||
| <nve-format-bytes>1073741824</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Forced unit magnitudes for comparing byte counts on a consistent scale. Use when values need the same unit across a table or chart. | ||
| */ | ||
| export const Unit = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes unit="kb">1048576</nve-format-bytes> | ||
| <nve-format-bytes unit="mb">1048576</nve-format-bytes> | ||
| <nve-format-bytes unit="gb">1048576</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Short and long unit labels for compact metrics or explanatory text. Long labels improve clarity when space allows. | ||
| */ | ||
| export const UnitDisplay = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes unit-display="short">1048576</nve-format-bytes> | ||
| <nve-format-bytes unit-display="long">1048576</nve-format-bytes> | ||
| <nve-format-bytes display="binary" unit-display="long">1048576</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Fraction digit controls for matching the precision of storage measurements. Use fixed digits when values must align visually. | ||
| */ | ||
| export const Precision = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes maximum-fraction-digits="0">1234567</nve-format-bytes> | ||
| <nve-format-bytes maximum-fraction-digits="2">1234567</nve-format-bytes> | ||
| <nve-format-bytes minimum-fraction-digits="3" maximum-fraction-digits="3">1234567</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Explicit locale formatting for audiences whose numeric separators differ from the document language. Unit labels remain lowercase English. | ||
| */ | ||
| export const Locale = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes locale="en-US">1048576</nve-format-bytes> | ||
| <nve-format-bytes locale="de-DE">1048576</nve-format-bytes> | ||
| <nve-format-bytes locale="fr-FR">1048576</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Value attribute input for JavaScript or bound data. It takes precedence over text content while the text remains an SSR fallback. | ||
| */ | ||
| export const Value = { | ||
| render: () => html`<nve-format-bytes value="1048576">1024</nve-format-bytes>` | ||
| }; | ||
|
|
||
| /** | ||
| * @summary Decimal and binary conversion for matching SI or IEC storage conventions. Use the convention expected by the surrounding product. | ||
| */ | ||
| export const Display = { | ||
| render: () => html` | ||
| <div nve-layout="column gap:sm"> | ||
| <nve-format-bytes display="decimal">1024</nve-format-bytes> | ||
| <nve-format-bytes display="binary">1024</nve-format-bytes> | ||
| </div> | ||
| ` | ||
| }; |
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 { FormatBytes } from '@nvidia-elements/core/format-bytes'; | ||
| import '@nvidia-elements/core/format-bytes/define.js'; | ||
|
|
||
| describe(FormatBytes.metadata.tag, () => { | ||
| it('should pass axe check', async () => { | ||
| const fixture = await createFixture(html` | ||
| <nve-format-bytes locale="en-US">1048576</nve-format-bytes> | ||
| <nve-format-bytes locale="en-US" display="binary" unit-display="long">1048576</nve-format-bytes> | ||
| <nve-format-bytes locale="de-DE" unit="kb">1048576</nve-format-bytes> | ||
| `); | ||
|
|
||
| try { | ||
| await elementIsStable(fixture.querySelector(FormatBytes.metadata.tag)); | ||
| const results = await runAxe([FormatBytes.metadata.tag]); | ||
| expect(results.violations.length).toBe(0); | ||
| } finally { | ||
| removeFixture(fixture); | ||
| } | ||
| }); | ||
| }); | ||
21 changes: 21 additions & 0 deletions
21
projects/core/src/format-bytes/format-bytes.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 { describe, expect, test } from 'vitest'; | ||
| import { lighthouseRunner } from '@internals/vite'; | ||
|
|
||
| describe('format-bytes lighthouse report', () => { | ||
| test('format-bytes should meet lighthouse benchmarks', async () => { | ||
| const report = await lighthouseRunner.getReport('nve-format-bytes', /* html */ ` | ||
| <nve-format-bytes locale="en-US">1048576</nve-format-bytes> | ||
| <script type="module"> | ||
| import '@nvidia-elements/core/format-bytes/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(11); | ||
| }); | ||
| }); |
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 { FormatBytes } from '@nvidia-elements/core/format-bytes'; | ||
| import '@nvidia-elements/core/format-bytes/define.js'; | ||
|
|
||
| describe(FormatBytes.metadata.tag, () => { | ||
| it('should render formatted semantic output during ssr', async () => { | ||
| const result = await ssrRunner.render(html`<nve-format-bytes locale="en-US" value="1048576"></nve-format-bytes>`); | ||
| expect(result.includes('shadowroot="open"')).toBe(true); | ||
| expect(result.includes('<data internal-host value="1048576">')).toBe(true); | ||
| expect(result.includes('1.05 mb')).toBe(true); | ||
| expect(result.includes('nve-format-bytes')).toBe(true); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Await stability for all three fixture elements, not just the first.
The fixture creates three
nve-format-byteselements. Line 20 only awaitselementIsStableonfixture.querySelector(FormatBytes.metadata.tag), which resolves to the first matching element only. The axe scan on line 21 covers all three elements, but the second and third elements' render cycles are not confirmed stable. This can produce a flaky or false-negative accessibility result if axe scans an element mid-update.🐛 Proposed fix to await stability for every element
try { - await elementIsStable(fixture.querySelector(FormatBytes.metadata.tag)); + await Promise.all( + [...fixture.querySelectorAll(FormatBytes.metadata.tag)].map((el) => elementIsStable(el)) + ); const results = await runAxe([FormatBytes.metadata.tag]);📝 Committable suggestion
🤖 Prompt for AI Agents