Created
April 5, 2014 01:33
-
-
Save fuzzyfox/9986322 to your computer and use it in GitHub Desktop.
JavaScript: String.prototype.filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 <[email protected]> | |
*/ | |
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