Files
lume/src/utils/truncate.tsx
2023-02-21 14:58:47 +07:00

13 lines
439 B
TypeScript

export const truncate = (fullStr: string, strLen: number, separator: string | unknown[]) => {
if (fullStr.length <= strLen) return fullStr;
separator = separator || '...';
const sepLen = separator.length,
charsToShow = strLen - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);
return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars);
};