- Code: Select all
var p = String.prototype, r = p.replace;
p.trim = p.trim || r.curry(/^[\s\u00A0]+|[\s\u00A0]+$/g, "");
p.trimLeft = p.trimLeft || r.curry(/^[\s\u00A0]+/, "");
p.trimRight = p.trimRight || r.curry(/[\s\u00A0]+$/, "");
How cool is that!?!?!? These four lines will only define the trim functions if they are not defined. If you want proof that this approach really works, execute the jPaq example which is found here. On that page, you will also find other one-liners.
Even though JavaScript is not a functional programming language, the fact that you can turn it into one is awesome!


