Skip to content

Commit 9a7d380

Browse files
committed
chore(attachments): use reduce instead map (closes #308)
1 parent 12f72ad commit 9a7d380

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

src/modules/Attachments.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export class Attachments {
2828

2929
const attachmentFields: string[] = [];
3030

31-
const parsedAttachments = (
32-
attachments.map(({ type, photo, video, link, doc, audio, poll, album, textlive, market }) => {
31+
const parsedAttachments = attachments
32+
.reduce<string[]>((parsedAttachments, {
33+
type, photo, video, link, doc, audio, poll, album, textlive, market
34+
}) => {
3335
switch (type) {
3436
case PHOTO: {
3537
const { sizes } = photo;
@@ -63,19 +65,23 @@ export class Attachments {
6365
title += ` - ${name}`;
6466
}
6567

66-
return `[${prefix}: ${title}](${LINK_PREFIX}${this.generateAttachmentContext(video)}?z=${VIDEO}${owner_id}_${id})`;
68+
parsedAttachments.push(
69+
`[${prefix}: ${title}](${LINK_PREFIX}${this.generateAttachmentContext(video)}?z=${VIDEO}${owner_id}_${id})`
70+
);
71+
break;
6772
}
6873
case LINK: {
6974
const { button_text = 'Ссылка', description, title, url } = link;
7075

71-
return `[🔗 ${description || button_text}: ${title}](${url})`;
76+
parsedAttachments.push(`[🔗 ${description || button_text}: ${title}](${url})`);
77+
break;
7278
}
7379
case DOCUMENT: {
7480
const { ext, url, title } = doc;
7581

7682
if (ext === 'gif') {
7783
const filename = `${generateRandomString(6)}.${ext}`;
78-
84+
7985
if (!embed.image) {
8086
files.push(
8187
new MessageAttachment(url, filename)
@@ -92,44 +98,62 @@ export class Attachments {
9298
);
9399
}
94100
} else {
95-
return `[📄 Файл: ${title}](${url})`;
101+
parsedAttachments.push(`[📄 Файл: ${title}](${url})`);
96102
}
97103
break;
98104
}
99105
case AUDIO: {
100106
const { owner_id, id, artist, title } = audio;
101107

102-
return `[🎵 Аудиозапись: ${artist} - ${title}](${LINK_PREFIX}${AUDIO}${owner_id}_${id})`;
108+
parsedAttachments.push(
109+
`[🎵 Аудиозапись: ${artist} - ${title}](${LINK_PREFIX}${AUDIO}${owner_id}_${id})`
110+
);
111+
break;
103112
}
104113
case POLL: {
105114
const { owner_id, id, question } = poll;
106115

107-
return `[📊 Опрос: ${question}](${LINK_PREFIX}${this.generateAttachmentContext(poll)}?w=${POLL}${owner_id}_${id})`;
116+
parsedAttachments.push(
117+
`[📊 Опрос: ${question}](${LINK_PREFIX}${this.generateAttachmentContext(poll)}?w=${POLL}${owner_id}_${id})`
118+
);
119+
break;
108120
}
109121
case ALBUM: {
110122
const { owner_id, id, title } = album;
111123

112-
return `[🖼️ Альбом: ${title}](${LINK_PREFIX}${ALBUM}${owner_id}_${id})`;
124+
parsedAttachments.push(
125+
`[🖼️ Альбом: ${title}](${LINK_PREFIX}${ALBUM}${owner_id}_${id})`
126+
);
127+
break;
113128
}
114129
case MARKET: {
115130
const { owner_id, id, title } = market;
116131

117-
return `[🛍️ Товар: ${title}](${LINK_PREFIX}${MARKET}${owner_id}?w=product${owner_id}_${id})`;
132+
parsedAttachments.push(
133+
`[🛍️ Товар: ${title}](${LINK_PREFIX}${MARKET}${owner_id}?w=product${owner_id}_${id})`
134+
);
135+
break;
118136
}
119137
case MARKET_ALBUM: {
120138
const { owner_id, id, title } = market;
121139

122-
return `[🛍️ Подборка товаров: ${title}](${LINK_PREFIX}${MARKET}${owner_id}?section=${ALBUM}_${id})`;
140+
parsedAttachments.push(
141+
`[🛍️ Подборка товаров: ${title}](${LINK_PREFIX}${MARKET}${owner_id}?section=${ALBUM}_${id})`
142+
);
143+
break;
123144
}
124145
case 'textlive': {
125146
const { textlive_id, title } = textlive;
126147

127-
return `[📣 Репортаж: ${title}](${LINK_PREFIX}textlive${textlive_id})`;
148+
parsedAttachments.push(
149+
`[📣 Репортаж: ${title}](${LINK_PREFIX}textlive${textlive_id})`
150+
);
151+
break;
128152
}
129153
}
130-
})
131-
.filter((attachment) => attachment) as string[]
132-
)
154+
155+
return parsedAttachments;
156+
}, [])
133157
.sort((a, b) => a.localeCompare(b))
134158
.map((attachment) => `\n${attachment}`);
135159

0 commit comments

Comments
 (0)