Skip to content

Commit

Permalink
Merge pull request #28 from adrianjost/support-tabs
Browse files Browse the repository at this point in the history
enable tab compatbility
  • Loading branch information
JoshuaKGoldberg committed May 22, 2023
2 parents 77673fe + acae034 commit 4c1969e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
12 changes: 12 additions & 0 deletions __tests__/__snapshots__/dedent-tests.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ exports[`dedent works with blank first line 1`] = `
That\'s all."
`;

exports[`dedent works with escaped tabs for indentation 1`] = `
"first
second
third"
`;

exports[`dedent works with interpolation 1`] = `
"first line
second
Expand All @@ -50,6 +56,12 @@ exports[`dedent works with suppressed newlines 1`] = `
third"
`;

exports[`dedent works with tabs for indentation 1`] = `
"first
second
third"
`;

exports[`dedent works without interpolation 1`] = `
"first
second
Expand Down
18 changes: 18 additions & 0 deletions __tests__/dedent-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,22 @@ describe("dedent", () => {
</p>\n
`).toMatchSnapshot();
});

/* eslint-disable indent */
it("works with tabs for indentation", () => {
expect(
dd`
first
second
third
`
).toMatchSnapshot();
});

it("works with escaped tabs for indentation", () => {
expect(
dd("\t\tfirst\n\t\t\tsecond\n\t\t\t\tthird")
).toMatchSnapshot();
});
/* eslint-enable indent */
});
2 changes: 1 addition & 1 deletion dedent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function dedent(

if (mindent !== null) {
const m = mindent; // appease Flow
result = lines.map(l => l[0] === " " ? l.slice(m) : l).join("\n");
result = lines.map(l => (l[0] === " " || l[0] === "\t") ? l.slice(m) : l).join("\n");
}

return result
Expand Down
15 changes: 7 additions & 8 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 All @@ -43,14 +42,14 @@ function dedent(strings) {
(function () {
var m = mindent; // appease Flow
result = lines.map(function (l) {
return l[0] === " " ? l.slice(m) : l;
return l[0] === " " || l[0] === "\t" ? l.slice(m) : l;
}).join("\n");
})();
}

return result.
return result
// dedent eats leading and trailing whitespace too
trim().
.trim()
// handle escaped newlines at the end to ensure they don't get stripped too
replace(/\\n/g, "\n");
.replace(/\\n/g, "\n");
}

0 comments on commit 4c1969e

Please sign in to comment.