Skip to content

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

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"

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions