-
Notifications
You must be signed in to change notification settings - Fork 33
feat: format metadata floats with significant digits #2067
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
emigun
wants to merge
2
commits into
master
Choose a base branch
from
metadata-float-format
base: master
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,105 @@ | ||
| import { TestBed } from "@angular/core/testing"; | ||
| import { MetadataValueService } from "./metadata-value.service"; | ||
| import { AppConfigInterface, AppConfigService } from "app-config.service"; | ||
|
|
||
| describe("MetadataValueService", () => { | ||
| let service: MetadataValueService; | ||
| let mockConfigService: jasmine.SpyObj<AppConfigService>; | ||
|
|
||
| beforeEach(() => { | ||
| mockConfigService = jasmine.createSpyObj("AppConfigService", ["getConfig"]); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| providers: [ | ||
| MetadataValueService, | ||
| { provide: AppConfigService, useValue: mockConfigService }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| function setConfig( | ||
| options?: Partial<{ | ||
| enabled: boolean; | ||
| significantDigits: number; | ||
| minCutoff: number; | ||
| maxCutoff: number; | ||
| }>, | ||
| ) { | ||
| const mockConfig: Partial<AppConfigInterface> = { | ||
| metadataFloatFormatEnabled: options?.enabled ?? true, | ||
| metadataFloatFormat: { | ||
| significantDigits: options?.significantDigits ?? 3, | ||
| minCutoff: options?.minCutoff ?? 0.001, | ||
| maxCutoff: options?.maxCutoff ?? 1000, | ||
| }, | ||
| }; | ||
|
|
||
| mockConfigService.getConfig.and.returnValue( | ||
| mockConfig as AppConfigInterface, | ||
| ); | ||
| } | ||
|
|
||
| it("should not format float if not enabled", () => { | ||
| setConfig({ enabled: false }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(1.23456789)).toBe("1.23456789"); | ||
| }); | ||
|
|
||
| it("should return string as-is for non-number values", () => { | ||
| setConfig(); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat("abc")).toBe("abc"); | ||
| expect(service.valueFormat("")).toBe(""); | ||
| }); | ||
|
|
||
| it("should return string representation for non-finite numbers", () => { | ||
| setConfig(); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(NaN)).toBe("NaN"); | ||
| expect(service.valueFormat(Infinity)).toBe("Infinity"); | ||
| expect(service.valueFormat(-Infinity)).toBe("-Infinity"); | ||
| }); | ||
|
|
||
| it("should return string representation for integers", () => { | ||
| setConfig(); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(42)).toBe("42"); | ||
| expect(service.valueFormat(-10)).toBe("-10"); | ||
| }); | ||
|
|
||
| it("should omit decimals when significant digits fit within the integer part", () => { | ||
| setConfig({ significantDigits: 2 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(12.3456)).toBe("12"); | ||
| }); | ||
|
|
||
| it("should use exponential notation for very small numbers", () => { | ||
| setConfig({ significantDigits: 3, minCutoff: 0.001 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(0.0000123)).toBe("1.23e-5"); | ||
| }); | ||
|
|
||
| it("should use exponential notation for very large numbers", () => { | ||
| setConfig({ significantDigits: 4, maxCutoff: 1e6 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(50004313.487)).toBe("5.000e+7"); | ||
| }); | ||
|
|
||
| it("should handle values just above minCutoff correctly", () => { | ||
| setConfig({ minCutoff: 0.001, significantDigits: 4 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(0.0023456)).toBe("0.002346"); | ||
| }); | ||
|
|
||
| it("should handle values just below maxCutoff correctly", () => { | ||
| setConfig({ maxCutoff: 1e6, significantDigits: 3 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(999999.99)).toBe("1.00e+6"); | ||
| }); | ||
|
|
||
| it("should respect significantDigits when using exponential notation", () => { | ||
| setConfig({ significantDigits: 5, minCutoff: 1e-3 }); | ||
| service = TestBed.inject(MetadataValueService); | ||
| expect(service.valueFormat(0.0000123)).toBe("1.2300e-5"); | ||
| }); | ||
| }); | ||
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,47 @@ | ||
| import { Injectable } from "@angular/core"; | ||
| import { AppConfigService } from "app-config.service"; | ||
|
|
||
| @Injectable({ | ||
| providedIn: "root", | ||
| }) | ||
| export class MetadataValueService { | ||
| private enabled: boolean; | ||
| private significantDigits: number; | ||
| private minCutoff: number; | ||
| private maxCutoff: number; | ||
|
|
||
| constructor(private configService: AppConfigService) { | ||
| const config = this.configService.getConfig(); | ||
| this.enabled = config.metadataFloatFormatEnabled ?? false; | ||
| if (this.enabled) { | ||
| const metadataFloatFormat = config.metadataFloatFormat; | ||
| this.significantDigits = metadataFloatFormat.significantDigits; | ||
| this.minCutoff = metadataFloatFormat.minCutoff; | ||
| this.maxCutoff = metadataFloatFormat.maxCutoff; | ||
| } | ||
| } | ||
|
|
||
| valueFormat(value: string | number): string { | ||
| if (!this.enabled) { | ||
| return String(value); | ||
| } | ||
| if (typeof value !== "number" || !Number.isFinite(value)) { | ||
| // value is not a finite number | ||
| return String(value); | ||
| } | ||
|
|
||
| // Do not format integers | ||
| if (Number.isInteger(value)) { | ||
| return String(value); | ||
| } | ||
|
|
||
| // use scientific notation if float value is large or small | ||
| const absoluteValue = Math.abs(value); | ||
| if (absoluteValue < this.minCutoff || absoluteValue > this.maxCutoff) { | ||
| // use scientific notation with (significantDigits - 1) decimals | ||
| return value.toExponential(this.significantDigits - 1); | ||
| } | ||
|
|
||
| return value.toPrecision(this.significantDigits); | ||
| } | ||
| } |
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.