Skip to content

Commit

Permalink
fix(replaceChunks method): replace undefined values instead of falsy …
Browse files Browse the repository at this point in the history
…ones with empty string
  • Loading branch information
Alexander Zhukov committed Mar 29, 2018
1 parent 2229c9d commit 737c2eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function isSameTextNode(nodeA, nodeB){
return false;
};

function isDefined(value){
return typeof value !== 'undefined';
}

//hash function taken from https://github.com/darkskyapp/string-hash/blob/master/index.js
function hash(str) {
var hash = 5381,
Expand Down Expand Up @@ -456,7 +460,8 @@ Template.prototype.generateTokenName = function(index){

Template.prototype.replaceTokens = function(text, dataMap = this.values){
return text.replace(this.replaceChunkRegex, (token, _, index) => {
return this.getChunkById(index, dataMap) || '';
const chunk = this.getChunkById(index, dataMap);
return isDefined(chunk) ? chunk : '';
});
};

Expand Down
38 changes: 35 additions & 3 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,36 @@ describe('transitions', () => {
expect(container.innerHTML).toBe(snapshot3);
});

it('updates classes correctly 2', () => {
const container = document.createElement('div');

const tpl = (scope) => html`
${scope.a.map((item, index) => html`
<div class="foo foo-${index} bar-${item}"></div>
`)}
`;

const data1 = { a: [1, 2, 3] };
const snapshot1 = `<div class="foo foo-0 bar-1"></div>
<div class="foo foo-1 bar-2"></div>
<div class="foo foo-2 bar-3"></div>
`;

render(tpl(data1), container);
expect(container.innerHTML).toBe(snapshot1);

const data2 = { a: [5, false, 3] };
const snapshot2 = `<div class="foo foo-0 bar-5"></div>
<div class="foo foo-1 bar-false"></div>
<div class="foo foo-2 bar-3"></div>
`;

render(tpl(data2), container);
expect(container.innerHTML).toBe(snapshot2);
});

});


Expand Down Expand Up @@ -922,7 +952,9 @@ describe('Range', () => {
////const container2 = document.createElement('div');

//const tpl = (scope) => html`
//${html`foo`}
//${scope.a.map((item, index) => html`
//<span class="foo foo-${index} bar-${item}">${item}</span>
//`)}
//`;

////const tpl2 = (scope) => html`
Expand All @@ -931,9 +963,9 @@ describe('Range', () => {

////const rr = tpl2({ a: 1});

//render(tpl({ a: 'test' }), container);
//render(tpl({ a: [1,2,3] }), container);
//console.log(container.innerHTML);
//render(tpl({ a: 'test' }), container);
//render(tpl({ a: [3,4,5] }), container);
//console.log(container.innerHTML);
////render(tpl({ a: 1, c: 1 }), container);
////console.log(container.innerHTML);
Expand Down

0 comments on commit 737c2eb

Please sign in to comment.