Skip to content

Commit

Permalink
fix(core): update shortenAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson86tw committed May 10, 2024
1 parent 39922d9 commit 2df52dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export function toHex(n: number): string {
return '0x' + hexValue
}

export function shortenAddress(address: string): string {
return address.slice(0, 6) + '...' + address.slice(-4)
export function shortenAddress(address: string, startLength = 6, endLength = 4): string {
if (typeof address !== 'string' || !address) return ''
return address.slice(0, startLength) + '...' + address.slice(-endLength)
}

export function normalizeChainId(chainId: string | number | bigint) {
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/utils/tests/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { expect, test } from 'vitest'
import { toHex } from '../format'
import { describe, expect, test } from 'vitest'
import { toHex, shortenAddress } from '../format'

test('toHex', () => {
expect(toHex(1)).toBe('0x1')
})

describe('shortenAddress', () => {
test('shortenAddress', () => {
expect(shortenAddress('0x9D75F4EbcB8e7669E59dcc27CBadC698E0F77187')).toBe('0x9D75...7187')
})

test('shortenAddress with custom start and end length', () => {
expect(shortenAddress('0x9D75F4EbcB8e7669E59dcc27CBadC698E0F77187', 8, 5)).toBe('0x9D75F4...77187')
})

test('shortenAddress with invalid address', () => {
expect(shortenAddress('')).toBe('')
expect(shortenAddress(null as any)).toBe('')
expect(shortenAddress(undefined as any)).toBe('')
})
})

0 comments on commit 2df52dc

Please sign in to comment.