[Text] Add Word-Boundary Preserving Smart Text Truncator in src/lib/text/smart-truncate.ts

Open Beginner friendly
#760 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@rudraparmar76 is already working on this.

Since Nov 7, 2026.

  • #764 by @rudraparmar76 — open

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

area:data difficulty:easy enhancement good first issue
🎯 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
  1. Module Implementation: Create src/lib/text/smart-truncate.ts
  2. Unit Test Suite: Create tests/smart-truncate.test.ts using native node:test and node: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:
  1. If text.length <= maxLength, return text unchanged.
  2. Default ellipsis is '…' (length 1). If maxLength <= ellipsis.length, return text.slice(0, maxLength) without ellipsis.
  3. Compute available character budget: budget = maxLength - ellipsis.length.
  4. If preserveWordBoundary is false: slice to budget, trim trailing spaces, and append ellipsis.
  5. If preserveWordBoundary is true (default):
    • Candidate substring: candidate = text.slice(0, budget + 1).
    • Find last whitespace in candidate at or before index budget.
    • If a whitespace exists, slice up to that whitespace, trim trailing spaces and trailing punctuation ([,;:.!?]), and append ellipsis.
    • If no whitespace exists (e.g. single giant word), fallback to text.slice(0, budget) and append ellipsis.
  6. Validation: Throws TypeError if text is not a string, or maxLength is 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TheVicky1/Pact_OS

All issues in TheVicky1/Pact_OS

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.