npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@amiearth/truncate-smart

v0.1.9

Published

Zero-dep, TypeScript-first string truncation with word boundary, HTML strip, and emoji safety

Readme

truncate-smart

Published on npm as @amiearth/truncate-smart. Zero-dependency, TypeScript-first string truncation. Handles word boundaries (including Thai/CJK via Intl.Segmenter), optional HTML tag stripping, grapheme-safe emoji boundaries, and protected substrings.

Install

pnpm add @amiearth/truncate-smart
# or
yarn add @amiearth/truncate-smart
# or
npm install @amiearth/truncate-smart

Quick start

import { truncate, createTruncator, truncateAll, stripHtml } from "@amiearth/truncate-smart"

truncate("Hello world today", 14)
// => "Hello world..."

truncate("<p>Hello <b>world</b></p>", 10, { stripHtml: true })
// => "Hello..."

truncate("👨‍👩‍👧 family", 12)
// Grapheme-safe: does not split the ZWJ family emoji

truncate("日本語の文章です", 10, { locale: "ja" })
// Word-aware when a BCP-47 locale is set

const t = createTruncator({ suffix: "…", locale: "th" })
t("สวัสดีครับ", 8)

truncateAll(["one", "two three"], 6)
// => ["one", "two..."]

API

truncate(str, maxLength, options?)

  • str: string | null | undefinednull/undefined are treated as empty.
  • maxLength: Non-negative integer length budget for the entire returned string (content + suffix). When emojiSafe is true (default), length is measured in grapheme clusters (via Intl.Segmenter when available).
  • options: See below.

Returns the truncated string. If the input is only whitespace (after optional HTML strip), returns "". If maxLength is less than or equal to the suffix length (in the same units as above), returns the suffix only (never throws).

createTruncator(defaults)

Returns (str, maxLength) => string with fixed default options.

truncateAll(strs, maxLength, options?)

Maps truncate over an array.

stripHtml(str)

Removes <...> tags with a small regex — not a full HTML parser.

Options

| Option | Type | Default | Description | | --------------- | ---------- | ------- | ------------------------------------------------------------------------------- | | suffix | string | "..." | Appended after truncated content | | wordBoundary | boolean | true | Prefer breaking at whitespace; with locale, uses word segments when supported | | stripHtml | boolean | false | Strip tags before measuring/truncating | | emojiSafe | boolean | true | Grapheme-aware counting and slicing | | locale | string | — | BCP-47 tag (e.g. "th", "ja") for Intl.Segmenter | | preserveWords | string[] | — | Substrings that must not be cut in the middle |

Word boundaries

Without locale, after fitting the content budget the result backs up to the last Unicode whitespace only if the cut would otherwise separate two word characters (letters or numbers). Newlines and tabs count as whitespace.

With locale and wordBoundary, word breaks use Intl.Segmenter with granularity: "word" when available; otherwise behavior falls back to grapheme slicing plus the whitespace rule above.

Runtime

Intl.Segmenter is used when present (Node 18+). Older runtimes fall back to code-point iteration from the string iterator.

Scripts

pnpm build        # tsup → dist (ESM + CJS + .d.ts)
pnpm test         # vitest run
pnpm typecheck    # tsc --noEmit
pnpm lint         # eslint src

License

MIT