Spacetrim is trimming string from all 4 sides.


Keywords
javascript, library, nodejs, typescript
License
Apache-2.0
Install
npm install spacetrim@0.11.60

Documentation

βœ‚οΈ Space trim

NPM Version of Space trim Quality of package Space trim lint test Known Vulnerabilities Issues Socket

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

npm i spacetrim
import spaceTrim from 'spacetrim';

const trimmed = spaceTrim(`

    Hello
    Space
    Trim


`);

console.log(trimmed);

/*
Hello
Space
Trim
*/

See more examples in simple tests.

Nesting

This is very usefull when you want to trim multiline strings inside multiline strings.

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.

Asynchronous nesting

You can also trim multiline strings which are fetched asynchronously inside multiline strings.

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.

πŸ–‹οΈ Contributing

I am open to pull requests, feedback, and suggestions. Or if you like this utility, you can β˜• buy me a coffee or donate via cryptocurrencies.

You can also ⭐ star the spacetrim package, follow me on GitHub or various other social networks.

✨ Partners

Collboard Β Β Β Β Β  WebGPT Β Β Β Β Β  Promptbook

Become a partner