cleanstring-ts
v1.1.7
Published
Clean multiline TypeScript string literals
Downloads
3,616
Maintainers
Readme
cleanstring-ts
Clean multiline TypeScript string literals by removing leading/trailing blank lines and stripping pipe prefixes.
Installation
npm install cleanstring-tsUsage
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"
// Custom prefix with trailing space example (note the prefix contains a space)
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:
- Removing leading blank lines - Any whitespace-only lines at the start are stripped
- Stripping prefixes - Lines with whitespace followed by a prefix character (default
|) have the prefix removed, preserving all content after the prefix - Removing trailing blank lines - Any whitespace-only lines at the end are stripped
- 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\n from someone famous"
// Shell comments with # prefix
const script = cleanstring(
`
# This is a shell script
# with multiple comment lines
`,
{ prefix: '#' },
);
// Result: " This is a shell script\n with multiple comment lines"
// List items with * prefix
const list = cleanstring(
`
* First item
* Second item
* Third item
`,
{ prefix: '*' },
);
// Result: " First item\n Second item\n Third item"Multi-character prefixes for space stripping
If you want to strip a space after the prefix (reproducing the old behavior), include the space as part of the prefix:
// 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"
// Using "| " (pipe + space) strips the space after |
const cleanCode = cleanstring(
`
| function example() {
| return 'hello world';
| }
`,
{ prefix: '| ' },
);
// Result: "function example() {\n return 'hello world';\n}"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
const text = cleanstring(`
This is a multiline string
with some content
`);
// Result: " This is a multiline string\n with some content"Development
Common commands
# Run tests
npm test
# Run linting and formatting checks
npm run ci
# Build the project
npm run build
# Format code
npm run formatReleases
Releases are automated via GitHub Actions. Only the repository owner can create releases.
Prerequisites (one-time setup)
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)
- Generate SSH key pair:
Add Repository Secrets:
- Go to repository → Settings → Secrets and variables → Actions
- Add
DEPLOY_KEYwith contents of private key file (release_key) - Add
NPM_TOKENwith your npm automation token
Creating a Release
- Go to your repository on GitHub
- Navigate to Actions tab
- Click on "Release" workflow in the left sidebar
- Click "Run workflow" button
- 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)
- Click "Run workflow" to start the release process
The automated release process:
- Runs all CI checks (lint, test, build)
- Updates package.json version
- Commits and pushes to main (bypasses branch protection)
- Creates and pushes git tag
- Creates GitHub release
- Automatically publishes to npm (triggered by release creation)
License
MIT License - see LICENSE file for details.
