signal-desktop/ts/util/getTextWithMentions.ts

18 lines
503 B
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { BodyRangesType } from '../types/Util';
2020-09-18 21:43:57 +00:00
export function getTextWithMentions(
bodyRanges: BodyRangesType,
text: string
): string {
2020-11-03 01:19:52 +00:00
return bodyRanges
.sort((a, b) => b.start - a.start)
.reduce((acc, { start, length, replacementText }) => {
const left = acc.slice(0, start);
const right = acc.slice(start + length);
return `${left}@${replacementText}${right}`;
}, text);
2020-09-18 21:43:57 +00:00
}