-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add support for outdenting string arguments. #8
Conversation
Thanks for the PR! I want outdent to intentionally avoid touching the contents of interpolated values. So it'll remove indentation from the template literal, as it statically appears in source code, but never touch the contents of a value (even if that value is a string with newlines and whitespace). Buy that doesn't affect your use case; you're not even invoking outdent as a template tag; you just want to give it a string. I'm also paranoid about overloading a single function with too many signatures. We already have two: one accepting a template literal array, the other accepting an options object. I can't think of a way this would break in the future, but you never know... How about having Sorry if this response is ignoring details you've already addressed in your PR; I'm not at my computer but I'll read the code later. |
I felt the same way but wanted to err on the side of convention here.
My original idea was along these lines. I'll happily update. The only thing would be choosing a good name. I'm ambivalent about
How would you feel about a named export, but called something like |
Sounds good, I like outdentString. And yeah, I agree, we should also do it
as a named export. Doing it as a method too allows piggybacking on any
options the user has configured. Like stripLeadingNewline. But I guess we
could also support the option-bag signature for outdentString the same as
with outdent. So a user would ` import {outdentString}` and then
`outdentString({/*options here */})('string to be outdented');`
…On Mar 11, 2018 9:31 PM, "Trey Shugart" ***@***.***> wrote:
I'm also paranoid about overloading a single function with too many
signatures.
I felt the same way but wanted to err on the side of convention here.
How about having outdent.string(stringValue)? It'll do exactly what you
want: strip indentation from a string.
My original idea was along these lines. I'll happily update. The only
thing would be choosing a good name. I'm ambivalent about string for a
couple reasons:
1. It feels like a separate export.
2. string is reserved in typed langs like TS.
How would you feel about a named export, but called something like
outdentString? If you're pretty set on outdent.string, then I'm totes
fine with that, but thought it was worth bikeshedding since it'll be the
public API :)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#8 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAW-uPJWVYNU0Yh5G1loWiUXturzSt9wks5tdc_ggaJpZM4Sl8n2>
.
|
Re options, that's a fairly compelling reason, so your idea of Thanks for the feedback! |
Sounds good, looking forward to it. Thank you for pointing out the CI failures; I pushed a fix to master. |
src/index.ts
Outdated
@@ -133,6 +133,10 @@ function createInstance(options: Options): Outdent { | |||
} | |||
} | |||
|
|||
(outdent as any).string = (str: string): string => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will currently be breaking the build. I have absolutely no idea how to type this in TS. Halp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm use Flow a bit, so my TS knowledge is limited. Basically, what I want to express is something like this: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5sxcAnAUwEN9KAKa8gcwC4wAjOOGG7ASnYBnfOULZmYAD5gAKpRFgA3qjBhCUMIxb9lqsAEgq+TOVIBycwG59AX324SiuooC8W3a4B8YSzbUu+AB0ALaU+AAWcAAmYO70nj5++sampIE29qj4AJ4ADpRgAGKk8YlgImIS2fmF8m7FpABkemphkTHsJai2NkA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, I'll make a commit to fix the types.
type Fn = () => string;
type Test = Fn & {
method: Fn
};
That actually works in TypeScript; it was just balking at augmenting a function without first declaring the extended interface. (outdent as Outdent).string =
does the trick.
One of these days I'll learn some Flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that would explain my confusion then. So close!
@treshugart I pushed some commits, and now all tests are passing. What do you think? If everything looks good to you I'll publish a new version. |
Yeah, LGTM and thanks again :) |
@treshugart I just published 0.5.0 to npm. Let me know if you encounter any issues. |
Thanks @cspotcode, will do! |
I'm not sure what your stance is on this but I thought I'd raise a PR just in case, as opposed to just raising an issue.
Use case
My use case for this is that we're using
outdent
to outdent code blocks for ourCode
component that highlights the inputtext
:We could use it as part of that template literal, but what we want is for outdenting to automatically happen on the template strings in the component. In order to do this, we need to be able to format strings. Our current solution is to trick
outdent
by providing it an array with theraw
property like so:However, it feels like providing this behaviour as part of
outdent
itself is a far better idea.Testing
I've tested locally using
npm test
and everything passes. However, we're getting the following error in CI:Are you aware of this? If not, I can investigate and fix.