-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shortened amount props for ValueViewComponent (#1915)
* Update value view component w/ shortened amount * Changesets add * post review updates * De-dupe trailing zeros func
- Loading branch information
Showing
12 changed files
with
541 additions
and
31 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
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.