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

@azghr/shorn

v0.1.3

Published

Truncate strings by byte budget without ever breaking a grapheme — safe for DB columns, headers, and anything with a byte limit.

Downloads

27

Readme

shorn

npm MIT License

Truncate strings by byte budget without ever breaking a grapheme cluster. Safe for database columns, HTTP headers, and anything with a hard byte limit.

The problem

A MySQL VARCHAR(255) utf8mb4 column holds up to 255 UTF-8 bytes. If you str.slice(0, 255) a user bio, you can land in the middle of a 4-byte emoji or a ZWJ family sequence. Encoding the sliced string back to UTF-8 and decoding it produces U+FFFD (replacement character) — the emoji is destroyed.

The same failure mode hits HTTP headers, MongoDB indexed fields, and any system with a byte limit on encoded text. The fix is to never split a grapheme cluster, which is what shorn guarantees.

Install

npm install @azghr/shorn
# or
pnpm add @azghr/shorn
# or
yarn add @azghr/shorn

Use

Truncate a string so it fits in 64 UTF-8 bytes. If it already fits, the original string reference is returned unchanged (zero alloc).

import { shorn } from "@azghr/shorn";

const bio = "Hi! I love 🎉 and 👨‍👩‍👧‍👦";
const safe = shorn(bio, 64);
// safe is 42 bytes, contains whole graphemes only

Truncate with an ellipsis counted inside the budget:

const longBio = "Hi! I love 🎉 and 👨‍👩‍👧‍👦 and also 🌍 café 🇩🇪";
const safe = shorn(longBio, 50, { unit: "utf8", ellipsis: "..." });
// safe is at most 50 bytes, ends with "..."

Measure in UTF-16 code units or grapheme clusters instead of bytes:

shorn(bio, 30, { unit: "utf16" });                 // 30 code units
shorn(bio, 10, { unit: "grapheme" });               // 10 grapheme clusters
shorn(bio, 10, { unit: "grapheme", ellipsis: "…" }); // 10 grapheme clusters + ellipsis

API

shorn(input, max, options?)

| Param | Type | Default | Description | |-----------|----------|------------|-------------| | input | string | (required) | String to truncate | | max | number | (required) | Maximum size in the chosen unit. Must be a non-negative integer | | options | ShornOptions | {} | See below |

Returns: string -- the longest prefix of whole grapheme clusters whose measured size does not exceed max, optionally followed by the ellipsis. If the input already fits, returns the same reference (identity fast path).

Throws: TypeError if max < 0, NaN, Infinity, or not an integer.

ShornOptions

interface ShornOptions {
  unit?: "utf8" | "utf16" | "grapheme";  // default "utf8"
  ellipsis?: string;                       // default "" (none)
  locale?: string;                         // passed to Intl.Segmenter
}
  • unit: Measurement mode. "utf8" uses TextEncoder byte length. "utf16" uses string.length (code units). "grapheme" uses Intl.Segmenter with granularity: "grapheme".
  • ellipsis: Appended to the result when truncation occurs. Measured in the same unit and counted inside the budget. If not even one grapheme can fit alongside the ellipsis, the ellipsis is dropped and only the grapheme prefix is returned. If the ellipsis alone exceeds the budget, it is dropped.
  • locale: BCP-47 locale forwarded to Intl.Segmenter. Only affects grapheme segmentation rules (e.g. locale-specific cluster boundaries in Indic scripts). Default undefined (runtime default).

Non-goals

This package will never support:

  • Streaming / chunked truncation
  • Unicode normalization (NFC/NFD/NFKC/NFKD)
  • Custom segmenter rules -- only Intl.Segmenter with "grapheme" granularity
  • Word-boundary or sentence-boundary truncation
  • In-place mutation
  • Non-UTF encodings (GB18030, Shift_JIS, etc.)
  • Surrogate-pair healing (grapheme segmentation already handles this)
  • Configurable normalization of the ellipsis
  • Terminal column width measurement
  • Padding

Composition example: If you need word-boundary truncation, pair shorn with a word splitter:

import { shorn } from "@azghr/shorn";

function truncateWords(text: string, maxBytes: number): string {
  const words = text.split(" ");
  let candidate = "";
  for (const word of words) {
    const next = candidate ? candidate + " " + word : word;
    if (shorn(next, maxBytes).length !== next.length) break;
    candidate = next;
  }
  return candidate;
}

Related Packages

Caching & Concurrency:

Text Processing:

  • seriatim — Sequential processing utilities

HTTP & Network:

  • forbear — Read server rate-limit instructions from HTTP responses
  • forestall — Delay execution until a condition is met
  • obviate — Render operations unnecessary through caching

System & Process:

  • quiesce — Ordered, timeboxed graceful shutdown for Node
  • sortition — Deterministic percentage rollouts and A/B bucketing
  • stanch — Stop flows or operations based on conditions

Utilities:

  • expunge — Remove or exclude items from collections
  • occlude — Hide or mask data and functionality
  • placemark — Geographic location and mapping utilities
  • specie — Currency and financial calculations

License

MIT