Created
May 14, 2011 01:13
-
-
Save jejacks0n/971572 to your computer and use it in GitHub Desktop.
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
// original javascript | |
String.prototype.singleDiff = function(that) { | |
var diff = ''; | |
var thatLength = that.length; | |
for (var i = 0; i < thatLength; ++i) { | |
if (this[i] != that[i]) { | |
var regExEscape = this.substr(i).regExEscape().replace(/^\s+|^( )+/g, ''); | |
var re = new RegExp(regExEscape + '$', 'm'); | |
diff = that.substr(i).replace(re, ''); | |
break; | |
} | |
} | |
return diff; | |
}; | |
# coffeescript -- note that we have to use _i here, which seems really bad | |
String::singleDiff = (that) -> | |
diff = '' | |
for char in that | |
if char != @[_i] # using _i is lame! | |
re = new RegExp(@substr(_i).regExpEscape().replace(/^\s+|^( )+/g, '') + '$', 'm') | |
diff = that.substr(_i).replace(re, '') | |
break | |
return diff | |
# other attempts and their results | |
for index in [0..that.length] | |
-- for (index = 0, _ref = that.length; 0 <= _ref ? index <= _ref : index >= _ref; 0 <= _ref ? index++ : index--) { } | |
for char in that | |
-- for (_i = 0, _len = that.length; _i < _len; _i++) { | |
char = that[_i]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment