-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: trim option #690
Comments
I also was thinking about something like this at the beginning. But I decided what explicit is better. Maybe an additional method should be introduced. Like const branch = await $`git branch --show-current`;
if (branch.toTrimmedStdoutString() !== "main") {
throw new Error("You must run this script on the main branch only.")
} Or something shorter, a nice name is difficult to find. |
|
What about just trim output inside toString()? |
Yes, that would be optimal, because the code in my first snippet in the OP would just work without having to do anything additional. And then, to get the non-trimmed output, one could access Additionally, right now, |
I like this idea. What do you think @antongolub ? |
We can implement a custom |
As a workaround for now import {ProcessOutput, $} from 'zx'
ProcessOutput.prototype.valueOf = function () {
return this.toString()
}
ProcessOutput.prototype.toString = function () {
const str = this._combined.toString()
return $.trim
? str.trim()
: str
}
// ...
{
const output = await $`echo foobar`
assert(output.toString().trim() === 'foobar')
assert(output == 'foobar')
} |
I like the idea of valueOf(). |
Added to the plan for zx v8(#589). But should toString() also return trimmed output? I think we can make this "breaking change" in v8. |
I'm against adding another configuration option |
Hello, and thanks for the very useful library!
The Problem
Consider the following simple script:
However, this has a subtle pitfall. The output of the
branch
variable is actually going to bemain\n
. So our actual script has to be something like this:Yuck! This adds boilerplate to scripts that we want to keep small and tidy.
Proposed Solution
Currently,
zx
is configurable with some options by modifying the$
object, like e.g.:Thus, I propose a new setting called
trim
(or maybetrimOutput
), that would be set to false by default for backwards compatibility, but could be changed to true like this:Once
trim
is set to true, then the code from the original snippet would work properly without any additional boilerplate.If you think this is a good idea, I can try to do a PR.
The text was updated successfully, but these errors were encountered: