2

I am trying to write a little helper class for my ajax chat system i am working on just trying to add basic functions that i may need.

var strings = {
        filterWords: ["fool", "dumb", "arse"],
        removeSpecialChars: function (str) {
            return str.replace(/[^\w\s]/gi, '');
        },
        killSpace: function (str) {
            return str.replace(/\s/g, '');
        },
        reduceSpace: function (str) {
            return str.replace(/\s+/g, ' ');
        },
        allowLetsAndNums: function (str) {
            return str.replace(/[^A-Za-z0-9]/g, ' ');
        },
        allowLets: function (str) {
            return str.replace(/[^A-Za-z]/g, ' ');
        },
        allowNums: function (str) {
            return str.replace(/[^0-9]/g, ' ');
        },
        wordFilter: function (str) {
            var rgx = new RegExp(this.filterWords.join("|"), "gi");
            return str.replace(rgx, "****");
        }
    }

What i am finding is i may need to run multiple methods together i am asking whats the best practise to do this without resulting with below?

alert(strings.wordFilter(strings.reduceSpace(strings.allowLets("efgwge @£235%^@£ fool you a dumb arse432345$%^"))));

Thanks

2
  • Create bridge methods which will do it for you. Commented Nov 7, 2012 at 10:28
  • Try having an array of functions, then loop through each. Pass the previous result on to the next call until finished. Commented Nov 7, 2012 at 10:28

4 Answers 4

3

You could make this a fluent interface, allowing code like this:

var x = new Validation("efgwge @£235%^@£ fool you a dumb arse432345$%^");
alert(x.allowLets().reduceSpace().wordFilter().result());
// alerts "efgwge **** you a **** ****"

Your main code would need to be:

var Validation = function(str) {
    this.str = str;
    filterWords = ["fool", "dumb", "arse"]
    this.removeSpecialChars = function () {
        this.str = this.str.replace(/[^\w\s]/gi, '');
        return this;
    };    
    this.killSpace = function () {
        this.str = this.str.replace(/\s/g, '');
        return this;
    };
    this.reduceSpace = function () {
        this.str = this.str.replace(/\s+/g, ' ');
        return this;
    };
    this.allowLetsAndNums = function () {
        this.str = this.str.replace(/[^A-Za-z0-9]/g, ' ');
        return this;
    };
    this.allowLets = function () {
        this.str = this.str.replace(/[^A-Za-z]/g, ' ');
        return this;
    };
    this.allowNums = function () {
        this.str = this.str.replace(/[^0-9]/g, ' ');
        return this;
    };
    this.wordFilter = function () {
        var rgx = new RegExp(filterWords.join("|"), "gi");
        this.str = this.str.replace(rgx, "****");
        return this;
    };
    this.result = function(){
        return this.str;
    };
}

Live example: http://jsfiddle.net/fb7en/

Sign up to request clarification or add additional context in comments.

1 Comment

really like this way of doing it seems the clearest and cleanest to me thanks
1

You could extend the String prototype:

String.prototype.removeSpecialChars = function () {
return this.replace(/[^\w\s]/gi, '');
}
String.prototype.killSpace = function () {
return this.replace(/\s/g, '');
}

var foo = "This is my§$% String";
​document.write​(foo.removeSpecialChars​().killSpace());​

Comments

1

You could add the functions to the String.prototype so you can call the functions like this:

String.prototype.killSpace = function() {
  return this.replace(/\s/g, '');
}
String.prototype.reduceSpace = function () {
  return this.replace(/\s+/g, ' ');
}

"foo   bar".reduceSpace().killSpace(); // => returns foobar

Only downside to this is that you can't iterate over a string with a for..in loop then because it will list the method as a member and there's currently no cross-browser way to make it non-iterable (IE doesn't support it).

Comments

0

You might consider a chainable API for your Object:

var StringFilter = {
    _string: '',
    string: function (string) {
        this._string = string || '';
        return this;
    },
    filterWords: ["fool", "dumb", "arse"],
    removeSpecialChars: function () {
        this._string = this._string.replace(/[^\w\s]/gi, '');
        return this;
    },
    killSpace: function () {
        this._string = this._string.replace(/\s/g, '');
        return this;
    },
    reduceSpace: function () {
        this._string = this._string.replace(/\s+/g, ' ');
        return this;
    },
    allowLetsAndNums: function () {
        this._string = this._string.replace(/[^A-Za-z0-9]/g, ' ');
        return this;
    },
    allowLets: function () {
        this._string = this._string.replace(/[^A-Za-z]/g, ' ');
        return this;
    },
    allowNums: function () {
        this._string = this._string.replace(/[^0-9]/g, ' ');
        return this;
    },
    wordFilter: function () {
        var rgx = new RegExp(this.filterWords.join("|"), "gi");
        this._string = this._string.replace(rgx, "****");
        return this;
    },
    select: function () {
        return this._string;
    }
};

StringFilter
    .string("efgwge @£235%^@£ fool you a dumb arse432345$%^")
    .allowLets()
    .reduceSpace()
    .wordFilter()
    .select();

2 Comments

Excellent idea, why didnt I think of this ;)
You had a very similar idea and yours seems advantageously in asynchronous actions because of the new object. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.