Skip to content

Instantly share code, notes, and snippets.

@jejacks0n
Created May 14, 2011 01:13
Show Gist options
  • Save jejacks0n/971572 to your computer and use it in GitHub Desktop.
Save jejacks0n/971572 to your computer and use it in GitHub Desktop.
// 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+|^(&nbsp;)+/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+|^(&nbsp;)+/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