Skip to content

Commit 4891f5d

Browse files
committed
Truncate unicode aware
1 parent c55d83e commit 4891f5d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/ia-topnav/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@internetarchive/ia-topnav",
3-
"version": "1.1.30-alpha.0",
3+
"version": "1.1.30-alpha.1",
44
"description": "Top nav for Internet Archive",
55
"license": "AGPL-3.0-only",
66
"main": "index.js",

packages/ia-topnav/src/primary-nav.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,23 @@ class PrimaryNav extends TrackedElement {
7979
);
8080
}
8181

82+
get isRTL() {
83+
const ltrChars = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF';
84+
const rtlChars = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';
85+
const rtlCheck = new RegExp('^[^'+ltrChars+']*['+rtlChars+']');
86+
87+
return rtlCheck.test(this.screenName);
88+
}
89+
8290
get truncatedScreenName() {
83-
if (this.screenName && this.screenName.length > 10) {
84-
return `${this.screenName.substr(0, 9)}…`;
91+
if (this.screenName && [...this.screenName].length > 10) {
92+
// Works with RTL and Unicode
93+
const truncated = [...this.screenName].slice(0, 9).join('');
94+
if (this.isRTL) {
95+
return `…${truncated}`;
96+
} else {
97+
return `${truncated}…`;
98+
}
8599
}
86100
return this.screenName;
87101
}

0 commit comments

Comments
 (0)