Files
ezbookkeeping/src/filters/textLimit.js
T
2023-04-09 01:04:00 +08:00

20 lines
442 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.substring(0, maxLength - 3) + '...';
}