[Text] Add Word-Boundary Preserving Smart Text Truncator in src/lib/text/smart-truncate.ts
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- frontend
Research direction
Create src/lib/text/smart-truncate.ts and tests/smart-truncate.test.ts; start by checking the project’s TypeScript configuration and existing node:test patterns. Implement the specified truncation rules and cover the listed boundary, punctuation, custom ellipsis, and unbroken-word cases. Done when npx tsc --noEmit passes and the tests verify those behaviors.
Written by the indexing model from the issue text.
Description
🎯 Feature Overview & Context
In PACT OS, displaying goal statements, habit descriptions, and accountability check-in notes inside compact UI cards, notification banners, or mobile previews requires truncating text to a maximum character budget. Standard string slicing bluntly cuts words in half (e.g. "Master algorithmic prob…"), creating ugly UI artifacts. This pure utility truncates text cleanly at natural word boundaries while respecting maximum length including the ellipsis symbol.
🧩 Scope & Deliverables
- Module Implementation: Create
src/lib/text/smart-truncate.ts - Unit Test Suite: Create
tests/smart-truncate.test.tsusing nativenode:testandnode:assert/strict.
📐 Specification & Interface Contracts
export interface SmartTruncateOptions {
/** Ellipsis indicator to append (default: '…') */
ellipsis?: string;
/** Whether to truncate cleanly at word whitespace boundaries (default: true) */
preserveWordBoundary?: boolean;
}
export function smartTruncate(
text: string,
maxLength: number,
options?: SmartTruncateOptions
): string;
Truncation Pipeline & Rules:
- If
text.length <= maxLength, returntextunchanged. - Default
ellipsisis'…'(length 1). IfmaxLength <= ellipsis.length, returntext.slice(0, maxLength)without ellipsis. - Compute available character budget:
budget = maxLength - ellipsis.length. - If
preserveWordBoundaryisfalse: slice tobudget, trim trailing spaces, and appendellipsis. - If
preserveWordBoundaryistrue(default):- Candidate substring:
candidate = text.slice(0, budget + 1). - Find last whitespace in
candidateat or before indexbudget. - If a whitespace exists, slice up to that whitespace, trim trailing spaces and trailing punctuation (
[,;:.!?]), and appendellipsis. - If no whitespace exists (e.g. single giant word), fallback to
text.slice(0, budget)and appendellipsis.
- Candidate substring:
- Validation: Throws
TypeErroriftextis not a string, ormaxLengthis not an integer or negative (< 0).
🧪 Acceptance Criteria
- TypeScript strict mode passes with 0 errors (
npx tsc --noEmit). - Uses extensionless import in test file (
import { smartTruncate } from '../src/lib/text/smart-truncate'). - Unit tests cover string shorter than maxLength, boundary matching whitespace, trailing punctuation cleanup, custom ellipsis (e.g.
'...'), and fallback on unbroken words. - Zero external dependencies added.
- Dominant language
- TypeScript
- Stars
- 5
- Forks
- 7
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 79
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TheVicky1/Pact_OS
-
area:finance difficulty:easy enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area:habits difficulty:easy enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
area:analytics difficulty:easy enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
area:data beginner-friendly difficulty:easy enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
beginner-friendly difficulty:easy enhancement gamification good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
All issues in TheVicky1/Pact_OS
Similar issues
-
Waiting for: Product Owner
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
getsentry/sentry-javascript#26203 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
bytedance/flowgram.ai#1235 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
devswha/herdr-web-ui#3325 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100