Skip to content

Commit

Permalink
feat: replaced regular mergeAndSort by lodash
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Jan 7, 2025
1 parent bf7bb63 commit 595037f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import path from 'path';
import constants from '../utils/constants';
import { ERCOutputInterface } from '../schemas/ERCRegistrySchemas';
import { Helper } from '../utils/helper';
import _ from 'lodash';

export class RegistryGenerator {
/**
Expand Down Expand Up @@ -135,16 +136,14 @@ export class RegistryGenerator {
existingContracts[existingContracts.length - 1].contractId <
newContracts[0].contractId
) {
// Create a Map to deduplicate contracts by contractId
const contractMap = new Map(
[...existingContracts, ...newContracts].map((contract) => [
contract.contractId,
contract,
])
);
uniqueContracts = Array.from(contractMap.values());
uniqueContracts = _.chain([...existingContracts, ...newContracts]) // merge contracts
.uniqBy('contractId') // Remove duplicates based on contractId
.value(); // Extract the final array
} else {
uniqueContracts = Helper.mergeAndSort(existingContracts, newContracts);
uniqueContracts = _.chain([...existingContracts, ...newContracts]) // merge contracts
.uniqBy('contractId') // Remove duplicates based on contractId
.sortBy((contract) => Number(contract.contractId.split('.')[2])) // Sort by the numeric value of contractId
.value(); // Extract the final array
}

await this.writeContentsToFile(filePath, uniqueContracts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import axios, { AxiosInstance } from 'axios';
import constants from './constants';
import { ERCOutputInterface } from '../schemas/ERCRegistrySchemas';
import path from 'path';

export class Helper {
Expand Down Expand Up @@ -104,54 +103,4 @@ export class Helper {
}),
};
}

/**
* Merges two sorted arrays of contract objects and returns a single sorted array without duplicates.
* This function assumes both input arrays are sorted by `contractId` in ascending order.
*
* @param {ERCOutputInterface[]} existingContracts - The first array of contract objects, already sorted by `contractId`.
* @param {ERCOutputInterface[]} newContracts - The second array of contract objects, already sorted by `contractId`.
* @returns {ERCOutputInterface[]} A merged and sorted array of contract objects, with duplicate `contractId` entries removed.
*/
static mergeAndSort(
existingContracts: ERCOutputInterface[],
newContracts: ERCOutputInterface[]
): ERCOutputInterface[] {
let i = 0;
let j = 0;
const mergedContracts: ERCOutputInterface[] = [];

// Merge two sorted arrays
while (i < existingContracts.length && j < newContracts.length) {
const existing = existingContracts[i];
const incoming = newContracts[j];

const existingContractIdComparedValue = Number(
existing.contractId.split('.')[2]
);
const incomingContractIdComparedValue = Number(
incoming.contractId.split('.')[2]
);

if (existingContractIdComparedValue < incomingContractIdComparedValue) {
mergedContracts.push(existing);
i++;
} else if (
existingContractIdComparedValue > incomingContractIdComparedValue
) {
mergedContracts.push(incoming);
j++;
} else {
// remove duplicated objects
mergedContracts.push(existing);
i++;
j++;
}
}

// Add any remaining elements from each array
return mergedContracts
.concat(existingContracts.slice(i))
.concat(newContracts.slice(j));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,66 +46,5 @@ describe('Helper', () => {
const nextLink = Helper.buildUrl(mockNext, mockScanningLimit);
expect(nextLink).toEqual(expectedNextLink);
});

describe('mergeAndSort', () => {
const existingContracts = [
{
contractId: '0.0.14902',
address: '0x0000...',
name: '',
symbol: '',
totalSupply: 0,
decimals: 0,
},
{
contractId: '0.0.15701',
address: '0x0000...',
name: 'CUSD',
symbol: 'CUSD',
totalSupply: 0,
decimals: 18,
},
];

const newContracts = [
{
contractId: '0.0.14903',
address: '0x0000...',
name: 'CUSD',
symbol: 'CUSD',
totalSupply: 0,
decimals: 18,
},
{
contractId: '0.0.15701',
address: '0x0000...',
name: 'CUSD',
symbol: 'CUSD',
totalSupply: 0,
decimals: 18,
},
{
contractId: '0.0.15707',
address: '0x0000...',
name: 'CUSD',
symbol: 'CUSD',
totalSupply: 0,
decimals: 18,
},
];

it('merges, sorts, and removes duplicate objects from the two contract arrays', () => {
const result = Helper.mergeAndSort(existingContracts, newContracts);

const expectedResult = [
existingContracts[0], // '0.0.14902'
newContracts[0], // '0.0.14903'
existingContracts[1], // '0.0.15701' (duplicate removed)
newContracts[2], // '0.0.15707'
];

expect(result).toEqual(expectedResult);
});
});
});
});

0 comments on commit 595037f

Please sign in to comment.