-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…runcation Truncate original messages more gently when replying
- Loading branch information
Showing
6 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Truncate original messages more gently when replying |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
const { trimString } = require("../../lib/util/TrimString.js"); | ||
|
||
describe("trimString", function() { | ||
it("should not cut unicode characters in half", () => { | ||
const input = "lol 😅"; | ||
const result = trimString(input, 5); | ||
expect(result).toEqual(input); | ||
}); | ||
|
||
it("should trim trailing whitespace by itself", () => { | ||
const input = "lol 😅"; | ||
const result = trimString(input, 4); | ||
expect(result).toEqual('lol'); | ||
}); | ||
|
||
it('should stop trimming at the word boundary if reasonable', () => { | ||
const input = "this sentence is waaaaay too long"; | ||
const result = trimString(input, 20); | ||
expect(result).toEqual('this sentence is'); | ||
}); | ||
|
||
it('should give up looking for a word boundary if result would become too short', () => { | ||
const input = "we're in Llanfairpwllgwyngyll"; | ||
const result = trimString(input, 24); | ||
expect(result).toContain("we're in Llan"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2021 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// since trimRight()/trimEnd() may break unicode characters | ||
function trimTrailingWhitespace(input: string): string { | ||
return input.replace(/\s*$/u, ''); | ||
} | ||
|
||
// a unicode-aware substring(0, $x) that tries to not break words if possible | ||
export function trimString(input: string, maxLength: number): string { | ||
const re = new RegExp(`^([\\s\\S]{0,${maxLength}})(\\p{L}?)`, 'u'); | ||
const match = input.match(re); | ||
|
||
if (!match) { | ||
// fallback to a dumb substring() if the regex failed for any reason | ||
return trimTrailingWhitespace(input.substring(0, maxLength)); | ||
} | ||
|
||
const trimmed = trimTrailingWhitespace(match[1]); | ||
|
||
if (match[2]) { | ||
// find as much as you can that is followed by a word boundary, | ||
// shorter than what we have now, but at least 75% of the desired length | ||
const smallerMatch = trimmed.match(/^([\s\S]*\S)\b[\s\S]/u); | ||
const minLength = maxLength * 0.75; | ||
|
||
if (smallerMatch && smallerMatch[1].length >= minLength) { | ||
return smallerMatch[1]; | ||
} | ||
} | ||
|
||
return trimmed; | ||
} |