Skip to content

Commit

Permalink
Make is empty return false for boolean true.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineveldhoven committed Sep 25, 2023
1 parent 59d991d commit 24c4581
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/twig.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module.exports = function (Twig) {
'use strict';
Twig.tests = {
empty(value) {
// Handle boolean true
if (value === true) {
return false;
}

// Handle null or undefined
if (value === null || value === undefined) {
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions test/test.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ describe('Twig.js Tests ->', function () {
twig({data: '{{ ["1"] is empty }}'}).render().should.equal('false');
});

it('should identify booleans', function () {
// Array
twig({data: '{{ foo is empty }}'}).render( { foo: true } ).should.equal('false');
twig({data: '{{ foo is empty }}'}).render( { foo: false }).should.equal('true');
});

it('should identify null or undefined', function () {
// Array
twig({data: '{{ foo is empty }}'}).render( {foo: null} ).should.equal('true');
twig({data: '{{ foo is empty }}'}).render( {foo: undefined} ).should.equal('true');
});

it('should identify empty objects', function () {
// Object
twig({data: '{{ {} is empty }}'}).render().should.equal('true');
Expand Down

0 comments on commit 24c4581

Please sign in to comment.