-
Notifications
You must be signed in to change notification settings - Fork 16
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
Shortened amount props for ValueViewComponent #1915
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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,5 @@ | ||
--- | ||
'@penumbra-zone/types': minor | ||
--- | ||
|
||
Adding shortify + round utilities |
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,5 @@ | ||
--- | ||
'@penumbra-zone/ui': minor | ||
--- | ||
|
||
Updating ValueViewComponent to support shortening symbols |
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
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
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,124 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { round, RoundOptions } from './round.js'; | ||
|
||
describe('round function', () => { | ||
const testCases: { | ||
description: string; | ||
options: RoundOptions; | ||
expected: string; | ||
}[] = [ | ||
// Default rounding mode ('round') | ||
{ | ||
description: 'should round up using default rounding (round)', | ||
options: { value: 1.2345, decimals: 3 }, | ||
expected: '1.235', | ||
}, | ||
{ | ||
description: 'should round down using default rounding (round)', | ||
options: { value: 1.2341, decimals: 3 }, | ||
expected: '1.234', | ||
}, | ||
{ | ||
description: 'should round a negative number using default rounding (round)', | ||
options: { value: -1.2345, decimals: 2 }, | ||
expected: '-1.23', | ||
}, | ||
{ | ||
description: 'should round zero', | ||
options: { value: 0, decimals: 2 }, | ||
expected: '0.00', | ||
}, | ||
{ | ||
description: 'should round an integer with decimals', | ||
options: { value: 5, decimals: 2 }, | ||
expected: '5.00', | ||
}, | ||
// Rounding mode: 'ceil' | ||
{ | ||
description: 'should ceil to 2 decimals', | ||
options: { value: 1.2345, decimals: 2, roundingMode: 'ceil' }, | ||
expected: '1.24', | ||
}, | ||
{ | ||
description: 'should ceil a negative number', | ||
options: { value: -1.2345, decimals: 2, roundingMode: 'ceil' }, | ||
expected: '-1.23', | ||
}, | ||
{ | ||
description: 'should ceil with zero decimals', | ||
options: { value: 1.5, decimals: 0, roundingMode: 'ceil' }, | ||
expected: '2', | ||
}, | ||
// Rounding mode: 'floor' | ||
{ | ||
description: 'should floor to 2 decimals', | ||
options: { value: 1.2399, decimals: 2, roundingMode: 'floor' }, | ||
expected: '1.23', | ||
}, | ||
{ | ||
description: 'should floor a negative number', | ||
options: { value: -1.2345, decimals: 2, roundingMode: 'floor' }, | ||
expected: '-1.24', | ||
}, | ||
{ | ||
description: 'should floor with zero decimals', | ||
options: { value: 1.9, decimals: 0, roundingMode: 'floor' }, | ||
expected: '1', | ||
}, | ||
// Edge Cases | ||
{ | ||
description: 'should handle very large numbers', | ||
options: { value: 1.23456789e10, decimals: 2, roundingMode: 'round' }, | ||
expected: '12345678900.00', | ||
}, | ||
{ | ||
description: 'should handle very small numbers', | ||
options: { value: 0.000123456, decimals: 8, roundingMode: 'round' }, | ||
expected: '0.00012346', | ||
}, | ||
{ | ||
description: 'should handle Infinity', | ||
options: { value: Infinity, decimals: 2, roundingMode: 'round' }, | ||
expected: 'Infinity', | ||
}, | ||
{ | ||
description: 'should handle -Infinity', | ||
options: { value: -Infinity, decimals: 2, roundingMode: 'floor' }, | ||
expected: '-Infinity', | ||
}, | ||
{ | ||
description: 'should handle NaN', | ||
options: { value: NaN, decimals: 2, roundingMode: 'ceil' }, | ||
expected: 'NaN', | ||
}, | ||
{ | ||
description: 'should handle decimals greater than available decimal places', | ||
options: { value: 1.2, decimals: 5, roundingMode: 'floor' }, | ||
expected: '1.20000', | ||
}, | ||
// Rounding to integer | ||
{ | ||
description: 'should round to integer using round mode', | ||
options: { value: 2.5, decimals: 0, roundingMode: 'round' }, | ||
expected: '3', | ||
}, | ||
{ | ||
description: 'should ceil to integer', | ||
options: { value: 2.1, decimals: 0, roundingMode: 'ceil' }, | ||
expected: '3', | ||
}, | ||
{ | ||
description: 'should floor to integer', | ||
options: { value: 2.9, decimals: 0, roundingMode: 'floor' }, | ||
expected: '2', | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ description, options, expected }) => { | ||
// eslint-disable-next-line vitest/valid-title | ||
it(description, () => { | ||
const result = round(options); | ||
expect(result).toBe(expected); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { ceil as lodashCeil, floor as lodashFloor, round as lodashRound } from 'lodash'; | ||
|
||
export type RoundingMode = 'round' | 'ceil' | 'floor'; | ||
|
||
export interface RoundOptions { | ||
value: number; | ||
decimals: number; | ||
roundingMode?: RoundingMode; | ||
} | ||
|
||
const roundingStrategies = { | ||
ceil: lodashCeil, | ||
floor: lodashFloor, | ||
round: lodashRound, | ||
} as const; | ||
|
||
/** | ||
* Rounds a number based on the specified options. | ||
* | ||
* @param options - An object containing the properties: | ||
* - value: The number to round. | ||
* - decimals: The number of decimal places to round to. | ||
* - roundingMode: The mode of rounding ('round', 'ceil', 'floor'). Defaults to 'round'. | ||
* | ||
* @returns A string representation of the rounded number. | ||
* | ||
* @example | ||
* | ||
* ```typescript | ||
* round({ value: 1.2345, decimals: 2, roundingMode: 'ceil' }); // "1.24" | ||
* round({ value: 1.2345, decimals: 2, roundingMode: 'floor' }); // "1.23" | ||
* round({ value: 1.2345, decimals: 2 }); // "1.23" (default rounding) | ||
* ``` | ||
*/ | ||
export function round({ value, decimals, roundingMode = 'round' }: RoundOptions): string { | ||
const roundingFn = roundingStrategies[roundingMode]; | ||
const roundedNumber = roundingFn(value, decimals); | ||
return roundedNumber.toFixed(decimals); | ||
} | ||
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.
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.
Both
round()
andshortify()
come from the dex explorer. However, expanded these quite a bit w/ testing as well.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.
If this works, after publishing, will delete the local one in the dex explorer