Skip to content

Commit

Permalink
feat(chore): improved performance using regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Intevel committed Jun 27, 2022
1 parent e767622 commit d7a5abb
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ const gitmojis = {
};

export function convert(content: string, withSpace?: boolean) {
if (!withSpace) withSpace = false;
Object.entries(gitmojis).forEach(([key, value]) => {
if (withSpace) {
content = content.replaceAll(key, value + " ");
} else {
content = content.replaceAll(key, value);
}
});
return content;
}
var re = new RegExp(Object.keys(gitmojis).join("|"), "gi");
return content.replace(re, function(matched) {
if (withSpace) {
// @ts-ignore
return gitmojis[matched.toLowerCase()] + " ";
} else {
// @ts-ignore
return gitmojis[matched.toLowerCase()];
}
})
}

0 comments on commit d7a5abb

Please sign in to comment.