Skip to content

Instantly share code, notes, and snippets.

@monsat
Last active October 3, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsat/2488671 to your computer and use it in GitHub Desktop.
Save monsat/2488671 to your computer and use it in GitHub Desktop.
wrap string

USAGE

    'your text'.wrap('[]');  // [your text]
    'your text'.wrap('<li></li>');  // <li>your text</li>
    'your text'.wrap('<li class="foo"><!-- sep --></li>', '<!-- sep -->');  // <li class="foo">your text</li>
    (1234).wrap('<li></li>');  // <li>1234</li>
(function(){
var wrap = function(wrapper, separator) {
if (!separator) {
var s = parseInt(wrapper.length / 2);
var wrappers = [wrapper.substr(0, s), wrapper.substr(s)];
} else {
var wrappers = wrapper.split(separator, 2);
}
return wrappers[0] + this + wrappers[1];
}
String.prototype.wrap = wrap;
Number.prototype.wrap = wrap;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment