String truncate To Width - Node.js String

Node.js examples for String:sub string

Description

String truncate To Width

Demo Code

    String.prototype.truncateToWidth = function g(B, p, s, C) {
        var w = 0;
        var z, v, D, y = undefined,
            t = undefined,/*  ww w  . j a  va  2s . c  o  m*/
            x = 0,
            q, o, u;
        if (!(C instanceof Function)) {
            C = String.prototype.truncateEnd
        }
        q = B / "i".cachedWidth(s);
        x = B / "W".cachedWidth(s);
        o = this.calculatedWidth(s);
        D = z = this;
        u = o < B;
        while (!u) {
            w++;
            if ((x === y && q === t) || o == B) {
                u = true;
                continue
            }
            y = x;
            t = q;
            v = Math.floor((x + q) / 2);
            D = C.call(z, v, p);
            o = D.calculatedWidth(s);
            if (o < B) {
                x = v
            } else {
                q = v
            }
        }
        return D
    };
})();

Related Tutorials