npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string-slurp

v2.1.0

Published

Trim spaces in multiline strings

Downloads

14

Readme

Readme

Imagine doing this... (Whitespaces have been indicated by dots '.')

....const multiline_text.=.`
........Hello world!
........How much is the fish?
....`

If you write the value of multiline_text into a file, you'll notice some unwanted newline (\n) and tab (\t) characters in front of each line! The build-in String.trim()method of JavaScript wouldn't help here, because it only removes leading and trailing spaces around the entire string but not the unwanted spaces before every line.

This package flattens multiline strings in more predictable ways.

Basically it takes a multiline string, splits it into single lines, then loops through each line, finding the common line indentation and removing it (also removing trailing line spaces). Finally it joins the lines back into a single multiline string.

Install

Run npm i string-slurp@latest, then import the package:

// Both import variants are equal!
import strim from "string-slurp"
import {strim} from "string-slurp"

Use

The package exports a single function strim(text, oneliner = false) where text argument must be a 'string'.

The optional oneliner = true setting will create a multiline shell command (by removing nested linebreaks and appending '' to each line ending).

Here's an example of a larger compilation:

strim(`

    # Application can not operate on files outside its ownership or realm.
    # If Certbot saves certificates at its default --config-dir (/etc/letsencrypt) then copy and give them appropriate file execution permissions.

    OG_KEY="${join("/etc/letsencrypt/live", HOST_ALIAS, basename(SSL_CREDENTIALS.letsencrypt.key))}"
    OG_CERT="${join("/etc/letsencrypt/live", HOST_ALIAS, basename(SSL_CREDENTIALS.letsencrypt.cert))}"
    KEY="${SSL_CREDENTIALS.letsencrypt.key}"
    CERT="${SSL_CREDENTIALS.letsencrypt.cert}"

    if [ -e $OG_KEY ] && [ -e $OG_CERT ]; then
        cp $OG_KEY $KEY
        cp $OG_CERT $CERT
        chmod ${FILE_PERMISSIONS.toString(8)} $KEY
        chmod ${FILE_PERMISSIONS.toString(8)} $CERT
    fi

`)

The above example would compile into something like this: (See how paragraphs have no leading tabs and were joined with an empty line in between them?)

# Application can not operate on files outside its ownership or realm.
# If Certbot saves certificates at its default --config-dir (/etc/letsencrypt) then copy and give them appropriate file execution permissions.

OG_KEY="${join("/etc/letsencrypt/live", HOST_ALIAS, basename(SSL_CREDENTIALS.letsencrypt.key))}"
OG_CERT="${join("/etc/letsencrypt/live", HOST_ALIAS, basename(SSL_CREDENTIALS.letsencrypt.cert))}"
KEY="${SSL_CREDENTIALS.letsencrypt.key}"
CERT="${SSL_CREDENTIALS.letsencrypt.cert}"

if [ -e $OG_KEY ] && [ -e $OG_CERT ]; then
        cp $OG_KEY $KEY
        cp $OG_CERT $CERT
        chmod ${FILE_PERMISSIONS.toString(8)} $KEY
        chmod ${FILE_PERMISSIONS.toString(8)} $CERT
fi

Here's an example with option oneliner = true: (This becomes handy when you generate scripts that are one-liners but for you want readability for the generator code.)

                openssl
                x509
                -inform PEM
                -in "cert.pem"
                -outform DER
                -out "cert.der"
openssl \
x509 \
-inform PEM \
-in "cert.pem" \
-outform DER \
-out "cert.der"