Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
A string template tag to strip leading indents from your heredoc-style multi-line strings
A little tag function that strips leading indent from multi-line ES string templates. It also strips the first and last lines if they're just whitespace.
$ npm install theredoc
const theredoc = require('theredoc')
console.log(theredoc`
I want to write multipline lines
but don't want to mess up my indenting.
Ok?
`)
Will output:
I want to write multipline lines
but don't want to mess up my indenting.
Ok?
Because good messages are often long messages, but out-of-the-box, ES template strings don't format multi-line error and console messages gracefully.
If you're familiar with """
-style heredocs from CoffeeScript or the
<<~
-style heredoc from Ruby 2.3, you might appreciate being able to write
multi-line strings with an indentation level that matches the surrounding code
listing without indenting the resulting string itself.
For example, if you want to print a multi-line error message without superfluous indentation in the following situation, an unadorned template string would have to be outdented awkwardly:
//...
} catch (e) {
console.error(`
Something bad happened.
Message: ${e.message}
`)
}
//...
Additionally, to format the string neatly at all in the above will also add a extraneous leading and trailing line containing nothing but whitespace.
Instead, theredoc
lets you write this:
//...
} catch (e) {
console.error(theredoc`
Something bad happened.
Message: ${e.message}
`)
}
//...
And the resulting console output will be:
Something bad happened.
Message: LOL errors
With the leading 10 spaces stripped from each line and (since they only contain whitespace) stripping the first and last lines of the template string.
If you still don't think this is nifty, I don't know what to tell you!
The very cool and fancy library
common-tags can accomplish the
same thing with the stripIndent
function it exports, however there are a few
downsides:
babel-runtime
, which drastically increases
the size of its
install and, because
babel-runtime depends on core-js
, sets a global
object that might be
more appropriate for an app to set than an intermediate lib\t
)?**Theredoc only deals with space character indentation, sorry!
A pull request to support tabs would be considered if (a) it didn't drastically
increase the module's complexity, and (b) deferred to counting spaces in the
event that the given string had some lines indented with spaces and other with
\t
tabs.
FAQs
A string template tag to strip leading indents from your heredoc-style multi-line strings
The npm package theredoc receives a total of 88,539 weekly downloads. As such, theredoc popularity was classified as popular.
We found that theredoc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.