From c058b1d1e75a1a57847828485602201a19bb9212 Mon Sep 17 00:00:00 2001 From: Walidoux Date: Wed, 19 Apr 2023 17:14:34 +0000 Subject: [PATCH] feat(mapping): add convertTXT --- src/mapping/convertTXT.ts | 55 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/mapping/convertTXT.ts b/src/mapping/convertTXT.ts index cd35ce4..692e868 100644 --- a/src/mapping/convertTXT.ts +++ b/src/mapping/convertTXT.ts @@ -1,3 +1,54 @@ -export const convertTXT = (data: string) => { - console.log(data) +import { parseData } from '../utils/parseData' + +interface IBadge { + code: string + name?: string + description?: string +} + +const badgePattern = /^badge_(?:name|desc)_([^=]+)/gim +const descriptionPattern = /^badge_desc_(\s*\w+)/ +const namePattern = /^badge_name_(\s*\w+)/ + +export const convertTXT = async (data: string): Promise => { + const badges: IBadge[] = [] + + const lines = data.split(/\r?\n/) + + for (const line of lines) { + const [key, value] = line.split('=') + const badge = key.match(badgePattern) + + if (badge != null) { + if (key.match(namePattern) != null) { + const badgeCode = key.match(namePattern)?.[1] as string + const existingBadge = badges.filter((badge) => { + return badge.code === badgeCode + })[0] + + if (Boolean(existingBadge)) { + const index = badges.indexOf(existingBadge) + badges[index].name = value + } else { + badges.push({ code: badgeCode, name: value }) + } + } else if (key.match(descriptionPattern) != null) { + const badgeCode = key.match(descriptionPattern)?.[1] as string + const existingBadge = badges.filter((badge) => { + return badge.code === badgeCode + })[0] + + if (Boolean(existingBadge)) { + const index = badges.indexOf(existingBadge) + badges[index].description = value + } else { + badges.push({ code: badgeCode, description: value }) + } + } + + lines.splice(lines.indexOf(line), 1) + } + } + + return await parseData('Badges', badges) }