Skip to content

Instantly share code, notes, and snippets.

@christoomey
Created November 15, 2012 01:00
Show Gist options
  • Select an option

  • Save christoomey/4075956 to your computer and use it in GitHub Desktop.

Select an option

Save christoomey/4075956 to your computer and use it in GitHub Desktop.
Compiled js version of spanWrapper utility
// Compiled js version of spanWrapper library written in coffeescript.
// Source code can be found in https://gist.github.com/4075843
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
window.CJT || (window.CJT = {});
CJT.SpanWrapper = (function() {
SpanWrapper.prototype.STRING_NOT_FOUND = -1;
function SpanWrapper(options) {
var _ref, _ref1;
if (options == null) {
options = {};
}
this._splitAround = __bind(this._splitAround, this);
this._closeTag = __bind(this._closeTag, this);
this._openTag = __bind(this._openTag, this);
this._wrapped = __bind(this._wrapped, this);
this._normalize = __bind(this._normalize, this);
this._contains = __bind(this._contains, this);
this._splitAtIndex = __bind(this._splitAtIndex, this);
this._wrapMatches = __bind(this._wrapMatches, this);
this._splitMatchSegments = __bind(this._splitMatchSegments, this);
this.wrap = __bind(this.wrap, this);
this.tagname = (_ref = options.tagname) != null ? _ref : 'span';
this.classname = (_ref1 = options.classname) != null ? _ref1 : '';
}
SpanWrapper.prototype.wrap = function(target, searchString) {
var matchSegments, normalizedSearchString;
if (searchString === '') {
return target;
}
normalizedSearchString = this._normalize(searchString);
matchSegments = this._splitMatchSegments(target, normalizedSearchString);
if (matchSegments === this.STRING_NOT_FOUND) {
return target;
} else {
return this._wrapMatches(matchSegments);
}
};
SpanWrapper.prototype._splitMatchSegments = function(target, searchString) {
var index, leftoverSearch, rangeFromSearchStringLengthToZero, searchTestSegment, splitEnd, splitTarget, unmatchedEnd, _i, _j, _len, _ref, _ref1, _results;
rangeFromSearchStringLengthToZero = (function() {
_results = [];
for (var _i = _ref = searchString.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; _ref <= 0 ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this);
for (_j = 0, _len = rangeFromSearchStringLengthToZero.length; _j < _len; _j++) {
index = rangeFromSearchStringLengthToZero[_j];
_ref1 = this._splitAtIndex(searchString, index), searchTestSegment = _ref1[0], leftoverSearch = _ref1[1];
if (this._contains(target, searchTestSegment)) {
splitTarget = this._splitAround(target, searchTestSegment);
if (leftoverSearch === '') {
unmatchedEnd = {
start: splitTarget.end,
match: ""
};
return [splitTarget].concat(unmatchedEnd);
} else {
splitEnd = this._splitMatchSegments(splitTarget.end, leftoverSearch);
if (splitEnd === this.STRING_NOT_FOUND) {
continue;
} else {
return [splitTarget].concat(splitEnd);
}
}
}
}
return this.STRING_NOT_FOUND;
};
SpanWrapper.prototype._wrapMatches = function(matchSegments) {
var segment, wrappedMatch, wrappedTarget, _i, _len;
wrappedTarget = '';
for (_i = 0, _len = matchSegments.length; _i < _len; _i++) {
segment = matchSegments[_i];
wrappedMatch = this._wrapped(segment.match);
wrappedTarget += segment.start + wrappedMatch;
}
return wrappedTarget;
};
SpanWrapper.prototype._splitAtIndex = function(string, zeroBasedIndex) {
var end, start;
start = string.slice(0, +zeroBasedIndex + 1 || 9e9);
end = string.slice(zeroBasedIndex + 1, +(string.length - 1) + 1 || 9e9);
return [start, end];
};
SpanWrapper.prototype._contains = function(target, substring) {
return target.toLowerCase().indexOf(substring) >= 0;
};
SpanWrapper.prototype._normalize = function(string) {
return string.toLowerCase().replace(/\s+/g, '');
};
SpanWrapper.prototype._wrapped = function(core) {
if (core === '') {
return '';
}
return this._openTag() + core + this._closeTag();
};
SpanWrapper.prototype._openTag = function() {
if (this.classname) {
return "<" + this.tagname + " class='" + this.classname + "'>";
} else {
return "<" + this.tagname + ">";
}
};
SpanWrapper.prototype._closeTag = function() {
return "</" + this.tagname + ">";
};
SpanWrapper.prototype._splitAround = function(target, substring) {
var downcasedTarget, end, index, match, start;
downcasedTarget = target.toLowerCase();
index = downcasedTarget.indexOf(substring);
start = target.substring(0, index);
match = target.substring(index, index + substring.length);
end = target.substring(index + substring.length, target.length);
return {
start: start,
match: match,
end: end
};
};
return SpanWrapper;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment