-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from declandewet/indent-newline-transform
Indent newline transform + convert newline strings to newline-separated arrays in HTML
- Loading branch information
Showing
9 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>amaze</h1> | ||
<ul> | ||
<li>one</li> | ||
<li>two</li> | ||
<li>three</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
import splitStringTransformer from './splitStringTransformer' | ||
|
||
export default splitStringTransformer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
const splitStringTransformer = (splitBy) => ({ | ||
onSubstitution (substitution, resultSoFar) { | ||
if (splitBy != null && typeof splitBy === 'string') { | ||
if (typeof substitution === 'string' && substitution.includes(splitBy)) { | ||
substitution = substitution.split(splitBy) | ||
} | ||
} else { | ||
throw new Error('You need to specify a string character to split by.') | ||
} | ||
return substitution | ||
} | ||
}) | ||
|
||
export default splitStringTransformer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
import test from 'ava' | ||
import TemplateTag from '../TemplateTag' | ||
import inlineArrayTransformer from '../inlineArrayTransformer' | ||
import splitStringTransformer from './splitStringTransformer' | ||
|
||
test('splits a string substitution into an array by the specified character', (t) => { | ||
const tag = new TemplateTag( | ||
splitStringTransformer('\n'), | ||
inlineArrayTransformer | ||
) | ||
t.is(tag`foo ${'bar\nbaz'}`, 'foo bar baz') | ||
}) | ||
|
||
test('ignores string if splitBy character is not found', (t) => { | ||
const tag = new TemplateTag(splitStringTransformer('.')) | ||
t.is(tag`foo ${'bar,baz'}`, 'foo bar,baz') | ||
}) | ||
|
||
test('ignores substitution if it is not a string', (t) => { | ||
const tag = new TemplateTag(splitStringTransformer('')) | ||
t.is(tag`foo ${5}`, 'foo 5') | ||
}) | ||
|
||
test('throws an error if splitBy param is undefined or not a string', (t) => { | ||
const tag1 = new TemplateTag(splitStringTransformer) | ||
const tag2 = new TemplateTag(splitStringTransformer(5)) | ||
t.throws(() => tag1`foo ${'bar'}`) | ||
t.throws(() => tag2`foo ${'bar'}`) | ||
}) |