https://github.com/lizhengnacl/vision-string
字符串视觉上的长度计算
https://github.com/lizhengnacl/vision-string
emoji javascript length string vision
Last synced: about 1 month ago
JSON representation
字符串视觉上的长度计算
- Host: GitHub
- URL: https://github.com/lizhengnacl/vision-string
- Owner: lizhengnacl
- Created: 2017-08-17T12:53:50.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-17T13:01:32.000Z (almost 9 years ago)
- Last Synced: 2025-12-09T02:41:48.290Z (7 months ago)
- Topics: emoji, javascript, length, string, vision
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
一种多行文本文末省略号的JS解决方案
字符串视觉感官上的长度计算方案
核心思想:
1. 将单个数字、字幕、符号算作一个长度
2. 将中文、表情符等算作两个长度
3. 将emoji表情符算作两个长度
使用
```
npm install vision-string
```
测试
```
npm t
```
```
test(t => {
let { length, getLength, isDouble} = b;
t.is(getLength('asdfghjkl'), 9);
t.is(getLength('@#$%^&*()'), 9);
t.is(getLength('123456789'), 9);
t.is(getLength('.........'), 9);
t.is(getLength('中中中中中'), 10);
t.is(getLength('😄😄😄😄😄'), 10);
t.is(isDouble('中'), true);
t.is(isDouble('😄'), true);
t.is(isDouble('1'), false);
t.is(isDouble('a'), false);
t.is(isDouble('@'), false);
t.is(length('1a中', 1), '1');
t.is(length('1a中', 2), '1a');
t.is(length('1a中', 3), '1a');
t.is(length('1a中', 4), '1a中');
t.is(length('1a中😄', 6), '1a中😄');
t.is(length('1a中😄😄', 8), '1a中😄😄');
t.is(length('😄😄😄😄😄😄😄😄😄😄😄😄😄😄', 4), '😄😄');
});
```