Skip to content

Commit

Permalink
Merge PR twigjs#161 and rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
justjohn committed Jun 10, 2014
2 parents c12b79d + bb2bc0e commit a1ddfe2
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 4 deletions.
Empty file added demos/test.html
Empty file.
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
49 changes: 49 additions & 0 deletions twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,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 || { });
Expand Down Expand Up @@ -4526,6 +4543,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
6 changes: 3 additions & 3 deletions twig.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion twig.min.js.map

Large diffs are not rendered by default.

0 comments on commit a1ddfe2

Please sign in to comment.