Skip to content

Commit

Permalink
Nue JS: Render zeros in loops. Fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
tipiirai committed Feb 23, 2024
1 parent 1cc7260 commit a3b29f5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/nuejs/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function setAttribute(key, attribs, data) {
if (val.constructor === Object) return

// attributes must be strings
if (1 * val) val = attribs[key] = '' + val
if (val === 0 || 1 * val) val = attribs[key] = '' + val

const has_expr = val.includes('{')

Expand Down Expand Up @@ -144,7 +144,7 @@ function processFor(node, expr, data, deps) {
if (i >= 0) return item[i]
}

return key === $keys ? item || data[key] :
return key === $keys ? (item == null ? data[key] : item) :
key == $index ? items.indexOf(item) :
$keys.includes(key) ? item[key] :
data[key]
Expand Down
8 changes: 4 additions & 4 deletions packages/nuejs/test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ test('Class and style', () => {
})


test('Loops', () => {
test.only('Loops', () => {

runTests({

'<p :for="n in nums">{ n }</p>': '<p>1</p><p>2</p><p>3</p>',
'<p :for="n in nums">{ n }</p>': '<p>-1</p><p>0</p><p>1</p>',

'<p :for="[key, value, i] in Object.entries(person)">{ i }: { key } = { value }</p>':
'<p>0: name = Nick</p><p>1: email = nick@acme.org</p><p>2: age = 10</p>',
Expand All @@ -109,12 +109,12 @@ test('Loops', () => {

// successive loops
'<div><p :for="x in nums">{ x }</p><a :for="y in nums">{ y }</a></div>':
'<div><p>1</p><p>2</p><p>3</p><a>1</a><a>2</a><a>3</a></div>',
'<div><p>-1</p><p>0</p><p>1</p><a>-1</a><a>0</a><a>1</a></div>',

}, {
items: [ { name: 'John', age: 22 }, { name: 'Alice', age: 33 }],
person: { name: 'Nick', email: 'nick@acme.org', age: 10 },
nums: [1, 2, 3],
nums: [-1, 0, 1],
})
})

Expand Down

0 comments on commit a3b29f5

Please sign in to comment.