Skip to content

Commit

Permalink
refactor: dedentString from dedent
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Mar 31, 2021
1 parent 9531486 commit f4421d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
41 changes: 26 additions & 15 deletions src/__testUtils__/__tests__/dedent-test.js
@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { dedent } from '../dedent';
import { dedent, dedentString } from '../dedent';

describe('dedent', () => {
describe('dedentString', () => {
it('removes indentation in typical usage', () => {
const output = dedent`
const output = dedentString(`
type Query {
me: User
}
Expand All @@ -14,7 +14,7 @@ describe('dedent', () => {
id: ID
name: String
}
`;
`);
expect(output).to.equal(
[
'type Query {',
Expand All @@ -30,23 +30,23 @@ describe('dedent', () => {
});

it('removes only the first level of indentation', () => {
const output = dedent`
const output = dedentString(`
first
second
third
fourth
`;
`);
expect(output).to.equal(
['first', ' second', ' third', ' fourth'].join('\n'),
);
});

it('does not escape special characters', () => {
const output = dedent`
const output = dedentString(`
type Root {
field(arg: String = "wi\th de\fault"): String
}
`;
`);
expect(output).to.equal(
[
'type Root {',
Expand All @@ -57,38 +57,49 @@ describe('dedent', () => {
});

it('also removes indentation using tabs', () => {
const output = dedent`
const output = dedentString(`
\t\t type Query {
\t\t me: User
\t\t }
`;
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes leading and trailing newlines', () => {
const output = dedent`
const output = dedentString(`
type Query {
me: User
}
`;
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes all trailing spaces and tabs', () => {
const output = dedent`
const output = dedentString(`
type Query {
me: User
}
\t\t \t `;
\t\t \t `);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('works on text without leading newline', () => {
const output = dedent` type Query {
const output = dedentString(` type Query {
me: User
}
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
});

describe('dedent', () => {
it('removes indentation in typical usage', () => {
const output = dedent`
type Query {
me: User
}
`;
Expand Down
31 changes: 18 additions & 13 deletions src/__testUtils__/dedent.js
@@ -1,3 +1,20 @@
export function dedentString(string: string): string {
const trimmedStr = string
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs

// fixes indentation by removing leading spaces and tabs from each line
let indent = '';
for (const char of trimmedStr) {
if (char !== ' ' && char !== '\t') {
break;
}
indent += char;
}

return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
}

/**
* An ES6 string tag that fixes indentation and also trims string.
*
Expand Down Expand Up @@ -25,17 +42,5 @@ export function dedent(
}
}

const trimmedStr = str
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs

// fixes indentation by removing leading spaces and tabs from each line
let indent = '';
for (const char of trimmedStr) {
if (char !== ' ' && char !== '\t') {
break;
}
indent += char;
}
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
return dedentString(str);
}

0 comments on commit f4421d9

Please sign in to comment.