Jelajahi Sumber

refactor: component util fix

Sendya 6 tahun lalu
induk
melakukan
554835f5c8

+ 1 - 1
src/components/Ellipsis/Ellipsis.vue

@@ -1,6 +1,6 @@
 <script>
 import Tooltip from 'ant-design-vue/es/tooltip'
-import { cutStrByFullLength, getStrFullLength } from '@/components/_util/StringUtil'
+import { cutStrByFullLength, getStrFullLength } from '@/components/_util/util'
 /*
     const isSupportLineClamp = document.body.style.webkitLineClamp !== undefined;
 

+ 0 - 25
src/components/_util/StringUtil.js

@@ -1,25 +0,0 @@
-
-export const getStrFullLength = (str = '') =>
-  str.split('').reduce((pre, cur) => {
-    const charCode = cur.charCodeAt(0)
-    if (charCode >= 0 && charCode <= 128) {
-      return pre + 1
-    }
-    return pre + 2
-  }, 0)
-
-export const cutStrByFullLength = (str = '', maxLength) => {
-  let showLength = 0
-  return str.split('').reduce((pre, cur) => {
-    const charCode = cur.charCodeAt(0)
-    if (charCode >= 0 && charCode <= 128) {
-      showLength += 1
-    } else {
-      showLength += 2
-    }
-    if (showLength <= maxLength) {
-      return pre + cur
-    }
-    return pre
-  }, '')
-}

+ 34 - 0
src/components/_util/util.js

@@ -10,3 +10,37 @@
 export function filterEmpty (children = []) {
   return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
 }
+
+/**
+ * 获取字符串长度,英文字符 长度1,中文字符长度2
+ * @param {*} str
+ */
+export const getStrFullLength = (str = '') =>
+  str.split('').reduce((pre, cur) => {
+    const charCode = cur.charCodeAt(0)
+    if (charCode >= 0 && charCode <= 128) {
+      return pre + 1
+    }
+    return pre + 2
+  }, 0)
+
+/**
+ * 截取字符串,根据 maxLength 截取后返回
+ * @param {*} str
+ * @param {*} maxLength
+ */
+export const cutStrByFullLength = (str = '', maxLength) => {
+  let showLength = 0
+  return str.split('').reduce((pre, cur) => {
+    const charCode = cur.charCodeAt(0)
+    if (charCode >= 0 && charCode <= 128) {
+      showLength += 1
+    } else {
+      showLength += 2
+    }
+    if (showLength <= maxLength) {
+      return pre + cur
+    }
+    return pre
+  }, '')
+}