Skip to content

Commit

Permalink
Implemented batch filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jmealo committed Jun 8, 2014
1 parent bcc3632 commit 19fe85e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/twig.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,38 @@ var Twig = (function (Twig) {
raw: function(value) {
//Raw filter shim
return value;
},
batch: function(items, params) {
var size = params.shift(),
fill = params.shift(),
result,
last,
missing;

if (!Twig.lib.is("Array", items)) {
throw new Twig.Error("batch filter expects items to be an array");
}

if (!Twig.lib.is("Number", size)) {
throw new Twig.Error("batch filter expects size to be a number");
}

size = Math.ceil(size);

result = Twig.lib.chunkArray(items, size);

if (fill && items.length % size != 0) {
last = result.pop();
missing = size - last.length;

while (missing--) {
last.push(fill);
}

result.push(last);
}

return result;
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/twig.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,23 @@ var Twig = (function(Twig) {
return string.split(search).join(replace);
};

// chunk an array (arr) into arrays of (size) items, returns an array of arrays, or an empty array on invalid input
Twig.lib.chunkArray = function (arr, size) {
var returnVal = [],
x = 0,
len = arr.length;

if (size < 1 || !Twig.lib.is("Array", arr)) {
return [];
}

while (x < len) {
returnVal.push(arr.slice(x, x += size));
}

return returnVal;
};

return Twig;

})(Twig || { });
23 changes: 23 additions & 0 deletions test/test.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,29 @@ describe("Twig.js Filters ->", function() {
});
});

describe('batch ->', function () {
it('should work with arrays that require filling (with fill specified)', function () {
var test_template = twig({data: "{{ ['a', 'b', 'c', 'd', 'e', 'f', 'g']|batch(3, 'x') }}"});
test_template.render().should.equal("a,b,c,d,e,f,g,x,x");
});
it('should work with arrays that require filling (without fill specified)', function () {
var test_template = twig({data: "{{ ['a', 'b', 'c', 'd', 'e', 'f', 'g']|batch(3) }}"});
test_template.render().should.equal("a,b,c,d,e,f,g");
});
it('should work with arrays that do not require filling (with fill specified)', function () {
var test_template = twig({data: "{{ ['a', 'b', 'c', 'd', 'e', 'f']|batch(3, 'x') }}"});
test_template.render().should.equal("a,b,c,d,e,f");
});
it('should work with arrays that do not require filling (without fill specified)', function () {
var test_template = twig({data: "{{ ['a', 'b', 'c', 'd', 'e', 'f']|batch(3) }}"});
test_template.render().should.equal("a,b,c,d,e,f");
});
it('should return an empty result for an empty array', function () {
var test_template = twig({data: "{{ []|batch(3, 'x') }}"});
test_template.render().should.equal("");
});
});

describe('last ->', function () {
it('should return last character in string', function () {
var test_template = twig({data: "{{ 'abcd'|last }}"});
Expand Down

0 comments on commit 19fe85e

Please sign in to comment.