Skip to content

Commit

Permalink
feat(utils): remove utils#toHexInLittleEndian which is deprecated
Browse files Browse the repository at this point in the history
Remove utils#toHexInLittleEndian which is deprecated

BREAKING CHANGE: Remove utils#toHexInLittleEndian which is deprecated
  • Loading branch information
Keith-CY committed Aug 11, 2020
1 parent a27d50b commit a028887
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 94 deletions.
10 changes: 0 additions & 10 deletions packages/ckb-sdk-utils/__tests__/convertors/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,5 @@
"hex": "0x4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e7365637465747572206164697069736963696e6720656c69742c2073656420646f20656975736d6f642074656d706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c697175612e20557420656e696d206164206d696e696d2076656e69616d2c2071756973206e6f737472756420657865726369746174696f6e20756c6c616d636f206c61626f726973206e69736920757420616c697175697020657820656120636f6d6d6f646f20636f6e7365717561742e2044756973206175746520697275726520646f6c6f7220696e20726570726568656e646572697420696e20766f6c7570746174652076656c697420657373652063696c6c756d20646f6c6f726520657520667567696174206e756c6c612070617269617475722e204578636570746575722073696e74206f6363616563617420637570696461746174206e6f6e2070726f6964656e742c2073756e7420696e2063756c706120717569206f666669636961206465736572756e74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e",
"expected": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
],
"toHexInLittleEndian": [
{
"value": "0x3e8",
"expected": "0xe8030000"
},
{
"value": 1000,
"expected": "0xe8030000"
}
]
}
18 changes: 0 additions & 18 deletions packages/ckb-sdk-utils/__tests__/convertors/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
bytesToHex,
utf8ToHex,
hexToUtf8,
toHexInLittleEndian,
} = require('../../lib/convertors')
const { HexStringWithout0xException } = require('../../lib/exceptions')

Expand All @@ -18,7 +17,6 @@ const {
bytesToHex: bytesToHexFixture,
utf8ToHex: utf8ToHexFixture,
hexToUtf8: hexToUtf8Fixture,
toHexInLittleEndian: toHexInLittleEndianFixture,
} = require('./fixtures.json')

describe('Test toUint16Le', () => {
Expand Down Expand Up @@ -98,19 +96,3 @@ describe('hex to utf8', () => {
expect(() => hexToBytes('abcd')).toThrow(new HexStringWithout0xException('abcd'))
})
})

describe('Test toHexInLittleEndian', () => {
const fixtureTable = toHexInLittleEndianFixture.map(({ value, expected }) => [
typeof value === 'number' ? BigInt(value) : value,
expected,
])
test.each(fixtureTable)('%s => %s', (value, expected) => {
expect(toHexInLittleEndian(value)).toBe(expected)
})
it('hex string without 0x should throw an error', () => {
expect(() => toHexInLittleEndian('123')).toThrow(new HexStringWithout0xException('123'))
})
it('throw an error when received a input unable to be converted into a number', () => {
expect(() => toHexInLittleEndian('invalid number')).toThrow(new HexStringWithout0xException('invalid number'))
})
})
44 changes: 0 additions & 44 deletions packages/ckb-sdk-utils/__tests__/utils/transformer.fixtures.json

This file was deleted.

23 changes: 1 addition & 22 deletions packages/ckb-sdk-utils/src/convertors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TextDecoder, TextEncoder, deprecate } from 'util'
import JSBI from 'jsbi'
import { TextDecoder, TextEncoder } from 'util'
import { assertToBeHexStringOrBigint } from '../validators'
import { HexStringWithout0xException } from '../exceptions'

Expand Down Expand Up @@ -74,25 +73,6 @@ export const utf8ToBytes = (str: string) => new TextEncoder().encode(str)

export const utf8ToHex = (str: string) => bytesToHex(utf8ToBytes(str))

const reverseString = (str: string) => str.split('').reverse().join('')

/**
* @deprecated since version 0.32,
* will be removed in version 0.35,
* use utils.{toUint16Le, toUint32Le, toUint64Le} instead
*/
export const toHexInLittleEndian = deprecate((int: string | bigint, paddingBytes: number = 4) => {
assertToBeHexStringOrBigint(int)
const hex = JSBI.BigInt(`${int}`).toString(16)
const reversedHex = reverseString(hex)
const frags = reversedHex.match(/\w{1,2}/g) || []
const hexInLittleEndian = frags
.map(frag => reverseString(frag.padEnd(2, '0')))
.join('')
.padEnd(paddingBytes * 2, '0')
return `0x${hexInLittleEndian}`
}, 'utils.toHexInLittleEndian is deprecated, use utils.{toUint16Le, toUint32Le, toUint64Le} instead')

export default {
toUint16Le,
toUint32Le,
Expand All @@ -101,5 +81,4 @@ export default {
bytesToHex,
hexToUtf8,
utf8ToBytes,
toHexInLittleEndian,
}

0 comments on commit a028887

Please sign in to comment.