From 53587ebaf504431fbf6f58db871f12294e282fe8 Mon Sep 17 00:00:00 2001 From: IdiNium <47635037+idinium96@users.noreply.github.com> Date: Mon, 1 Aug 2022 04:58:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor=20duplicated=20function?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/Commands/sub-classes/Misc.ts | 15 +-------------- src/classes/Listings.ts | 16 ++-------------- src/lib/tools/getAttachmentName.ts | 19 +++++++++++++++++++ src/lib/tools/getHighValue.ts | 17 +---------------- 4 files changed, 23 insertions(+), 44 deletions(-) create mode 100644 src/lib/tools/getAttachmentName.ts diff --git a/src/classes/Commands/sub-classes/Misc.ts b/src/classes/Commands/sub-classes/Misc.ts index 23802ae6b..074f87c3d 100644 --- a/src/classes/Commands/sub-classes/Misc.ts +++ b/src/classes/Commands/sub-classes/Misc.ts @@ -8,8 +8,7 @@ import CommandParser from '../../CommandParser'; import Bot from '../../Bot'; import { Discord, Stock } from '../../Options'; import { pure, timeNow, uptime, testPriceKey } from '../../../lib/tools/export'; -import { killstreakersData, sheensData, spellsData } from '../../../lib/data'; -import { Paints, StrangeParts } from '@tf2autobot/tf2-schema'; +import getAttachmentName from '../../../lib/tools/getAttachmentName'; type Misc = 'time' | 'uptime' | 'pure' | 'rate' | 'owner' | 'discord' | 'stock'; type CraftUncraft = 'craftweapon' | 'uncraftweapon'; @@ -374,15 +373,3 @@ export default class MiscCommands { return stock; } } - -function getKeyByValue(object: { [key: string]: any }, value: any): string { - return Object.keys(object).find(key => object[key] === value); -} - -function getAttachmentName(attachment: string, pSku: string, paints: Paints, parts: StrangeParts): string { - if (attachment === 's') return getKeyByValue(spellsData, pSku); - else if (attachment === 'sp') return getKeyByValue(parts, pSku); - else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSku); - else if (attachment === 'ks') return getKeyByValue(sheensData, pSku); - else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSku.replace('p', ''))); -} diff --git a/src/classes/Listings.ts b/src/classes/Listings.ts index d39cda2b9..76e4e440e 100644 --- a/src/classes/Listings.ts +++ b/src/classes/Listings.ts @@ -7,11 +7,11 @@ import Bot from './Bot'; import Pricelist, { Entry, PricesObject } from './Pricelist'; import log from '../lib/logger'; import { exponentialBackoff } from '../lib/helpers'; -import { noiseMakers, spellsData, killstreakersData, sheensData } from '../lib/data'; +import { noiseMakers } from '../lib/data'; import { DictItem } from './Inventory'; import { PaintedNames } from './Options'; -import { Paints, StrangeParts } from '@tf2autobot/tf2-schema'; import ListingManager from '@tf2autobot/bptf-listings'; +import getAttachmentName from '../lib/tools/getAttachmentName'; export default class Listings { private checkingAllListings = false; @@ -717,18 +717,6 @@ export default class Listings { type Attachment = 's' | 'sp' | 'ke' | 'ks' | 'p'; -function getKeyByValue(object: { [key: string]: any }, value: any): string { - return Object.keys(object).find(key => object[key] === value); -} - -function getAttachmentName(attachment: string, pSKU: string, paints: Paints, parts: StrangeParts): string { - if (attachment === 's') return getKeyByValue(spellsData, pSKU); - else if (attachment === 'sp') return getKeyByValue(parts, pSKU); - else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU); - else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU); - else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', ''))); -} - function boldDetails(str: string, style: number): string { // https://lingojam.com/BoldTextGenerator diff --git a/src/lib/tools/getAttachmentName.ts b/src/lib/tools/getAttachmentName.ts new file mode 100644 index 000000000..a78757334 --- /dev/null +++ b/src/lib/tools/getAttachmentName.ts @@ -0,0 +1,19 @@ +import { spellsData, killstreakersData, sheensData } from '../data'; +import { Paints, StrangeParts } from '@tf2autobot/tf2-schema'; + +function getKeyByValue(object: { [key: string]: any }, value: any): string { + return Object.keys(object).find(key => object[key] === value); +} + +export default function getAttachmentName( + attachment: string, + pSKU: string, + paints: Paints, + parts: StrangeParts +): string { + if (attachment === 's') return getKeyByValue(spellsData, pSKU); + else if (attachment === 'sp') return getKeyByValue(parts, pSKU); + else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU); + else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU); + else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', ''))); +} diff --git a/src/lib/tools/getHighValue.ts b/src/lib/tools/getHighValue.ts index a59b4f602..6b9aa323f 100644 --- a/src/lib/tools/getHighValue.ts +++ b/src/lib/tools/getHighValue.ts @@ -1,9 +1,8 @@ import SKU from '@tf2autobot/tf2-sku'; import { Items } from '@tf2autobot/tradeoffer-manager'; -import { spellsData, killstreakersData, sheensData } from '../data'; import Bot from '../../classes/Bot'; -import { Paints, StrangeParts } from '@tf2autobot/tf2-schema'; import { testPriceKey } from '../tools/export'; +import getAttachmentName from './getAttachmentName'; interface ItemsWithName { [name: string]: string; @@ -75,17 +74,3 @@ export default function getHighValueItems(items: Items, bot: Bot): ItemsWithName } type Attachment = 's' | 'sp' | 'ke' | 'ks' | 'p'; - -function getAttachmentName(attachment: string, pSKU: string, paints: Paints, parts: StrangeParts): string { - if (attachment === 's') return getKeyByValue(spellsData, pSKU); - else if (attachment === 'sp') return getKeyByValue(parts, pSKU); - else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSKU); - else if (attachment === 'ks') return getKeyByValue(sheensData, pSKU); - else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSKU.replace('p', ''))); -} - -function getKeyByValue(object: { [key: string]: any }, value: any): string { - const keys = Object.keys(object); - const key = keys.find(key => object[key] === value); - return key; -}