-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: truncate short description exceeding the byte limit (#143)
* feat: truncate short description exceeding the byte limit * fix tests
- Loading branch information
1 parent
3ba533f
commit 4b1a4bb
Showing
7 changed files
with
80 additions
and
47 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
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,10 @@ | ||
import {truncateToBytes} from '../src/utils' | ||
|
||
describe('truncate to bytes tests', () => { | ||
test('unicode aware truncation to a number of bytes', async () => { | ||
expect(truncateToBytes('test string to be truncated', 10)).toEqual( | ||
'test strin' | ||
) | ||
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁') | ||
}) | ||
}) |
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
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,14 @@ | ||
import unicodeSubstring = require('unicode-substring') | ||
|
||
export function getErrorMessage(error: unknown) { | ||
if (error instanceof Error) return error.message | ||
return String(error) | ||
} | ||
|
||
export function truncateToBytes(s: string, n: number): string { | ||
let len = n | ||
while (Buffer.byteLength(s) > n) { | ||
s = unicodeSubstring(s, 0, len--) | ||
} | ||
return s | ||
} |