Skip to content
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

Trim only one line at start and end #30

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions __tests__/__snapshots__/dedent-macro-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ dedent\`
"
`;

exports[`explicit newline 1`] = `
"
import dedent from \"../macro\";

dedent\`
<p>
Hello world!
</p>

\`;

↓ ↓ ↓ ↓ ↓ ↓

\"<p>\\n Hello world!\\n</p>\";
"
`;

exports[`expressions 1`] = `
"
import dedent from \"../macro\";
Expand Down
29 changes: 16 additions & 13 deletions __tests__/__snapshots__/dedent-tests.js.snap
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
exports[`dedent can be used as a function 1`] = `"A test argument."`;

exports[`dedent doesn't strip exlicit newlines 1`] = `
"<p>Hello world!</p>
"
`;

exports[`dedent doesn't strip exlicit newlines with mindent 1`] = `
"<p>
Hello world!
</p>
"
`;

exports[`dedent escapes backticks 1`] = `"\`"`;

exports[`dedent single line input works with single line and closing backtick on newline 1`] = `"A single line of input."`;
Expand All @@ -27,18 +15,33 @@ exports[`dedent works with blank first line 1`] = `
That\'s all."
`;

exports[`dedent works with blank last line 1`] = `
"Some text that I might want to indent:
* reasons
* fun
That\'s all."
`;

exports[`dedent works with interpolation 1`] = `
"first line
second
third"
`;

exports[`dedent works with multiple blank first lines 1`] = `
"first
"
first
second
third"
`;

exports[`dedent works with multiple blank last lines 1`] = `
"first
second
third
"
`;

exports[`dedent works with removing same number of spaces 1`] = `
"first
second
Expand Down
12 changes: 0 additions & 12 deletions __tests__/dedent-macro-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ pluginTester({
\`;
`
},
{
title: "explicit newline",
code: `
import dedent from "../macro";

dedent\`
<p>
Hello world!
</p>\n
\`;
`
},
{
title: "multiple indentations",
code: `
Expand Down
34 changes: 20 additions & 14 deletions __tests__/dedent-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ describe("dedent", () => {
).toMatchSnapshot();
});

it("works with blank last line", () => {
expect(dd`
Some text that I might want to indent:
* reasons
* fun
That's all.
`).toMatchSnapshot();
});

it("works with multiple blank last lines", () => {
expect(
dd`
first
second
third

`
).toMatchSnapshot();
});

it("works with removing same number of spaces", () => {
expect(
dd`
Expand Down Expand Up @@ -83,18 +103,4 @@ describe("dedent", () => {
it("escapes backticks", () => {
expect(dd`\``).toMatchSnapshot();
});

it("doesn't strip exlicit newlines", () => {
expect(dd`
<p>Hello world!</p>\n
`).toMatchSnapshot();
});

it("doesn't strip exlicit newlines with mindent", () => {
expect(dd`
<p>
Hello world!
</p>\n
`).toMatchSnapshot();
});
});
9 changes: 4 additions & 5 deletions dedent.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes start and end trimming by only trimming one newline each

@dword-design is there a reason why you'd want this? Dedenting a string doesn't necessarily need to include trimming newlines from the start and/or end. Some users might not want that.

I wonder if this should be an opt-in option? Or just not in the purview of dedent specifically?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mainly added this for trailing newlines in strings written to a file. I'd say probably don't need it anymore but I still think that the user has more possibilities. So he could add more newlines to the start or end but doesn't need to.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok interesting - similar to #29 I think it'd be good to talk in an issue first. I'll close this PR to keep the queue small, but not saying no! Just hoping to discuss :)

Thanks!

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export default function dedent(
result = lines.map(l => l[0] === " " ? l.slice(m) : l).join("\n");
}

return result
// dedent eats leading and trailing whitespace too
.trim()
// handle escaped newlines at the end to ensure they don't get stripped too
.replace(/\\n/g, "\n");
if (result.startsWith('\n')) {
result = result.substr(1);
}
return result.replace(/^((.|\n)*)\n[ \t]*?$/, '$1');
}
16 changes: 7 additions & 9 deletions dist/dedent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ function dedent(strings) {
// first, perform interpolation
var result = "";
for (var i = 0; i < raw.length; i++) {
result += raw[i].
result += raw[i]
// join lines when there is a suppressed newline
replace(/\\\n[ \t]*/g, "").

.replace(/\\\n[ \t]*/g, "")
// handle escaped backticks
replace(/\\`/g, "`");
.replace(/\\`/g, "`");

if (i < (arguments.length <= 1 ? 0 : arguments.length - 1)) {
result += arguments.length <= i + 1 ? undefined : arguments[i + 1];
Expand Down Expand Up @@ -48,9 +47,8 @@ function dedent(strings) {
})();
}

return result.
// dedent eats leading and trailing whitespace too
trim().
// handle escaped newlines at the end to ensure they don't get stripped too
replace(/\\n/g, "\n");
if (result.startsWith('\n')) {
result = result.substr(1);
}
return result.replace(/^((.|\n)*)\n[ \t]*?$/, '$1');
}
Loading