This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Move formatInt to a separate util file
- Loading branch information
1 parent
8449f43
commit 0690da9
Showing
5 changed files
with
64 additions
and
35 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
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,30 @@ | ||
/* | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
export const formatInt = (num: number | bigint): string => { | ||
let buf: Buffer; | ||
if (typeof num === 'bigint') { | ||
if (num < BigInt(0)) { | ||
throw new Error('Negative number cannot be formatted'); | ||
} | ||
buf = Buffer.alloc(8); | ||
buf.writeBigUInt64BE(num); | ||
} else { | ||
if (num < 0) { | ||
throw new Error('Negative number cannot be formatted'); | ||
} | ||
buf = Buffer.alloc(4); | ||
buf.writeUInt32BE(num, 0); | ||
} | ||
return buf.toString('binary'); | ||
}; |
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,30 @@ | ||
/* | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
* | ||
*/ | ||
import { formatInt } from '../../../src/utils/number'; | ||
|
||
describe('Test formatInt method', () => { | ||
it('should return formatted result when called with valid BigInt', async () => { | ||
const formattedResult = formatInt(BigInt(100)); | ||
await expect(typeof formattedResult).toBe('string'); | ||
}); | ||
|
||
it('should throw error when called with negative number', async () => { | ||
await expect(() => formatInt(-1)).toThrow(); | ||
}); | ||
|
||
it('should throw error when called with negative BigInteger', async () => { | ||
await expect(() => formatInt(BigInt(-1))).toThrow(); | ||
}); | ||
}); |