Aerostack
edit

text-truncate-smart Edge Function — Content

Content

Truncates text at character, word, or sentence boundaries. Appends a configurable suffix and never cuts mid-word or mid-sentence when using smart modes.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function text-truncate-smart Truncates text at character, word, or sentence boundaries. Appends a configurable suffix and never cuts mid-word or mid-sentence when using smart modes.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/text-truncate-smart
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "text-truncate-smart",
  args: {
    "text": "example_text",
    "maxLength": 42,
    "mode": "word",
    "suffix": "..."
  }
})

text-truncate-smart — Smart Text Truncation

Truncates text at character, word, or sentence boundaries with a configurable suffix. Never cuts mid-word or mid-sentence when using smart modes.


API

POST /api/text-truncate-smart

Request body

Field Type Required Default Description
text string The text to truncate
maxLength number Maximum character length of the output (including suffix)
mode "word" | "sentence" | "char" "word" Truncation strategy
suffix string "..." String appended when truncation occurs

Modes:

  • "char" — truncate at the exact character limit
  • "word" — truncate at the last complete word before maxLength
  • "sentence" — truncate at the last complete sentence (ending with . ! ?) before maxLength

Success response (200)

{
  "success": true,
  "data": {
    "text": "The quick brown fox...",
    "truncated": true,
    "originalLength": 43
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing required field or invalid maxLength
INTERNAL_ERROR 500 Unexpected processing error

Usage

cURL
curl -X POST "$FUNCTION_URL/api/text-truncate-smart" \
  -H "Content-Type: application/json" \
  -d '{"text": "The quick brown fox jumps over the lazy dog", "maxLength": 20, "mode": "word"}'
TypeScript / JavaScript (HTTP)
const response = await fetch(`${FUNCTION_URL}/api/text-truncate-smart`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    text: 'First sentence. Second sentence. Third one.',
    maxLength: 30,
    mode: 'sentence',
  }),
});
const { data } = await response.json();
console.log(data.text);       // "First sentence..."
console.log(data.truncated);  // true
Direct import (Node / Bun / Deno)
import { truncateSmart } from '@aerostack/functions/text-truncate-smart';

const result = truncateSmart({
  text: 'The quick brown fox jumps over the lazy dog',
  maxLength: 20,
  mode: 'word',
  suffix: '…',
});
console.log(result.text);       // "The quick brown fox…"
console.log(result.truncated);  // true
console.log(result.originalLength); // 43

Use Cases

  • Card previews: Truncate blog post descriptions to fit card UI components without cutting mid-word.
  • Search result snippets: Show a clean sentence-boundary excerpt from a long document in search results.
  • Notification messages: Shorten push notification text to fit within platform character limits.
  • Meta descriptions: Generate SEO meta description content that stays within the 160-character limit.

Notes

  • The suffix counts toward maxLength. A 3-character suffix with maxLength: 20 leaves 17 characters for text content.
  • If the text already fits within maxLength, it is returned unchanged with truncated: false.
  • sentence mode falls back to word mode when no sentence boundary is found within the target length.
  • When suffix is longer than maxLength, the text is truncated to maxLength characters with no suffix applied.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

text
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More Content Functions

Browse Content Functions →

Frequently asked questions

What does the text-truncate-smart function do? +

text-truncate-smart is a serverless edge function for content automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the text-truncate-smart function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/text-truncate-smart ``` It will be live on Cloudflare Workers in seconds.

What runtime does text-truncate-smart use? +

text-truncate-smart runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the text-truncate-smart function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.