Comments (16)
I needed exactly the same today, so #13 was born.
It now adds the indention of the line you interpolate on to every line in the interpolated string.
What would your preferred behaviour for the last example be?
In my code now, that would result (when you have 3 items in the things
array) in this
foo.
hello
world
hello
world
hello
world
bar.
from dedent.
@bertho-zero You are right. This is not because of the multiple indentation though. It is a bug in endent that makes it not work well with string that start without indentation at all.
This one (that clearly has one line indented and the other not indented) will output a totally not indented string
https://runkit.com/dral/5b036f8dabe4a00011e3e82e
Once you add a layer of indentation to all your lines, it gives the right output
https://runkit.com/dral/5b037090b225ba00122e66cf
@ZhouHansen This is a bug in endent - ofcourse you want to first apply endent to the substring.. not sure why you made your "right usage" so wrong ;)
I see you made me collaborator on that repo though, so I'll give fixing it a shot tomorrow :)
from dedent.
This is fixed with ts-dedent
: https://runkit.com/embed/di4ek5toqg7o
from dedent.
Awesome! Will discuss expected behavior in the PR
from dedent.
indentjs/endent endows suitable indentation for multiline interpolation.
from dedent.
@ZhouHansen This issue exist also with endent: https://runkit.com/embed/fx2ssy6zqm9x
from dedent.
@bertho-zero , here's the right usage: https://runkit.com/zhouhansen/5b023b1fd78bc100121e15f8
from dedent.
This is not a neested endent.
from dedent.
@dralletje that will be very helpful!
from dedent.
@dralletje you didn't compile your fork, so it still has the same dist code.
from dedent.
@d07RiV Good find, updated it :)
from dedent.
Thank you @ZhouHansen , I confirm that the endent package has the behavior I want β I have replaced all uses of dedent
with it.
By the way, it looks like endent
now has over 2.5mm downloads/wk, congrats!
from dedent.
Hmm, I can see why you'd want this behavior... but also it's a bit counter-intuitive to how the package is positioned right now IMO. Today, it's used as a "dedent everything in this string" utility - not a "dedent this string, and still allow indents in some contents".
Perhaps this can become an opt-in? A couple of API starting ideas..
dedent.recursive`
${text}
`;
dedent({ recursive: true })`
${text}
`;
Thoughts, everyone?
from dedent.
Perhaps this can become an opt-in? A couple of API starting ideas..
...
Thoughts, everyone?
@JoshuaKGoldberg thanks for pointing me here! To provide some feedback on this - I use this package a lot (I write a lot of cli tooling in particular that spits out formatted error messages to users) and 10/10 times this is the default behavior i'm looking for. That said, I understand and totally respect it's a question of scope and not wanting to fundamentally change (in a backwards incompatible way) the behaviour of this significantly depended-on library!
With that in mind, I feel like it's worthy enough of being a top level namespace thing (rather than buried as an option), and so I like what you initially suggested the most:
dedent.recursive`...`
fwiw in terms of API ideas, here's some prior art for a similar thing in execa: https://github.com/sindresorhus/execa#shared-options - so you'd suggest something like:
import dedent from 'dedent';
const ddedent = dedent({ recursive: true });
console.log(ddedent`...`)
I don't personally love this, the $$
example is neat enough but ddedent
or anything else i can think of just looks weird and non obvious. but maybe if you could come up with a catchy alias it could be nice.
3rd option (which could go alongside as an alias of the first option)
import { recursive as dedent } from 'dedent';
console.log(dedent`...`);
from dedent.
The dedent.recursive
option is definitely appealing. A similar idea came up with escapeSpecialCharacters
too, like dedent.escapeSpecialCharacters
. The one issue is that we'd then have to content with a whole bunch of potential options there. Would dedent.escapeSpecialCharacters.recursive
need to work too?
For now, let's add it in as an opt-in option: dedent({ recursive: true })
.
from dedent.
Thoughts, everyone?
Heh, hey @JoshuaKGoldberg ! Good to see you here! Sorry I missed this before.
FWIW, it's not my expected behavior to have the library dedent most things, except child strings. I consider the current behavior a bug, not a separate feature, though I may be missing something.
I wouldn't want to have to use an option for a library like this βΒ more specifically, I wouldn't want to have to police that everyone in my codebase always calls the recursive version.
I could maybe be okay with import dedent from 'dedent/recursive'
, but really I'd much rather this just be the default behavior (though I'll admit it may require a major version bump).
For now, personally, I'd probably simply stick with ts-dedent
which has this behavior out of the box (as well as TS types) if a recursive
option were added.
Just my 2c!
from dedent.
Related Issues (20)
- CommonJS breakage: many consumers still use dedent = require("dedent")
- Should only trim one newline at start and end so that you can add custom newlines HOT 5
- 1.0.1 type change HOT 1
- escaped `$` and `{` characters are not handled correctly HOT 3
- `babel-plugin-macros` should be an optional peer dependency HOT 1
- Add a changelog HOT 5
- Convert dedent.ts and tests to TypeScript
- Convert macro.js to TypeScript
- Onboard to create-typescript-app@1 HOT 1
- Node ESM compatibility issue HOT 2
- π Feature: Add an option to disable special character escaping HOT 2
- Avoid escaping $ without a { after it? HOT 1
- dedent.d.mts missing in package.json > files
- Consider being `String.dedent` HOT 3
- Bug(?) JSON.serialize(..., null, 2) throws off indentation HOT 2
- error with npm install on firebase project HOT 1
- π Tooling: Set up tokens for automated publishing & contributor recognition
- π Bug: Doesn't do dedent if first character is a space character HOT 2
- π Docs: add an 'install' section to the README? HOT 5
- π Tooling: Build failures on main from references to yarn HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dedent.