Skip to content

Commit

Permalink
Remove use of ES5 Array.prototype.filer() function, to maintain com…
Browse files Browse the repository at this point in the history
…patibility with IE8. Fixes #144
  • Loading branch information
gregjacobs committed Mar 14, 2016
1 parent e878f8a commit 733c05b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/Autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,39 @@ Autolinker.prototype = {
// And finally, remove matches for match types that have been turned
// off. We needed to have all match types turned on initially so that
// things like hashtags could be filtered out if they were really just
// part of a URL match (as a named anchor).
if( !this.hashtag ) matches = matches.filter( function( match ) { return match.getType() !== 'hashtag'; } );
if( !this.email ) matches = matches.filter( function( match ) { return match.getType() !== 'email'; } );
if( !this.phone ) matches = matches.filter( function( match ) { return match.getType() !== 'phone'; } );
if( !this.twitter ) matches = matches.filter( function( match ) { return match.getType() !== 'twitter'; } );
// part of a URL match (for instance, as a named anchor).
matches = this.removeUnwantedMatches( matches );

return matches;
},


/**
* Removes matches for matchers that were turned off in the options. For
* example, if {@link #hashtag hashtags} were not to be matched, we'll
* remove them from the `matches` array here.
*
* @private
* @param {Autolinker.match.Match[]} matches The array of matches to remove
* the unwanted matches from. Note: this array is mutated for the
* removals.
* @return {Autolinker.match.Match[]} The mutated input `matches` array.
*/
removeUnwantedMatches : function( matches ) {
var remove = Autolinker.Util.remove;

if( !this.hashtag ) remove( matches, function( match ) { return match.getType() === 'hashtag'; } );
if( !this.email ) remove( matches, function( match ) { return match.getType() === 'email'; } );
if( !this.phone ) remove( matches, function( match ) { return match.getType() === 'phone'; } );
if( !this.twitter ) remove( matches, function( match ) { return match.getType() === 'twitter'; } );
if( !this.urls.schemeMatches ) {
matches = matches.filter( function( m ) { return m.getType() !== 'url' || m.getUrlMatchType() !== 'scheme'; } );
remove( matches, function( m ) { return m.getType() === 'url' && m.getUrlMatchType() === 'scheme'; } );
}
if( !this.urls.wwwMatches ) {
matches = matches.filter( function( m ) { return m.getType() !== 'url' || m.getUrlMatchType() !== 'www'; } );
remove( matches, function( m ) { return m.getType() === 'url' && m.getUrlMatchType() === 'www'; } );
}
if( !this.urls.tldMatches ) {
matches = matches.filter( function( m ) { return m.getType() !== 'url' || m.getUrlMatchType() !== 'tld'; } );
remove( matches, function( m ) { return m.getType() === 'url' && m.getUrlMatchType() === 'tld'; } );
}

return matches;
Expand Down
22 changes: 22 additions & 0 deletions src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ Autolinker.Util = {
},


/**
* Removes array elements based on a filtering function. Mutates the input
* array.
*
* Using this instead of the ES5 Array.prototype.filter() function, to allow
* Autolinker compatibility with IE8, and also to prevent creating many new
* arrays in memory for filtering.
*
* @param {Array} arr The array to remove elements from. This array is
* mutated.
* @param {Function} fn A function which should return `true` to
* remove an element.
* @return {Array} The mutated input `arr`.
*/
remove : function( arr, fn ) {
for( var i = arr.length - 1; i >= 0; i-- ) {
if( fn( arr[ i ] ) === true ) {
arr.splice( i, 1 );
}
}
},


/**
* Performs the functionality of what modern browsers do when `String.prototype.split()` is called
Expand Down

0 comments on commit 733c05b

Please sign in to comment.