Skip to content

Commit

Permalink
feat(fmt): use template substitution instead of +=
Browse files Browse the repository at this point in the history
  • Loading branch information
MKRhere committed Nov 18, 2022
1 parent d1d223e commit 6e081dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions release-notes/4.11.0.md
Expand Up @@ -83,3 +83,7 @@ You might be happy, or fairly upset about this development. But it was important
This meant you could use `bot.on("message")`, or `bot.on("text")` (text here is a message type, and not an update type, so this was really making sure that `update.message.text` existed). However, when polls were introduced, this caused a conflict. `bot.on("poll")` would match both `update.poll` (update about stopped polls sent by the bot) and `update.message.poll` (a message that is a native poll). At type-level, both objects will show as available, which was wrong.

Besides, this type of filters really limited how far we could go with Telegraf. That's why we introduced filters, which are way more powerful and flexible!

## Minor changes

* Format helpers (`"telegraf/format"`) now use template string substitution instead of naively using `+=`. ([Discussion](https://t.me/TelegrafJSChat/87251))
10 changes: 7 additions & 3 deletions src/core/helpers/formatting.ts
Expand Up @@ -49,16 +49,20 @@ export function _fmt(kind: Types.Text | 'very-plain', opts?: object) {
parts = typeof parts === 'string' ? [parts] : parts
for (let i = 0; i < parts.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
text += parts[i]!
text = `${text}${parts[i]!}`

const item = items[i]
if (item == null) continue
if (!(item instanceof FmtString)) {
text += item
// item is some value that's not FmtString
text = `${text}${item}`
continue
}

// item is FmtString
for (const child of item.entities || [])
entities.push({ ...child, offset: text.length + child.offset })
text += item.text
text = `${text}${item.text}`
}
if (kind !== 'very-plain')
entities.unshift({ type: kind, offset: 0, length: text.length, ...opts })
Expand Down

0 comments on commit 6e081dc

Please sign in to comment.