Truncate a string by weighted UTF-16 length.
Code units from U+0000 through U+00FF count as one; all other code units count as two. Optional truncation text is appended after the length limit. This does not measure bytes or rendered width and can split surrogate pairs.
Usage:
import { truncateByWeightedLength } from "mazey";const ret = truncateByWeightedLength("hello world", 5);console.log(ret); Copy
import { truncateByWeightedLength } from "mazey";const ret = truncateByWeightedLength("hello world", 5);console.log(ret);
Output:
hello Copy
hello
String to truncate.
Maximum weighted length.
Optional
Text appended when truncation occurs.
Whether to append truncation text.
The truncated string.
Truncate a string by weighted UTF-16 length.
Code units from U+0000 through U+00FF count as one; all other code units count as two. Optional truncation text is appended after the length limit. This does not measure bytes or rendered width and can split surrogate pairs.
Usage:
Output: