Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→string-truncatornext

string-truncator

Over-engineered string truncation for web UI's

Downloads per monthMIT Licenselibera manifesto
  • the top
  • THE PURPOSE
  • FEATURES
  • API — TRUNCATE()
  • API — DEFAULTS
  • API — VERSION

The Purpose

It is a function which trims a given string, to accommodate the “X letters wide (opts.maxLen), Y lines-long (opts.maxLines)” cramped spaces such as aside UI components.

You can see it here, on this page, on the sidebar on the right ⤴

Features

  • Super lightweight
  • Extremely performant
  • Multilingual — supports all alphabets: Latin (from English to Lithuanian), Cyrillic (from Russian to Chechen) and Hebrew
  • Two trusted dependencies only: one our own and one from lodash
  • Source in TS
  • Automatically-generated type definitions
  • Supports native ES Modules
  • Browser-Compatible
  • 100% code coverage unit tests

API — truncate()

The main function truncate() is imported like this:

It’s a function which takes two input arguments:

Input argumentTypeObligatoryDescription
str
Type: String
Obligatory: yes
strStringyesThe string in which you want to perform a search
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object.

None of the input arguments will be mutated by this program; we have unit tests to prove that.

The Optional Options Object has the following shape:

KeyTypeDefaultDescription
maxLen
Type: Number
Default: 10
maxLenNumber10How many lengths of the longest character from opts.letterWidths is allowed per-line?
maxLines
Type: Number
Default: 2
maxLinesNumber2How many lines are allowed, maximum?
ellipsisLen
Type: Number
Default: 155
ellipsisLenNumber155Optionally, override the ellipsis length to optimise the calculations.
monospace
Type: Boolean
Default: false
monospaceBooleanfalseOptionally, disable the letter lengths being used. When enabled, it crops by the character count, as if it was a monospace font.
noEmpty
Type: Boolean
Default: true
noEmptyBooleantrueOptionally, you can disable the throwing when the empty string argument has been passed.
letterWidths
Type: Plain object
Default: see below
letterWidthsPlain objectsee belowWhile the default set here is based on “Outfit” font letter widths, you can pass your own font’s widths for each letter.

The function will return a plain object:

Key’s nameTypeDescription
result
Type: String
resultStringA truncated string
addEllipsis
Type: Boolean
addEllipsisBooleanShould you append an ellipsis (…/\u2026) or not.

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

PS. Sorry the Hebrew characters are right-to-left, they display JSON lines the other way-around ↑

The opts.letterWidths are based on Outfit fontopens in a new tab, as used on this website. You are free to re-use the settings as-is or override them with your font’s widths.

API — version

You can import version:

↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab