Skip to content

Commit 61e9270

Browse files
committed
refactor(core): use for..of instead of forEach
1 parent 322fdb4 commit 61e9270

File tree

1 file changed

+55
-59
lines changed
  • governance/xc_admin/packages/xc_admin_common/src/programs/core

1 file changed

+55
-59
lines changed

governance/xc_admin/packages/xc_admin_common/src/programs/core/core_adapter.ts

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -265,50 +265,47 @@ export class PythCoreAdapter implements ProgramAdapter {
265265
*/
266266
private sortData(data: any) {
267267
const sortedData: any = {};
268-
Object.keys(data)
269-
.sort()
270-
.forEach((key) => {
271-
const sortedInnerData: any = {};
272-
Object.keys(data[key])
273-
.sort()
274-
.forEach((innerKey) => {
275-
if (innerKey === "metadata") {
276-
sortedInnerData[innerKey] = this.sortObjectByKeys(
277-
data[key][innerKey],
278-
);
279-
} else if (innerKey === "priceAccounts") {
280-
// Sort price accounts by address
281-
sortedInnerData[innerKey] = data[key][innerKey].sort(
282-
(priceAccount1: any, priceAccount2: any) =>
283-
priceAccount1.address.localeCompare(priceAccount2.address),
284-
);
285-
// Sort price accounts keys
286-
sortedInnerData[innerKey] = sortedInnerData[innerKey].map(
287-
(priceAccount: any) => {
288-
const sortedPriceAccount: any = {};
289-
Object.keys(priceAccount)
290-
.sort()
291-
.forEach((priceAccountKey) => {
292-
if (priceAccountKey === "publishers") {
293-
sortedPriceAccount[priceAccountKey] = priceAccount[
294-
priceAccountKey
295-
].sort((pub1: string, pub2: string) =>
296-
pub1.localeCompare(pub2),
297-
);
298-
} else {
299-
sortedPriceAccount[priceAccountKey] =
300-
priceAccount[priceAccountKey];
301-
}
302-
});
303-
return sortedPriceAccount;
304-
},
305-
);
306-
} else {
307-
sortedInnerData[innerKey] = data[key][innerKey];
308-
}
309-
});
310-
sortedData[key] = sortedInnerData;
311-
});
268+
const keys = Object.keys(data).sort();
269+
for (const key of keys) {
270+
const sortedInnerData: any = {};
271+
const innerKeys = Object.keys(data[key]).sort();
272+
for (const innerKey of innerKeys) {
273+
if (innerKey === "metadata") {
274+
sortedInnerData[innerKey] = this.sortObjectByKeys(
275+
data[key][innerKey],
276+
);
277+
} else if (innerKey === "priceAccounts") {
278+
// Sort price accounts by address
279+
sortedInnerData[innerKey] = data[key][innerKey].sort(
280+
(priceAccount1: any, priceAccount2: any) =>
281+
priceAccount1.address.localeCompare(priceAccount2.address),
282+
);
283+
// Sort price accounts keys
284+
sortedInnerData[innerKey] = sortedInnerData[innerKey].map(
285+
(priceAccount: any) => {
286+
const sortedPriceAccount: any = {};
287+
const priceAccountKeys = Object.keys(priceAccount).sort();
288+
for (const priceAccountKey of priceAccountKeys) {
289+
if (priceAccountKey === "publishers") {
290+
sortedPriceAccount[priceAccountKey] = priceAccount[
291+
priceAccountKey
292+
].sort((pub1: string, pub2: string) =>
293+
pub1.localeCompare(pub2),
294+
);
295+
} else {
296+
sortedPriceAccount[priceAccountKey] =
297+
priceAccount[priceAccountKey];
298+
}
299+
}
300+
return sortedPriceAccount;
301+
},
302+
);
303+
} else {
304+
sortedInnerData[innerKey] = data[key][innerKey];
305+
}
306+
}
307+
sortedData[key] = sortedInnerData;
308+
}
312309
return sortedData;
313310
}
314311

@@ -317,11 +314,10 @@ export class PythCoreAdapter implements ProgramAdapter {
317314
*/
318315
private sortObjectByKeys(obj: any) {
319316
const sortedObj: any = {};
320-
Object.keys(obj)
321-
.sort()
322-
.forEach((key) => {
323-
sortedObj[key] = obj[key];
324-
});
317+
const keys = Object.keys(obj).sort();
318+
for (const key of keys) {
319+
sortedObj[key] = obj[key];
320+
}
325321
return sortedObj;
326322
}
327323

@@ -347,7 +343,7 @@ export class PythCoreAdapter implements ProgramAdapter {
347343
const changes: Record<string, any> = {};
348344

349345
// Check for changes to existing symbols
350-
Object.keys(uploadedConfig).forEach((symbol) => {
346+
for (const symbol of Object.keys(uploadedConfig)) {
351347
// Remove duplicate publishers
352348
if (
353349
uploadedConfig[symbol]?.priceAccounts?.[0]?.publishers &&
@@ -380,18 +376,18 @@ export class PythCoreAdapter implements ProgramAdapter {
380376
changes[symbol].prev = { ...existingConfig[symbol] };
381377
changes[symbol].new = { ...uploadedConfig[symbol] };
382378
}
383-
});
379+
}
384380

385381
// Check for symbols to remove (in existing but not in uploaded)
386-
Object.keys(existingConfig).forEach((symbol) => {
382+
for (const symbol of Object.keys(existingConfig)) {
387383
if (!uploadedConfig[symbol]) {
388384
changes[symbol] = { prev: {} };
389385
changes[symbol].prev = { ...existingConfig[symbol] };
390386
}
391-
});
387+
}
392388

393389
// Validate that address field is not changed for existing symbols
394-
Object.keys(uploadedConfig).forEach((symbol) => {
390+
for (const symbol of Object.keys(uploadedConfig)) {
395391
if (
396392
existingSymbols.has(symbol) &&
397393
uploadedConfig[symbol].address &&
@@ -402,10 +398,10 @@ export class PythCoreAdapter implements ProgramAdapter {
402398
error: `Address field for product cannot be changed for symbol ${symbol}. Please revert any changes to the address field and try again.`,
403399
};
404400
}
405-
});
401+
}
406402

407403
// Validate that priceAccounts address field is not changed
408-
Object.keys(uploadedConfig).forEach((symbol) => {
404+
for (const symbol of Object.keys(uploadedConfig)) {
409405
if (
410406
existingSymbols.has(symbol) &&
411407
uploadedConfig[symbol].priceAccounts?.[0] &&
@@ -419,10 +415,10 @@ export class PythCoreAdapter implements ProgramAdapter {
419415
error: `Address field for priceAccounts cannot be changed for symbol ${symbol}. Please revert any changes to the address field and try again.`,
420416
};
421417
}
422-
});
418+
}
423419

424420
// Check that no price account has more than the maximum number of publishers
425-
Object.keys(uploadedConfig).forEach((symbol) => {
421+
for (const symbol of Object.keys(uploadedConfig)) {
426422
const maximumNumberOfPublishers = getMaximumNumberOfPublishers(cluster);
427423
if (
428424
uploadedConfig[symbol].priceAccounts?.[0]?.publishers &&
@@ -434,7 +430,7 @@ export class PythCoreAdapter implements ProgramAdapter {
434430
error: `${symbol} has more than ${maximumNumberOfPublishers} publishers.`,
435431
};
436432
}
437-
});
433+
}
438434

439435
return {
440436
isValid: true,

0 commit comments

Comments
 (0)