https://github.com/hejny/spacetrim
Spacetrim is trimming string from all 4 sides. It is very helpful to keep pretty code indentation without keeping a strange spaces inside a strings.
https://github.com/hejny/spacetrim
javascript library nodejs typescript
Last synced: about 1 year ago
JSON representation
Spacetrim is trimming string from all 4 sides. It is very helpful to keep pretty code indentation without keeping a strange spaces inside a strings.
- Host: GitHub
- URL: https://github.com/hejny/spacetrim
- Owner: hejny
- License: apache-2.0
- Created: 2021-10-20T20:17:14.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-26T20:15:31.000Z (almost 2 years ago)
- Last Synced: 2024-08-03T14:51:45.423Z (almost 2 years ago)
- Topics: javascript, library, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 82.9 MB
- Stars: 10
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ✂️ Space trim
[](https://www.npmjs.com/package/spacetrim)
[](https://packagequality.com/#?package=spacetrim)
[](https://github.com/hejny/spacetrim/actions/workflows/lint.yml)
[](https://github.com/hejny/spacetrim/actions/workflows/test.yml)
[](https://snyk.io/test/github/hejny/spacetrim)
[](https://github.com/hejny/spacetrim/issues)
[](https://socket.dev/npm/package/spacetrim)
Spacetrim is trimming string from all 4 sides.
It is very helpful to keep pretty code indentation without keeping strange spaces inside a string.
> ░ is whitespace and `spaceTrim` will trim the string at the boundaries ╔═╗
```
░░░░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░╔═════╗░
░░░░░░░║Hello║░░
░░░░░░░║Space║░░░
░░░░░░░║Trim ║░░░░
░░░░░░░╚═════╝░░░░░
░░░░░░░░░░░░░░░░░░░░
```
## Usage
```bash
npm i spacetrim
```
```typescript
import spaceTrim from 'spacetrim';
const trimmed = spaceTrim(`
Hello
Space
Trim
`);
console.log(trimmed);
/*
Hello
Space
Trim
*/
```
_See more examples in [simple tests](./src/spaceTrim.test.ts)._
## Nesting
This is very usefull when you want to trim multiline strings inside multiline strings.
```typescript
import { spaceTrim } from 'spacetrim';
const trimmed = spaceTrim(
(block) => `
Numbers
${block(['1', '2', '3'].join('\n'))}
Chars
${block(['A', 'B', 'C'].join('\n'))}
`,
);
console.log(trimmed);
/*
Numbers
1
2
3
Chars
A
B
C
*/
```
_See more examples in [nesting tests](./src/nesting/nesting.test.ts)._
## Asynchronous nesting
You can also trim multiline strings which are fetched asynchronously inside multiline strings.
```typescript
import { spaceTrim } from 'spacetrim';
const trimmed = await spaceTrim(
async (block) => `
TypeScript:
${await fetch('https://en.wikipedia.org/wiki/TypeScript').then(
(result) => result.text(),
)}
`,
);
console.log(trimmed);
/*
TypeScript:
TypeScript is a free and open source programming language...
*/
```
_See more examples in [asynchronous nesting tests](./src/nesting/async-nesting.test.ts)._
## 🖋️ Contributing
I am open to pull requests, feedback, and suggestions. Or if you like this utility, you can [☕ buy me a coffee](https://www.buymeacoffee.com/hejny) or [donate via cryptocurrencies](https://github.com/hejny/hejny/blob/main/documents/crypto.md).
You can also ⭐ star the spacetrim package, [follow me on GitHub](https://github.com/hejny) or [various other social networks](https://www.pavolhejny.com/contact/).
## ✨ Partners
[Become a partner](https://www.pavolhejny.com/contact/)


