Skip to content
Merged
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
41 changes: 26 additions & 15 deletions src/__testUtils__/__tests__/dedent-test.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}