Skip to content

Commit

Permalink
Fix stripIndents and add regression tests (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz authored Nov 24, 2017
1 parent f81559f commit 292c10a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/stripIndent/fixtures/maintainEmptyLines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wow such indent gone

very amaze
10 changes: 10 additions & 0 deletions src/stripIndent/stripIndent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ test('maintains deeper indentation', async (t) => {
t.is(actual, expected)
})

test('maintains empty lines', async (t) => {
const expected = await readFromFixture(__dirname, 'maintainEmptyLines')
const actual = stripIndent`
wow such indent gone
very ${val}
`
t.is(actual, expected)
})

test('does nothing if there are no indents', async (t) => {
const expected = 'wow such doge'
const actual = stripIndent`wow such doge`
Expand Down
2 changes: 1 addition & 1 deletion src/stripIndentTransformer/stripIndentTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const stripIndentTransformer = (type = 'initial') => ({
endResult = indent > 0 ? endResult.replace(regexp, '') : endResult
} else if (type === 'all') {
// remove all indentation from each line
endResult = endResult.replace(/(?:\n\s*)/g, '\n')
endResult = endResult.replace(/(?:\n[^\S\n]*)/g, '\n')
} else {
throw new Error(`Unknown type: ${type}`)
}
Expand Down
4 changes: 4 additions & 0 deletions src/stripIndents/fixtures/maintainEmptyLines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wow such indent gone
very amaze

foo bar baz
11 changes: 11 additions & 0 deletions src/stripIndents/stripIndents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ test('strips all indentation', async (t) => {
`
t.is(actual, expected)
})

test('maintains empty lines', async (t) => {
const expected = await readFromFixture(__dirname, 'maintainEmptyLines')
const actual = stripIndents`
wow such indent gone
very ${val}
foo bar baz
`
t.is(actual, expected)
})

0 comments on commit 292c10a

Please sign in to comment.