Socket
Book a DemoInstallSign in
Socket

antiemdash

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

antiemdash

Remove or replace em dashes (—) in strings with a simple boolean parameter

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
69
Maintainers
1
Weekly downloads
 
Created
Source

antiemdash

A TypeScript library scaffolded by init-npm-pkg

A simple TypeScript utility to remove or replace em dashes (—) in strings, with built-in support for wrapping Vercel AI SDK functions.

— -> -

Installation

npm install antiemdash

Basic Usage

The core antiemdash function removes or replaces em dashes in strings:

import { antiemdash } from 'antiemdash';

// Remove em dashes (default behavior)
console.log(antiemdash('Hello—world')); // 'Helloworld'
console.log(antiemdash('Test—string—with—multiple—dashes')); // 'Teststringwithmultipledashes'

// Replace em dashes with regular dashes
console.log(antiemdash('Hello—world', true)); // 'Hello-world'
console.log(antiemdash('Test—string—with—multiple—dashes', true)); // 'Test-string-with-multiple-dashes'

// Handle edge cases
console.log(antiemdash('')); // ''
console.log(antiemdash('—')); // ''
console.log(antiemdash('—', true)); // '-'
console.log(antiemdash('No em dashes here')); // 'No em dashes here'

Vercel AI SDK Integration

Wrapping AI Functions

Use withAntiemdash to automatically process em dashes in AI function outputs:

import { withAntiemdash } from 'antiemdash';

// Example: Wrapping a custom AI function
const myAIFunction = async (prompt: string) => {
  // Simulate AI response with em dashes
  return `Here's your response—with some em dashes—for: ${prompt}`;
};

const cleanAI = withAntiemdash(myAIFunction);
const result = await cleanAI('test prompt');
console.log(result); // 'Here's your responsewith some em dashesfor: test prompt'

// With replacement instead of removal
const cleanAIWithDashes = withAntiemdash(myAIFunction, true);
const result2 = await cleanAIWithDashes('test prompt');
console.log(result2); // 'Here's your response-with some em dashes-for: test prompt'

Processing Complex AI Responses

The wrapper handles nested objects and arrays automatically:

import { withAntiemdash } from 'antiemdash';

const complexAI = async () => ({
  text: 'Main response—with dashes',
  messages: [
    { content: 'First—message' },
    { content: 'Second—message' }
  ],
  metadata: {
    summary: 'Summary with—dashes',
    tags: ['tag1', 'tag—with—dash']
  }
});

const cleanComplexAI = withAntiemdash(complexAI);
const result = await cleanComplexAI();

console.log(result);
// {
//   text: 'Main responsewith dashes',
//   messages: [
//     { content: 'Firstmessage' },
//     { content: 'Secondmessage' }
//   ],
//   metadata: {
//     summary: 'Summary withdashes',
//     tags: ['tag1', 'tagwithdash']
//   }
// }

Vercel AI SDK Specific Wrappers

For generateText Functions

import { createTextGenerator } from 'antiemdash';
import { generateText } from 'ai';

// Create a wrapper for generateText
const textWrapper = createTextGenerator();
const cleanGenerateText = textWrapper(generateText);

// Use it like the original generateText
const result = await cleanGenerateText({
  model: 'gpt-4',
  prompt: 'Write a sentence with em dashes'
});
// Result will have em dashes automatically removed

For streamText Functions

import { createStreamGenerator } from 'antiemdash';
import { streamText } from 'ai';

// Create a wrapper for streamText
const streamWrapper = createStreamGenerator();
const cleanStreamText = streamWrapper(streamText);

// Use it like the original streamText
const result = await cleanStreamText({
  model: 'gpt-4',
  prompt: 'Write a story with em dashes'
});
// Streamed chunks will have em dashes automatically processed

Real-World Example with Vercel AI SDK

import { createTextGenerator, createStreamGenerator } from 'antiemdash';
import { generateText, streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Set up clean versions of AI functions
const cleanGenerateText = createTextGenerator()(generateText);
const cleanStreamText = createStreamGenerator()(streamText);

// Use in your API route or server function
export async function POST(req: Request) {
  const { prompt } = await req.json();

  // Generate clean text without em dashes
  const result = await cleanGenerateText({
    model: openai('gpt-4'),
    prompt: prompt,
    maxTokens: 1000,
  });

  return Response.json({ text: result.text });
}

// Or for streaming
export async function POST(req: Request) {
  const { prompt } = await req.json();

  const result = await cleanStreamText({
    model: openai('gpt-4'),
    prompt: prompt,
    maxTokens: 1000,
  });

  return result.toDataStreamResponse();
}

Advanced Usage: Custom Processing Options

import { withAntiemdash } from 'antiemdash';

// Create different processing configurations
const removeDashes = withAntiemdash(myAIFunction, false);
const replaceDashes = withAntiemdash(myAIFunction, true);

// Use them for different use cases
const summary = await removeDashes('Summarize this text');
const formatted = await replaceDashes('Format this text');

API Reference

antiemdash(str: string, replaceWithDash?: boolean): string

  • str (required): The input string containing em dashes
  • replaceWithDash (optional): If true, replaces em dashes with regular dashes (-). If false or omitted, removes em dashes entirely.
  • Returns: The processed string

withAntiemdash<T extends AIFunction>(aiFunction: T, replaceWithDash?: boolean): T

  • aiFunction (required): Any async function that returns a string or object with string properties
  • replaceWithDash (optional): Whether to replace em dashes with regular dashes (default: false)
  • Returns: A wrapped function that automatically processes em dashes in the output

createTextGenerator(replaceWithDash?: boolean)

  • replaceWithDash (optional): Whether to replace em dashes with regular dashes (default: false)
  • Returns: A function that wraps Vercel AI SDK's generateText function

createStreamGenerator(replaceWithDash?: boolean)

  • replaceWithDash (optional): Whether to replace em dashes with regular dashes (default: false)
  • Returns: A function that wraps Vercel AI SDK's streamText function

Development

npm install
npm run build
npm test

Keywords

typescript

FAQs

Package last updated on 24 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.