Skip to content

kurrik/cleanstring-ts

Repository files navigation

cleanstring-ts

npm version

Clean multiline TypeScript string literals by removing leading/trailing blank lines and stripping pipe prefixes.

Installation

npm install cleanstring-ts

Usage

import cleanstring from 'cleanstring-ts';

const result = cleanstring(`
    |Any literal
    |which needs to be split
    |on multiple lines for readability.
`);
// Result: "Any literal\nwhich needs to be split\non multiple lines for readability."

// Custom prefix example
const markdown = cleanstring(`
    > This is a markdown quote
    > with multiple lines
`, { prefix: '> ' });
// Result: "This is a markdown quote\nwith multiple lines"

CommonJS

const cleanstring = require('cleanstring-ts');

How it works

The cleanstring() function processes multiline strings by:

  1. Removing leading blank lines - Any whitespace-only lines at the start are stripped
  2. Stripping prefixes - Lines with whitespace followed by a prefix character (default |) have the prefix removed, preserving all content after the prefix
  3. Removing trailing blank lines - Any whitespace-only lines at the end are stripped
  4. Preserving internal structure - Whitespace lines between content are maintained

Important: Content after the prefix is preserved exactly as-is. If you want to strip a space after the prefix, include the space as part of the prefix (e.g., { prefix: '> ' } instead of { prefix: '>' }).

Examples

Basic usage with pipe prefix (default)

import cleanstring from 'cleanstring-ts';

const sql = cleanstring(`
    |SELECT *
    |FROM users
    |WHERE active = true
`);
// Result: "SELECT *\nFROM users\nWHERE active = true"

Custom prefix characters

// Markdown quotes with > prefix
const quote = cleanstring(`
    >This is a quote
    >from someone famous
`, { prefix: '>' });
// Result: "This is a quote\nfrom someone famous"

Multi-character prefixes for space stripping

// Using "> " (greater than + space) strips the space after >
const cleanQuote = cleanstring(`
    > This is a quote
    > with multiple lines
`, { prefix: '> ' });
// Result: "This is a quote\nwith multiple lines"

Mixed content with prefix

const script = cleanstring(`
    |#!/bin/bash
    |
    |echo "Hello World"
    |exit 0
`);
// Result: "#!/bin/bash\n\necho \"Hello World\"\nexit 0"

Without prefixes

Note that leading/trailing blank lines are still stripped.

const text = cleanstring(`

    This is a multiline string
    with some content

`);
// Result: "    This is a multiline string\n    with some content"

Development

Instructions for working on the library...

Common commands

# Run tests
npm test

# Run linting and formatting checks
npm run ci

# Build the project
npm run build

# Format code
npm run format

Releases

Releases are automated via GitHub Actions. Only the repository owner can create releases.

Prerequisites (one-time setup)

  1. Create Deploy Key with Write Access:

    • Generate SSH key pair: ssh-keygen -t ed25519 -f release_key -N ""
    • Go to repository → Settings → Deploy keys
    • Click "Add deploy key"
    • Title: "Release Automation"
    • Key: Contents of release_key.pub
    • ✅ Check "Allow write access"
    • Configure to bypass repository rules (Settings → Rules)
  2. Add Repository Secrets:

    • Go to repository → Settings → Secrets and variables → Actions
    • Add DEPLOY_KEY with contents of private key file (release_key)
    • Add NPM_TOKEN with your npm automation token

Creating a Release

  1. Go to your repository on GitHub
  2. Navigate to Actions tab
  3. Click on "Release" workflow in the left sidebar
  4. Click "Run workflow" button
  5. Select the version bump type from the dropdown:
    • patch: 1.0.0 → 1.0.1 (bug fixes)
    • minor: 1.0.0 → 1.1.0 (new features)
    • major: 1.0.0 → 2.0.0 (breaking changes)
  6. Click "Run workflow" to start the release process

The automated release process:

  1. Runs all CI checks (lint, test, build)
  2. Updates package.json version
  3. Commits and pushes to main (bypasses branch protection)
  4. Creates and pushes git tag
  5. Creates GitHub release
  6. Automatically publishes to npm (triggered by release creation)

License

MIT License - see LICENSE file for details.

About

Clean multiline Typescript string literals

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •