Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit fc3d46e

Browse files
committed
refactor(utils): use stringz in cutFrom
1 parent 4916e11 commit fc3d46e

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

package-docker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"sentry-testkit": "^2.1.0",
9090
"shortid": "^2.2.8",
9191
"store": "^2.0.12",
92+
"stringz": "^2.0.0",
9293
"styled-components": "4.1.3",
9394
"timeago-react": "2.0.0",
9495
"uuid": "3.3.2"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"sentry-testkit": "^2.1.0",
121121
"shortid": "^2.2.8",
122122
"store": "^2.0.12",
123+
"stringz": "^2.0.0",
123124
"styled-components": "4.3.2",
124125
"timeago-react": "2.0.0",
125126
"uuid": "^3.3.2"

utils/functions.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import R from 'ramda'
22
import PubSub from 'pubsub-js'
3+
import { limit, length } from 'stringz'
34

45
import { TAG_COLOR_ORDER } from '@config'
56
import { EVENT } from '@constant'
@@ -46,16 +47,7 @@ export const Rlog = (arg = 'Rlog: ') => R.tap(log(arg))
4647
* @param {*number} len 需要显示多少个汉字,两个英文字母相当于一个汉字
4748
*/
4849
export const cutFrom = (str, len = 20) => {
49-
const reg = /[\u4e00-\u9fa5]/g // 匹配中文
50-
const slice = str && str.substring(0, len)
51-
// eslint-disable-next-line no-bitwise
52-
const chineseCharNum = ~~(
53-
slice &&
54-
slice.match(reg) &&
55-
slice.match(reg).length
56-
)
57-
const realen = slice && slice.length * 2 - chineseCharNum
58-
return str && str.substr(0, realen) + (realen < str.length ? '...' : '')
50+
return len >= length(str) ? str : `${limit(str, len, '')}...`
5951
}
6052

6153
// https://stackoverflow.com/questions/9461621/how-to-format-a-number-as-2-5k-if-a-thousand-or-more-otherwise-900-in-javascrip

yarn.lock

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,12 +3430,13 @@ cosmiconfig@^5.0.0, cosmiconfig@^5.2.0, cosmiconfig@^5.2.1:
34303430
js-yaml "^3.13.1"
34313431
parse-json "^4.0.0"
34323432

3433-
coveralls@3.0.3:
3434-
version "3.0.3"
3435-
resolved "https://registry.npmjs.org/coveralls/-/coveralls-3.0.3.tgz#83b1c64aea1c6afa69beaf50b55ac1bc4d13e2b8"
3433+
coveralls@3.0.6:
3434+
version "3.0.6"
3435+
resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.6.tgz#5c63b2759b6781118e7439bd870ba5e9ee428b25"
3436+
integrity sha512-Pgh4v3gCI4T/9VijVrm8Ym5v0OgjvGLKj3zTUwkvsCiwqae/p6VLzpsFNjQS2i6ewV7ef+DjFJ5TSKxYt/mCrA==
34363437
dependencies:
34373438
growl "~> 1.10.0"
3438-
js-yaml "^3.11.0"
3439+
js-yaml "^3.13.1"
34393440
lcov-parse "^0.0.10"
34403441
log-driver "^1.2.7"
34413442
minimist "^1.2.0"
@@ -6846,7 +6847,7 @@ js-tokens@^3.0.2:
68466847
version "3.0.2"
68476848
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
68486849

6849-
js-yaml@^3.11.0, js-yaml@^3.13.1, js-yaml@^3.7.0:
6850+
js-yaml@^3.13.1, js-yaml@^3.7.0:
68506851
version "3.13.1"
68516852
resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
68526853
dependencies:
@@ -10900,6 +10901,13 @@ stringify-package@1.0.0:
1090010901
version "1.0.0"
1090110902
resolved "https://registry.npmjs.org/stringify-package/-/stringify-package-1.0.0.tgz#e02828089333d7d45cd8c287c30aa9a13375081b"
1090210903

10904+
stringz@^2.0.0:
10905+
version "2.0.0"
10906+
resolved "https://registry.yarnpkg.com/stringz/-/stringz-2.0.0.tgz#0a092bc64ed9b42eff2936d0401d2398393d54e9"
10907+
integrity sha512-pRWc5RGpedKEDvQ/ukYs8kS8tKj+cKu5ayOoyOvsavbpiLBcm1dGX6p1o5IagaN11cbfN8tKGpgQ4fHdEq5LBA==
10908+
dependencies:
10909+
unicode-astral-regex "^1.0.1"
10910+
1090310911
strip-ansi@5.2.0, strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
1090410912
version "5.2.0"
1090510913
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz#8c9a536feb6afc962bdfa5b104a5091c1ad9c0ae"
@@ -11388,6 +11396,11 @@ unfetch@4.1.0:
1138811396
version "4.1.0"
1138911397
resolved "https://registry.npmjs.org/unfetch/-/unfetch-4.1.0.tgz#6ec2dd0de887e58a4dee83a050ded80ffc4137db"
1139011398

11399+
unicode-astral-regex@^1.0.1:
11400+
version "1.0.1"
11401+
resolved "https://registry.yarnpkg.com/unicode-astral-regex/-/unicode-astral-regex-1.0.1.tgz#2cab8529480646f9614ddbc7b62158ad05123feb"
11402+
integrity sha512-UP6cmDeiWi8bMDmkyXLRsDuVPTvPjh8Wsz+pHu1VkGgTBl4pUceYAdHXY0cdc6Q3+Z2q7QMzi/0E7L/S/yOvFw==
11403+
1139111404
unicode-canonical-property-names-ecmascript@^1.0.4:
1139211405
version "1.0.4"
1139311406
resolved "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)