cal/packages/lib/text.ts
Lucas Smith d81d772cdf
feat(lib): add more tests to lib package (#7210)
* feat(lib): add more tests to lib package

Add more tests to the lib package to make it more robust overall. Additionally, tidy any methods that can be modified without changing behaviour and tighten types where possible.

* fix(lib): update missed imports

* fix: revert stylistic changes

* Update getSchedule.test.ts

---------

Co-authored-by: Omar López <zomars@me.com>
2023-03-10 22:10:56 +00:00

21 lines
698 B
TypeScript

export const truncate = (text: string, maxLength: number, ellipsis = true) => {
if (text.length <= maxLength) return text;
return `${text.slice(0, maxLength - 3)}${ellipsis ? "..." : ""}`;
};
export const truncateOnWord = (text: string, maxLength: number, ellipsis = true) => {
if (text.length <= maxLength) return text;
// First split on maxLength chars
let truncatedText = text.substring(0, 148);
// Then split on the last space, this way we split on the last word,
// which looks just a bit nicer.
truncatedText = truncatedText.substring(0, Math.min(truncatedText.length, truncatedText.lastIndexOf(" ")));
if (ellipsis) truncatedText += "...";
return truncatedText;
};