Skip to content

Instantly share code, notes, and snippets.

@AnatolyUA
Last active December 14, 2015 11:50
Show Gist options
  • Save AnatolyUA/5082344 to your computer and use it in GitHub Desktop.
Save AnatolyUA/5082344 to your computer and use it in GitHub Desktop.

Revisions

  1. AnatolyUA revised this gist Mar 14, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions jquery.cuttext.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    (function($){
    function cutAt(text, min, max) {
    if (min === 0) return "";
    if(min && text.length > min){
    min = min || 0;
    if (min == 0) return "";
    if(text.length > min){
    if (max) {
    var BEST = ".!?",
    GOOD = ",;",
  2. AnatolyUA revised this gist Mar 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.cuttext.js
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,6 @@
    };

    jQuery.cutText = function(text, min, max){
    return cutAt($("<div>" + text + "</div>").text(), min, max);
    return cutAt($('<div/>').html(text).text(), min, max);
    };
    })(jQuery);
  3. AnatolyUA revised this gist Mar 12, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jquery.cuttext.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    (function($){
    function cutAt(text, min, max) {
    if (min === 0) return "";
    if(min && text.length > min){
    if (max) {
    var BEST = ".!?",
  4. AnatolyUA created this gist Mar 4, 2013.
    39 changes: 39 additions & 0 deletions jquery.cuttext.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    (function($){
    function cutAt(text, min, max) {
    if(min && text.length > min){
    if (max) {
    var BEST = ".!?",
    GOOD = ",;",
    SPACE = " ",
    HELLIP = "&hellip;";

    text = text.trim();
    var n = max;
    for (; BEST.indexOf(text[n]) === -1 && n > min; n--){}
    if (n != min || BEST.indexOf(text[n]) !== -1) return text.substr(0, n + 1);

    n = max;
    for (; GOOD.indexOf(text[n]) === -1 && n > min; n--){}
    if (n != min || GOOD.indexOf(text[n]) !== -1) return text.substr(0, n) + HELLIP;

    n = max;
    for (; SPACE.indexOf(text[n]) === -1 && n > min; n--){}
    if (n != min || SPACE.indexOf(text[n]) !== -1) return text.substr(0, n) + HELLIP;

    return text.substr(0, max) + HELLIP;

    } else {
    return text.substr(0, min) + HELLIP;
    }
    }
    return text;
    }

    $.fn.cutText = function(min, max) {
    return cutAt(this.text(), min, max);
    };

    jQuery.cutText = function(text, min, max){
    return cutAt($("<div>" + text + "</div>").text(), min, max);
    };
    })(jQuery);