Skip to content

Commit

Permalink
Merge pull request #182 from element-hq/t3chguy/segmenter
Browse files Browse the repository at this point in the history
Switch from graphemer to Intl.Segmenter
  • Loading branch information
t3chguy authored Jun 14, 2024
2 parents d92ec2b + 188741d commit 7135b63
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 21 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
"@radix-ui/react-tooltip": "^1.0.6",
"@storybook/manager-api": "^8.1.1",
"classnames": "^2.3.2",
"graphemer": "^1.4.0",
"vaul": "^0.7.0"
},
"peerDependencies": {
Expand Down
22 changes: 3 additions & 19 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import GraphemeSplitter from "graphemer";

export const MX_USERNAME_PREFIX = "@";
export const MX_ROOM_PREFIX = "#";
export const MX_ALIAS_PREFIX = "+";

/**
* The CommonJS output of the graphemer package looks wrong,
* this is a workaround
* To remove when https://github.com/flmnt/graphemer/issues/11 is fixed
* @param value the constructor or wrapper with `default`
* @returns the Graphemer constructor
*/
function interopDefault<T>(value: T): T {
if ((value as unknown as { default: T }).default) {
return (value as unknown as { default: T }).default;
}

return value;
}
const graphemeSegmenter = new Intl.Segmenter();

/**
* returns the first (non-sigil) character of 'name',
Expand All @@ -52,7 +37,6 @@ export function getInitialLetter(name: string): string {
}

// rely on a grapheme cluster splitter so that we don't break apart compound emojis
const splitter = new (interopDefault(GraphemeSplitter))();
const result = splitter.iterateGraphemes(name).next();
return result.done ? "" : result.value;
const result = graphemeSegmenter.segment(name)[Symbol.iterator]().next();
return result.done ? "" : result.value.segment;
}
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default defineConfig({
"react/jsx-runtime",
"react/jsx-dev-runtime",
"classnames",
"graphemer",
"vaul",
"@radix-ui/react-context-menu",
"@radix-ui/react-dropdown-menu",
Expand Down

0 comments on commit 7135b63

Please sign in to comment.