Skip to content

Commit

Permalink
feat(template transform): added JSON capabilities to template transfo…
Browse files Browse the repository at this point in the history
…rms.
  • Loading branch information
David Diers committed Feb 14, 2017
1 parent 0477553 commit 845beb8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/plugins/transform/tokenTemplateValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ module.exports = superclass => class extends superclass {
}
const templatePath = this.getTemplatePath(this.options.origin);

const tmpFile = fs.readFileSync(templatePath, 'utf-8');
const tmpFile = fs.readFileSync(templatePath, 'utf-8').replace(/\\"/g, '"');

const templated = mustache.render(tmpFile, tokens);
if (this.options.json) {
return super.transform(JSON.parse(mustache.render(tmpFile, tokens)));
}

if (this.options.unescape) {
return super.transform(mustache.render(tmpFile, tokens).replace(/\\"/g, '"'));
}

// Call super.transform and pass along the new value to honor composition.
return super.transform(templated);
return super.transform(mustache.render(tmpFile, tokens));
}

getTemplatePath(value) {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/templateFour.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dust template "one" name is: {{name}}, test has the value of \"testKey2: {{testKey2}}.
8 changes: 8 additions & 0 deletions test/helpers/templateJson.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"test": "{{name}}",
"item": "{{testKey2}}",
"thing": [
"one",
"two"
]
}
63 changes: 62 additions & 1 deletion test/tokenTemplateValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ const Implemented = class implementer extends tokenTemplateValues(superClass) {
// Options for testing with template path and object with no tokens.
const options = {
origin: '../../../test/helpers/templateLiteralTestOne.tpl',
named: false
named: false,
json: false,
unescape: false
};

// Options for testing with tokens and value.
const options2 = {
origin: '../../../test/helpers/templateLiteralTestTwo.tpl',
named: true,
json: false,
unescape: false,
tokens: {
name: 'name',
test: 'testKey2'
Expand All @@ -40,17 +44,46 @@ const value = {
const options3 = {
origin: '../../../test/helpers/templateLiteralTestThree.tpl',
named: true,
json: false,
unescape: false,
tokens: {
test: 'testKey2'
}
};

// Options for testing with tokens and value.
const options4 = {
origin: '../../../test/helpers/templateJson.tpl',
json: true,
unescape: true,
named: false,
tokens: {
test: 'testKey2'
}
};

// Options for testing with tokens and value.
const options5 = {
origin: '../../../test/helpers/templateFour.tpl',
json: false,
unescape: true,
named: false,
tokens: {
test: 'testKey2'
}
};


const testClass = new Implemented(options, {});

const testClass2 = new Implemented(options2, {});

const testClass3 = new Implemented(options3, {monkey: 'toofer'});

const testClass4 = new Implemented(options4, {monkey: 'toofer'});

const testClass5 = new Implemented(options5, {monkey: 'toofer'});

module.exports = {
tokenOnlyValues: (test) => {
test.expect(1);
Expand Down Expand Up @@ -88,5 +121,33 @@ module.exports = {
'Dust template one name is: toofer, test has the value of testKey2: item1.'
);
test.done();
},
holdOversWithTokensJson: (test) => {
test.expect(1);
testClass4.options.origin = 'test/helpers/templateJson.tpl';
testClass4.transform(value);
test.deepEqual(
testClass4.value,
{
test: 'testName',
item: 'item1',
thing: [
'one',
'two'
]
}
);
test.done();
},
holdOversWithTokensUnescape: (test) => {
test.expect(1);
testClass5.options.origin = 'test/helpers/templateFour.tpl';
testClass5.transform(value);
test.deepEqual(
testClass5.value,
'Dust template "one" name is: testName, test has the value of \"testKey2: item1.' // eslint-disable-line no-useless-escape
);
test.done();
}

};

0 comments on commit 845beb8

Please sign in to comment.