Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tighten up filters. allow most filters to apply to all values in obje…

…cts and arrays
  • Loading branch information...
commit 64edcf2d2a0206b1cf77607dbd3762c120a70e43 1 parent d759a1e
@paularmstrong authored
Showing with 47 additions and 14 deletions.
  1. +30 −1 lib/filters.js
  2. +17 −13 tests/filters.test.js
View
31 lib/filters.js
@@ -51,6 +51,9 @@ exports.default = function (input, def) {
};
exports.first = function (input) {
+ if (typeof input === 'object' && !Array.isArray(input)) {
+ return '';
+ }
return _.first(input);
};
@@ -67,6 +70,9 @@ exports.json_encode = function (input) {
};
exports.last = function (input) {
+ if (typeof input === 'object' && !Array.isArray(input)) {
+ return '';
+ }
return _.last(input);
};
@@ -74,11 +80,16 @@ exports.length = function (input) {
if (typeof input === 'object') {
return _.keys(input).length;
}
-
return input.length;
};
exports.lower = function (input) {
+ if (typeof input === 'object') {
+ _.each(input, function (value, key) {
+ input[key] = exports.lower(value);
+ });
+ return input;
+ }
return input.toString().toLowerCase();
};
@@ -96,10 +107,22 @@ exports.reverse = function (input) {
};
exports.striptags = function (input) {
+ if (typeof input === 'object') {
+ _.each(input, function (value, key) {
+ input[key] = exports.striptags(value);
+ });
+ return input;
+ }
return input.toString().replace(/(<([^>]+)>)/ig, '');
};
exports.title = function (input) {
+ if (typeof input === 'object') {
+ _.each(input, function (value, key) {
+ input[key] = exports.title(value);
+ });
+ return input;
+ }
return input.toString().replace(/\w\S*/g, function (str) {
return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
});
@@ -110,6 +133,12 @@ exports.uniq = function (input) {
};
exports.upper = function (input) {
+ if (typeof input === 'object') {
+ _.each(input, function (value, key) {
+ input[key] = exports.upper(value);
+ });
+ return input;
+ }
return input.toString().toUpperCase();
};
View
30 tests/filters.test.js
@@ -84,9 +84,9 @@ exports.default = function (test) {
};
exports.first = function (test) {
- var input = [1, 2, 3, 4];
- test.strictEqual(1, filters.first(input));
+ test.strictEqual(1, filters.first([1, 2, 3, 4]));
test.strictEqual('2', filters.first('213'));
+ test.strictEqual('', filters.first({ foo: 'blah', bar: 'nope' }));
test.done();
};
@@ -94,8 +94,8 @@ exports.join = function (test) {
var input = [1, 2, 3];
test.strictEqual('1+2+3', filters.join(input, '+'));
test.strictEqual('1 * 2 * 3', filters.join(input, ' * '));
- input = 'asdf';
- test.strictEqual('asdf', filters.join(input, '-'), 'Non-array input is not joined.');
+ test.deepEqual({ foo: 1, bar: 2, baz: 3 }, filters.join({ foo: 1, bar: 2, baz: 3 }, ','));
+ test.strictEqual('asdf', filters.join('asdf', '-'), 'Non-array input is not joined.');
test.done();
};
@@ -116,21 +116,21 @@ exports.last = function (test) {
var input = [1, 2, 3, 4];
test.strictEqual(4, filters.last(input));
test.strictEqual('3', filters.last('123'));
+ test.strictEqual('', filters.last({ foo: 'blah', bar: 'nope' }));
test.done();
};
exports.lower = function (test) {
- var input = 'BaR';
- test.strictEqual('bar', filters.lower(input));
- input = 345;
- test.strictEqual('345', filters.lower(input));
+ test.strictEqual('bar', filters.lower('BaR'));
+ test.strictEqual('345', filters.lower(345));
+ test.deepEqual(['foo', 'bar'], filters.lower(['FOO', 'BAR']));
+ test.deepEqual({ foo: 'bar' }, filters.lower({ foo: 'BAR' }));
test.done();
};
exports.replace = function (test) {
test.strictEqual('fb', filters.replace('fooboo', 'o', '', 'g'));
test.strictEqual('fao', filters.replace('foo', 'o', 'a'));
-
test.strictEqual('-1aZ', filters.replace('$*&1aZ', '\\W+', '-'));
test.done();
};
@@ -145,12 +145,16 @@ exports.reverse = function (test) {
exports.striptags = function (test) {
var input = '<h1>foo</h1> <div class="blah">hi</div>';
test.strictEqual('foo hi', filters.striptags(input));
+ test.deepEqual(['foo', 'hi'], filters.striptags(['<h1>foo</h1>', '<div class="blah">hi</div>']));
+ test.deepEqual({ foo: 'foo', bar: 'hi' }, filters.striptags({ foo: '<h1>foo</h1>', bar: '<div class="blah">hi</div>' }));
test.done();
};
exports.title = function (test) {
var input = 'this is title case';
test.strictEqual('This Is Title Case', filters.title(input));
+ test.deepEqual(['Foo Bar', 'Blahbitty Blah'], filters.title(['foo bar', 'blaHbiTTy bLAH']));
+ test.deepEqual({ foo: 'Foo Bar', bar: 'Blahbitty Blah' }, filters.title({ foo: 'foo bar', bar: 'blaHbiTTy bLAH' }));
test.done();
};
@@ -160,10 +164,10 @@ exports.uniq = function (test) {
test.done();
};
exports.upper = function (test) {
- var input = 'bar';
- test.strictEqual('BAR', filters.upper(input));
- input = 345;
- test.strictEqual('345', filters.upper(input));
+ test.strictEqual('BAR', filters.upper('bar'));
+ test.strictEqual('345', filters.upper(345));
+ test.deepEqual(['FOO', 'BAR'], filters.upper(['foo', 'bar']));
+ test.deepEqual({ foo: 'BAR' }, filters.upper({ foo: 'bar' }));
test.done();
};
Please sign in to comment.
Something went wrong with that request. Please try again.