Skip to content

Commit

Permalink
Added filter chaining using chain() and value() re validatorjs#36
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Oct 9, 2011
1 parent fa6aab2 commit 0d5c2cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
35 changes: 24 additions & 11 deletions lib/filter.js
Expand Up @@ -9,6 +9,19 @@ Filter.prototype.modify = function(str) {
this.str = str;
}

Filter.prototype.wrap = function (str) {
return str;
}

Filter.prototype.value = function () {
return this.str;
}

Filter.prototype.chain = function () {
this.wrap = function () { return this };
return this;
}

//Create some aliases - may help code readability
Filter.prototype.convert = Filter.prototype.sanitize = function(str) {
this.str = str;
Expand All @@ -17,53 +30,53 @@ Filter.prototype.convert = Filter.prototype.sanitize = function(str) {

Filter.prototype.xss = function(is_image) {
this.modify(xss.clean(this.str, is_image));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.entityDecode = function() {
this.modify(entities.decode(this.str));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.entityEncode = function() {
this.modify(entities.encode(this.str));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.ltrim = function(chars) {
chars = chars || whitespace;
this.modify(this.str.replace(new RegExp('^['+chars+']+', 'g'), ''));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.rtrim = function(chars) {
chars = chars || whitespace;
this.modify(this.str.replace(new RegExp('['+chars+']+$', 'g'), ''));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.trim = function(chars) {
chars = chars || whitespace;
this.modify(this.str.replace(new RegExp('^['+chars+']+|['+chars+']+$', 'g'), ''));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.ifNull = function(replace) {
if (!this.str || this.str === '') {
this.modify(replace);
}
return this.str;
return this.wrap(this.str);
}

Filter.prototype.toFloat = function() {
this.modify(parseFloat(this.str));
return this.str;
return this.wrap(this.str);
}

Filter.prototype.toInt = function(radix) {
radix = radix || 10;
this.modify(parseInt(this.str, radix));
return this.str;
return this.wrap(this.str);
}

//Any strings with length > 0 (except for '0' and 'false') are considered true,
Expand All @@ -74,7 +87,7 @@ Filter.prototype.toBoolean = function() {
} else {
this.modify(true);
}
return this.str;
return this.wrap(this.str);
}

//String must be equal to '1' or 'true' to be considered true, all other strings
Expand All @@ -85,5 +98,5 @@ Filter.prototype.toBooleanStrict = function() {
} else {
this.modify(false);
}
return this.str;
return this.wrap(this.str);
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ "name" : "validator",
"description" : "Data validation, filtering and sanitization for node.js",
"version" : "0.2.7",
"version" : "0.2.8",
"homepage" : "http://github.com/chriso/node-validator",
"keywords" : ["validator", "validation", "assert", "params", "sanitization", "xss", "entities", "sanitize", "sanitisation", "input"],
"author" : "Chris O'Hara <cohara87@gmail.com>",
Expand Down
5 changes: 5 additions & 0 deletions test/filter.test.js
Expand Up @@ -130,5 +130,10 @@ module.exports = {
//Need more tests!
assert.equal('[removed] foobar', Filter.sanitize('javascript : foobar').xss());
assert.equal('[removed] foobar', Filter.sanitize('j a vasc ri pt: foobar').xss());
},

'test chaining': function () {
assert.equal('&amp;amp;amp;', Filter.sanitize('&').chain().entityEncode().entityEncode().entityEncode().value());
}

}

0 comments on commit 0d5c2cf

Please sign in to comment.