Skip to content

truncateLeft returns a longer string than the input when len is 1 #37368

Closed as not planned

Description

@chinesepowered

Description

truncateLeft(str, len) in packages/tui/src/util/locale.ts returns a string longer than the input when len === 1.

It ends with "…" + str.slice(-(len - 1)). At len === 1 that's str.slice(-0), and slice(-0) is slice(0) — the whole string. So truncateLeft("abcdef", 1) returns "…abcdef" (7 chars) instead of "…" (1 char).

The sibling truncate() right above it handles the same edge correctly (str.slice(0, 0) + "…" → "…"), so this is an inconsistency between the two.

Current callers all pass a width ≥ 2 (e.g. Math.max(2, ...)), so it isn't hit in practice today — but it's a latent off-by-one that will overflow the moment a caller passes 1.

Steps to reproduce

  1. import { truncateLeft } from "packages/tui/src/util/locale.ts"
  2. Call truncateLeft("abcdef", 1)
  3. Expected "…"; actual "…abcdef"

Activity

  1. github-actions commented on Sep 15, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions