Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ Available Filters

* batch
* groupby
* length
* markdown
* nl2br
* pluck
* slice
* split
* trim
* truncate
Expand Down
2 changes: 2 additions & 0 deletions lib/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
exports.batch = require('./batch');
exports.groupby = require('./groupby');
exports.length = require('./length');
exports.markdown = require('./markdown');
exports.nl2br = require('./nl2br');
exports.pluck = require('./pluck');
exports.slice = require('./slice');
exports.split = require('./split');
exports.trim = require('./trim');
exports.truncate = require('./truncate');
24 changes: 24 additions & 0 deletions lib/filters/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var utils = require('../utils');

/**
* Returns the length of an array or string.
*
* @example
* {{ "foobar"|length }}
* // => 6
*
* @example
* {{ [1, 2, 3, 4]|length }}
* // => 4
*
* @example
* {{ obj|length }}
* // => 3
*/
module.exports = function (input) {
if (utils.isArray(input) || (typeof input === 'string' || input instanceof String)) {
return input.length;
}

return utils.keys(input).length;
};
14 changes: 14 additions & 0 deletions lib/filters/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Extracts a slice of an array or a string.
*
* @example
* {{ "12345"|slice(1,3) }}
* // => 23
*
* @example
* {{ [1, 2, 3, 4, 5]|slice(1, 3) }}
* // => [2, 3]
*/
module.exports = function (input, start, end) {
return input.slice(start, end);
}
41 changes: 41 additions & 0 deletions tests/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ describe('Filters:', function () {
});
});

describe('length', function () {
extras.useFilter(swig, 'length');
it('{{ "foobar"|length }}', function () {
expect(swig.render('{{ "foobar"|length }}'))
.to.equal('6');
});

it('{{ [1, 2, 3, 4, 5, 6]|length }}', function () {
expect(swig.render('{{ [1, 2, 3, 4, 5, 6]|length }}'))
.to.equal('6');
});

it('{{ obj|length }}', function () {
expect(swig.render('{{ foo|length }}', { locals: { foo: { 'a': 1, 'b': 2, 'c': 3 } }}))
.to.equal('3');
});
});

describe('markdown', function () {
extras.useFilter(swig, 'markdown');
it('{{ foo|markdown }}', function () {
Expand Down Expand Up @@ -54,6 +72,29 @@ describe('Filters:', function () {
});
});

describe('slice', function () {
extras.useFilter(swig, 'slice');
it('{{ "12345"|slice(1,3) }}', function () {
expect(swig.render('{{ "12345"|slice(1,3) }}'))
.to.equal('23');
});

it('{{ "12345"|slice(1) }}', function () {
expect(swig.render('{{ "12345"|slice(1) }}'))
.to.equal('2345');
});

it('{{ [1, 2, 3, 4, 5]|slice(1,3) }}', function () {
expect(swig.render('{{ [1, 2, 3, 4, 5]|slice(1,3) }}'))
.to.equal('2,3');
});

it('{{ [1, 2, 3, 4, 5]|slice(1) }}', function () {
expect(swig.render('{{ [1, 2, 3, 4, 5]|slice(1) }}'))
.to.equal('2,3,4,5');
});
});

describe('split', function () {
extras.useFilter(swig, 'split');
it('{{ foo|split(",")|join(" & ") }}', function () {
Expand Down