Skip to content

Instantly share code, notes, and snippets.

@fuzzyfox
Created April 5, 2014 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzyfox/9986322 to your computer and use it in GitHub Desktop.
Save fuzzyfox/9986322 to your computer and use it in GitHub Desktop.
JavaScript: String.prototype.filter
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* String.prototype.filter
*
* Allows for an arbitrary number of arguments which take a string as a single
* parameter, and return a string, to modify the string.
*
* @example
* ' This is my string '.filter(String.trim, function(str){return str + '!';}, String.bold);
* // returns '<b>This is my string!</b>'
*
* @author William Duyck <fuzzyfox0@gmail.com>
*/
if ( !String.prototype.filter ) {
String.prototype.filter = function() {
'use strict';
var args = Array.prototype.slice.call( arguments );
var str = this;
args.forEach( function( fn ) {
str = fn.call( str, str );
});
return str;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment