signal-desktop/ts/util/getTextWithMentions.ts

19 lines
617 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
2022-11-10 04:59:36 +00:00
import type { DraftBodyRangeType, DraftBodyRangesType } from '../types/Util';
2020-09-18 21:43:57 +00:00
export function getTextWithMentions(
2022-11-10 04:59:36 +00:00
bodyRanges: DraftBodyRangesType,
2020-09-18 21:43:57 +00:00
text: string
): string {
2022-11-10 04:59:36 +00:00
const sortableBodyRanges: Array<DraftBodyRangeType> = bodyRanges.slice();
return sortableBodyRanges
2020-11-03 01:19:52 +00:00
.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
}