-
Notifications
You must be signed in to change notification settings - Fork 9
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
Precisely format large numbers using Intl through i18n #773
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e922f9
Add getPreciseNumberFormat to format large stringified numbers with Intl
lukaw3d 0b3aecd
Format valueInToken with getPreciseNumberFormat
lukaw3d 919eec8
Format token balance with getPreciseNumberFormat
lukaw3d 6dea781
Add cross-browser playwright tests for getPreciseNumberFormat
lukaw3d 5b310bd
Fallback to precise unformatted numbers in getPreciseNumberFormat
lukaw3d c2a618b
Always precisely format small numbers in getPreciseNumberFormat
lukaw3d 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 @@ | ||
Precisely format large numbers using Intl through i18n |
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 @@ | ||
# NOTE: This name appears in GitHub's Checks API and in workflow's status badge. | ||
name: ci-playwright | ||
|
||
# Trigger the workflow when: | ||
on: | ||
# A push occurs to one of the matched branches. | ||
push: | ||
branches: [master] | ||
# Or when a pull request event occurs for a pull request against one of the | ||
# matched branches. | ||
pull_request: | ||
branches: [master] | ||
|
||
jobs: | ||
playwright: | ||
# NOTE: This name appears in GitHub's Checks API. | ||
name: playwright | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- name: Set up Node.js 20 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '20.x' | ||
cache: yarn | ||
- name: Install dependencies | ||
run: yarn install --frozen-lockfile | ||
- name: Run dev server | ||
run: yarn start & | ||
- name: Install playwright | ||
run: yarn install --frozen-lockfile | ||
working-directory: playwright | ||
- name: Install playwright dependencies | ||
run: yarn playwright install --with-deps | ||
working-directory: playwright | ||
- name: Run playwright tests | ||
run: yarn test | ||
working-directory: playwright |
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,8 @@ | ||
{ | ||
"scripts": { | ||
"test": "playwright test --browser all" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "^1.36.2" | ||
} | ||
} |
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,80 @@ | ||
import { Page, expect, test } from '@playwright/test' | ||
import { RuntimeAccount } from '../../src/oasis-nexus/api' | ||
|
||
async function setup(page: Page, balance: string, decimals: number) { | ||
await page.route( | ||
'https://api.coingecko.com/api/v3/simple/price?ids=oasis-network&vs_currencies=usd', | ||
route => { | ||
// Don't respond | ||
}, | ||
) | ||
await page.route('**/v1/', route => { | ||
// Don't respond | ||
}) | ||
await page.route('**/v1/sapphire/status', route => { | ||
// Don't respond | ||
}) | ||
|
||
await page.route('**/v1/sapphire/accounts/oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek', route => { | ||
route.fulfill({ | ||
body: JSON.stringify({ | ||
address: 'oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek', | ||
address_preimage: { | ||
address_data: 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=', | ||
context: 'oasis-runtime-sdk/address: secp256k1eth', | ||
context_version: 0, | ||
}, | ||
balances: [], | ||
evm_balances: [ | ||
{ | ||
balance: balance, | ||
token_decimals: decimals, | ||
token_contract_addr: 'oasis1qzpyvqpw4e55wj4wjasp70w30x3kxsp4evy5dtf8', | ||
token_contract_addr_eth: '0x6b59C68405B0216C2C8ba1EC1f8DCcBd47892c58', | ||
token_name: 'TokenForTests', | ||
token_symbol: 'USDT', | ||
token_type: 'ERC20', | ||
}, | ||
], | ||
stats: { | ||
num_txns: 0, | ||
total_received: '0', | ||
total_sent: '0', | ||
}, | ||
} satisfies Partial<RuntimeAccount>), | ||
}) | ||
}) | ||
|
||
await page.goto( | ||
'http://localhost:1234/mainnet/sapphire/address/0x0000000000000000000000000000000000000000/tokens/erc-20#tokens', | ||
) | ||
} | ||
|
||
test.describe('getPreciseNumberFormat', () => { | ||
test('small number should be precise and formatted', async ({ page }) => { | ||
await setup(page, '111222333444555', 9) | ||
// Expect precisely formatted small number even when browser doesn't support precise formatting for large numbers | ||
await expect(page.getByText('111,222.333444555', { exact: true })).toBeVisible() | ||
}) | ||
|
||
test('large number should be precise and formatted or fallback to precise unformatted number in browsers without support', async ({ | ||
page, | ||
}) => { | ||
await setup(page, '111222333444555666777888999111222333444555666', 18) | ||
await expect( | ||
page | ||
.getByText('111,222,333,444,555,666,777,888,999.111222333444555666', { exact: true }) | ||
// Expect precise fallback when browser doesn't support precise formatting | ||
.or(page.getByText('111222333444555666777888999.111222333444555666', { exact: true })), | ||
).toBeVisible() | ||
}) | ||
|
||
test('exceeding maximumFractionDigits:20 should fallback to precise unformatted number', async ({ | ||
page, | ||
}) => { | ||
await setup(page, '111222333444555666777888999111222333444555666', 36) | ||
await expect( | ||
page.getByText('111222333.444555666777888999111222333444555666', { exact: true }), | ||
).toBeVisible() | ||
}) | ||
}) | ||
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,28 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
"@playwright/test@^1.36.2": | ||
version "1.36.2" | ||
resolved "https://registry.yarnpkg.com/@playwright/test/-/test-1.36.2.tgz#9edd68a02b0929c5d78d9479a654ceb981dfb592" | ||
integrity sha512-2rVZeyPRjxfPH6J0oGJqE8YxiM1IBRyM8hyrXYK7eSiAqmbNhxwcLa7dZ7fy9Kj26V7FYia5fh9XJRq4Dqme+g== | ||
dependencies: | ||
"@types/node" "*" | ||
playwright-core "1.36.2" | ||
optionalDependencies: | ||
fsevents "2.3.2" | ||
|
||
"@types/node@*": | ||
version "20.4.5" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.4.5.tgz#9dc0a5cb1ccce4f7a731660935ab70b9c00a5d69" | ||
integrity sha512-rt40Nk13II9JwQBdeYqmbn2Q6IVTA5uPhvSO+JVqdXw/6/4glI6oR9ezty/A9Hg5u7JH4OmYmuQ+XvjKm0Datg== | ||
|
||
[email protected]: | ||
version "2.3.2" | ||
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" | ||
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA== | ||
|
||
[email protected]: | ||
version "1.36.2" | ||
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.36.2.tgz#32382f2d96764c24c65a86ea336cf79721c2e50e" | ||
integrity sha512-sQYZt31dwkqxOrP7xy2ggDfEzUxM1lodjhsQ3NMMv5uGTRDsLxU0e4xf4wwMkF2gplIxf17QMBCodSFgm6bFVQ== |
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
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
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.
How it behaves across browsers
@donouwens says "Yeah, I think this should work. 👍"