From b98d89560c3358c3fd244be818307211b90de63b Mon Sep 17 00:00:00 2001 From: Adrian Jost Date: Fri, 8 Mar 2019 10:09:17 +0100 Subject: [PATCH 1/2] enable tab compatbility --- __tests__/__snapshots__/dedent-tests.js.snap | 12 ++++++++++++ __tests__/dedent-tests.js | 18 ++++++++++++++++++ dedent.js | 3 ++- dist/dedent.js | 16 ++++++++-------- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/__tests__/__snapshots__/dedent-tests.js.snap b/__tests__/__snapshots__/dedent-tests.js.snap index 9d2abbc8..06d6211b 100644 --- a/__tests__/__snapshots__/dedent-tests.js.snap +++ b/__tests__/__snapshots__/dedent-tests.js.snap @@ -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 @@ -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 diff --git a/__tests__/dedent-tests.js b/__tests__/dedent-tests.js index 501bc7c5..74eb3927 100644 --- a/__tests__/dedent-tests.js +++ b/__tests__/dedent-tests.js @@ -97,4 +97,22 @@ describe("dedent", () => {

\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 */ }); diff --git a/dedent.js b/dedent.js index 9d472d48..8ef9a4ee 100644 --- a/dedent.js +++ b/dedent.js @@ -39,7 +39,8 @@ export default function dedent( if (mindent !== null) { const m = mindent; // appease Flow - result = lines.map(l => l[0] === " " ? l.slice(m) : l).join("\n"); + const whitespaceCharacters = [" ", "\t"]; // [space, tab] + result = lines.map(l => whitespaceCharacters.includes(l[0]) ? l.slice(m) : l).join("\n"); } return result diff --git a/dist/dedent.js b/dist/dedent.js index 5abc6a8c..bbfb024b 100644 --- a/dist/dedent.js +++ b/dist/dedent.js @@ -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]; @@ -42,15 +41,16 @@ function dedent(strings) { if (mindent !== null) { (function () { var m = mindent; // appease Flow + var whitespaceCharacters = [" ", "\t"]; // [space, tab] result = lines.map(function (l) { - return l[0] === " " ? l.slice(m) : l; + return whitespaceCharacters.includes(l[0]) ? 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"); } From acae03442515886e3249cbe57ab76c3840e8182e Mon Sep 17 00:00:00 2001 From: adrianjost <22987140+adrianjost@users.noreply.github.com> Date: Wed, 4 Nov 2020 08:52:57 +0100 Subject: [PATCH 2/2] incorporate pr feedback - improve speed --- dedent.js | 3 +-- dist/dedent.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dedent.js b/dedent.js index 8ef9a4ee..b1516d95 100644 --- a/dedent.js +++ b/dedent.js @@ -39,8 +39,7 @@ export default function dedent( if (mindent !== null) { const m = mindent; // appease Flow - const whitespaceCharacters = [" ", "\t"]; // [space, tab] - result = lines.map(l => whitespaceCharacters.includes(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 diff --git a/dist/dedent.js b/dist/dedent.js index bbfb024b..3a441f78 100644 --- a/dist/dedent.js +++ b/dist/dedent.js @@ -41,9 +41,8 @@ function dedent(strings) { if (mindent !== null) { (function () { var m = mindent; // appease Flow - var whitespaceCharacters = [" ", "\t"]; // [space, tab] result = lines.map(function (l) { - return whitespaceCharacters.includes(l[0]) ? l.slice(m) : l; + return l[0] === " " || l[0] === "\t" ? l.slice(m) : l; }).join("\n"); })(); }