From 9dc7ab5a53a2b18c0f725f2cde53930ac6aac071 Mon Sep 17 00:00:00 2001 From: AlexanderZhao Date: Fri, 29 Mar 2024 17:27:44 +0800 Subject: [PATCH] feat(): js i18n --- i7h.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 i7h.js diff --git a/i7h.js b/i7h.js new file mode 100644 index 0000000..f908e99 --- /dev/null +++ b/i7h.js @@ -0,0 +1,15 @@ +export const i18n = (str) => { + if (typeof str !== "string") { + throw new Error("Not string"); + } + + return str.replace(/\w+/g, ellipsis); +}; + +const ellipsis = (word) => { + if (word.length < 3) { + return word; + } + + return `${word.slice(0, 1)}${word.length - 2}${word.slice(-1)}`; +};