Skip to content

Commit

Permalink
Util internal tests and fix (#3112)
Browse files Browse the repository at this point in the history
* Add tests for internal util functions

* Reimplement fromUtf8 function to not double-add the hex prefix

* Do not try to pad an already padded string

* Use bytesToUnprefixedHex for fromUtf8

* Add more tests

* Remove unused import

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
scorbajio and holgerd77 authored Oct 25, 2023
1 parent 61af5c3 commit 25696ac
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/util/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
*/

import { bytesToHex, utf8ToBytes } from './bytes.js'
import { bytesToUnprefixedHex, utf8ToBytes } from './bytes.js'

/**
* Returns a `Boolean` on whether or not the a `String` starts with '0x'
Expand Down Expand Up @@ -129,7 +129,8 @@ export function toAscii(hex: string): string {
}

/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
* Should be called to get hex representation (prefixed by 0x) of utf8 string.
* Strips leading and trailing 0's.
*
* @param string
* @param optional padding
Expand All @@ -138,7 +139,7 @@ export function toAscii(hex: string): string {
export function fromUtf8(stringValue: string) {
const str = utf8ToBytes(stringValue)

return `0x${padToEven(bytesToHex(str)).replace(/^0+|0+$/g, '')}`
return `0x${padToEven(bytesToUnprefixedHex(str)).replace(/^0+|0+$/g, '')}`
}

/**
Expand Down
68 changes: 68 additions & 0 deletions packages/util/test/internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { assert, describe, it } from 'vitest'
import {
arrayContainsArray,
bytesToUtf8,
fromAscii,
fromUtf8,
getBinarySize,
getKeys,
isHexPrefixed,
Expand Down Expand Up @@ -67,4 +69,70 @@ describe('internal', () => {
assert.equal(isHexString('0x0000000000000000000000000000000000000000'), true)
assert.equal(isHexString('123'), false)
})

describe('isHexPrefixed', () => {
it('should return true for hex-prefixed string', () => {
assert.isTrue(isHexPrefixed('0x123'))
})

it('should return false for non-hex-prefixed string', () => {
assert.isFalse(isHexPrefixed('123'))
})
})

describe('padToEven', () => {
it('should pad odd-length string to even', () => {
assert.equal(padToEven('123'), '0123')
})

it('should not pad even-length string', () => {
assert.equal(padToEven('1234'), '1234')
})
})

describe('getBinarySize', () => {
it('should return the correct binary size of a string', () => {
assert.equal(getBinarySize('Hello, World!'), 13)
})
})

describe('arrayContainsArray', () => {
it('should return true when the first array contains all elements of the second', () => {
assert.isTrue(arrayContainsArray([1, 2, 3, 4, 5], [3, 4]))
})

it('should return false when the first array does not contain any elements of the second', () => {
assert.isFalse(arrayContainsArray([1, 2, 3, 4, 5], [6, 7]))
})

it('should return false when the first array contains some but not all elements of the second', () => {
assert.isFalse(arrayContainsArray([1, 2, 3, 4, 5], [5, 6]))
})
})

describe('fromUtf8', () => {
it('should convert a UTF-8 string to a hex string', () => {
assert.equal(fromUtf8('Hello, World!'), '0x48656c6c6f2c20576f726c6421')
})

it('should convert a UTF-8 string with 2-byte characters to a hex string', () => {
assert.equal(fromUtf8('ϋύϒϗϘϢϰЂ'), '0xcf8bcf8dcf92cf97cf98cfa2cfb0d082')
})
})

describe('fromAscii', () => {
it('should convert an ASCII string to a hex string', () => {
assert.equal(fromAscii('Hello, World!'), '0x48656c6c6f2c20576f726c6421')
})
})

describe('getKeys', () => {
it('should extract keys from an array of objects', () => {
const input = [
{ a: '1', b: '2' },
{ a: '3', b: '4' },
]
assert.deepEqual(getKeys(input, 'a'), ['1', '3'])
})
})
})

0 comments on commit 25696ac

Please sign in to comment.