Trim the whole of a message's text

Co-authored-by: Chris Svenningsen <chris@carbonfive.com>
This commit is contained in:
Sidney Keese 2020-11-13 11:54:11 -08:00 committed by GitHub
parent e9f37ec46b
commit 34be0744d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 61 deletions

View file

@ -41,82 +41,53 @@ export const isInsertMentionOp = (op: Op): op is InsertMentionOp =>
isSpecificInsertOp(op, 'mention'); isSpecificInsertOp(op, 'mention');
export const getTextFromOps = (ops: Array<DeltaOperation>): string => export const getTextFromOps = (ops: Array<DeltaOperation>): string =>
ops.reduce((acc, { insert }, index) => { ops
if (typeof insert === 'string') { .reduce((acc, op) => {
let textToAdd; if (typeof op.insert === 'string') {
switch (index) { return acc + op.insert;
case 0: {
textToAdd = insert.trimLeft();
break;
}
case ops.length - 1: {
textToAdd = insert.trimRight();
break;
}
default: {
textToAdd = insert;
break;
}
} }
const textWithoutNewlines = textToAdd.replace(/\n+$/, '');
return acc + textWithoutNewlines;
}
if (insert.emoji) { if (isInsertEmojiOp(op)) {
return acc + insert.emoji; return acc + op.insert.emoji;
} }
if (insert.mention) { if (isInsertMentionOp(op)) {
return `${acc}@${insert.mention.title}`; return `${acc}@${op.insert.mention.title}`;
} }
return acc; return acc;
}, ''); }, '')
.trim();
export const getTextAndMentionsFromOps = ( export const getTextAndMentionsFromOps = (
ops: Array<Op> ops: Array<Op>
): [string, Array<BodyRangeType>] => { ): [string, Array<BodyRangeType>] => {
const mentions: Array<BodyRangeType> = []; const mentions: Array<BodyRangeType> = [];
const text = ops.reduce((acc, op, index) => { const text = ops
if (typeof op.insert === 'string') { .reduce((acc, op) => {
let textToAdd; if (typeof op.insert === 'string') {
switch (index) { return acc + op.insert;
case 0: {
textToAdd = op.insert.trimLeft();
break;
}
case ops.length - 1: {
textToAdd = op.insert.trimRight();
break;
}
default: {
textToAdd = op.insert;
break;
}
} }
const textWithoutNewlines = textToAdd.replace(/\n+$/, ''); if (isInsertEmojiOp(op)) {
return acc + textWithoutNewlines; return acc + op.insert.emoji;
} }
if (isInsertEmojiOp(op)) { if (isInsertMentionOp(op)) {
return acc + op.insert.emoji; mentions.push({
} length: 1, // The length of `\uFFFC`
mentionUuid: op.insert.mention.uuid,
replacementText: op.insert.mention.title,
start: acc.length,
});
if (isInsertMentionOp(op)) { return `${acc}\uFFFC`;
mentions.push({ }
length: 1, // The length of `\uFFFC`
mentionUuid: op.insert.mention.uuid,
replacementText: op.insert.mention.title,
start: acc.length,
});
return `${acc}\uFFFC`; return acc;
} }, '')
.trim();
return acc;
}, '');
return [text, mentions]; return [text, mentions];
}; };

View file

@ -156,6 +156,31 @@ describe('getTextAndMentionsFromOps', () => {
}, },
]); ]);
}); });
it('does not trim newlines padding mentions', () => {
const ops = [
{ insert: 'test \n' },
{
insert: {
mention: {
uuid: 'abcdef',
title: '@fred',
},
},
},
{ insert: '\n test' },
];
const [resultText, resultMentions] = getTextAndMentionsFromOps(ops);
assert.equal(resultText, 'test \n\uFFFC\n test');
assert.deepEqual(resultMentions, [
{
length: 1,
mentionUuid: 'abcdef',
replacementText: '@fred',
start: 6,
},
]);
});
}); });
}); });