Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🔨 Move formatInt to a separate util file
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Oct 24, 2023
1 parent 8449f43 commit 0690da9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
19 changes: 1 addition & 18 deletions src/utils/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,7 @@ import { BlockHeader } from '@liskhq/lisk-chain';
import { DB_KEY_BLOCKS_HEIGHT, DB_KEY_BLOCKS_ID } from '../constants';
import { blockHeaderSchema } from '../schemas';
import { keyString, incrementOne } from './transaction';

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');
};
import { formatInt } from './number';

export const getDataFromDBStream = async (stream: NodeJS.ReadableStream, schema: Schema) => {
const data = await new Promise<Record<string, unknown>[]>((resolve, reject) => {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/number.ts
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');
};
2 changes: 1 addition & 1 deletion test/unit/createAsset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
GenesisAssetEntry,
VoteWeightsWrapper,
} from '../../src/types';
import { formatInt } from '../../src/utils/block';
import { formatInt } from '../../src/utils/number';

const { hash } = utils;
const { getKeys } = legacy;
Expand Down
18 changes: 2 additions & 16 deletions test/unit/utils/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,13 @@ import { codec } from '@liskhq/lisk-codec';
import { blockHeaderAssetSchema, blockHeaderSchema } from '@liskhq/lisk-chain';

import { generateBlocks } from './blocks';
import { formatInt, getBlockHeaderByHeight } from '../../../src/utils/block';
import { getBlockHeaderByHeight } from '../../../src/utils/block';
import { formatInt } from '../../../src/utils/number';
import { DB_KEY_BLOCKS_HEIGHT, DB_KEY_BLOCKS_ID } from '../../../src/constants';
import { keyString } from '../../../src/utils/transaction';

jest.mock('@liskhq/lisk-db');

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();
});
});

describe('Test getBlockHeaderByHeight method', () => {
let db: any;
let blockHeader: Buffer;
Expand Down
30 changes: 30 additions & 0 deletions test/unit/utils/number.spec.ts
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();
});
});

0 comments on commit 0690da9

Please sign in to comment.