Files
ezbookkeeping/src/filters/textLimit.js
T
2021-03-15 00:24:45 +08:00

20 lines
439 B
JavaScript

export default function (value, maxLength) {
let length = 0;
for (let i = 0; i < value.length; i++) {
const c = value.charCodeAt(i);
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
length++;
} else {
length += 2;
}
}
if (length <= maxLength || maxLength <= 3) {
return value;
}
return value.substr(0, maxLength - 3) + '...';
}