From 8801de62df04a3c1b73ec50f07f23150c2c677d0 Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Mon, 6 Oct 2014 06:35:20 +0900 Subject: [PATCH] Added support for inverted sections and arrays. Fixes #14 Added test for populated arrays and inverted Finished up array logic Caught logic flaw Adjusted to more practical test content Added support for falsy arrays Added test for falsy arrays --- index.js | 8 +++++++- test/index.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a10e1e7..f195852 100644 --- a/index.js +++ b/index.js @@ -126,7 +126,13 @@ function indent(str) { function section(obj, prop, negate, thunk) { var val = obj[prop]; - if (Array.isArray(val)) return val.map(thunk).join(''); + if (Array.isArray(val)) { + if (negate) { + return val.length ? '' : thunk(obj); + } else { + return val.map(thunk).join(''); + } + } if ('function' == typeof val) return val.call(obj, thunk(obj)); if (negate) val = !val; if (val) return thunk(obj); diff --git a/test/index.js b/test/index.js index 572f625..a9b60c6 100644 --- a/test/index.js +++ b/test/index.js @@ -118,4 +118,16 @@ describe('{{^id}}', function(){ var user = { admin: false, authenticated: false }; mm('{{^admin}}{{^authenticated}}nope{{/}}{{/}}', user).should.equal('nope'); }) + + it('should consider empty arrays falsy', function(){ + var users = { users: [] }; + mm('users exist: {{^users}}nope{{/users}}', users) + .should.equal('users exist: nope'); + }) + + it('should ignore populated arrays', function(){ + var users = { users: [ 'tobi' ] }; + mm('users exist: {{#users}}yep{{/users}}{{^users}}nope{{/users}}', users) + .should.equal('users exist: yep'); + }) })