angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

limitTo filter should accept strings as well

Open vojtajina opened this issue 15 years ago • 6 comments

Currently we support only arrays...

vojtajina avatar Nov 14 '11 19:11 vojtajina

Do you mean something like this: 'abcde'.$limitTo(3) => 'abc' ? I can take care of that :)

Codier avatar Nov 14 '11 19:11 Codier

Well, yes... but wait until Misko's injector is merged, as we changed this a bit... We don't augment the types anymore and limitTo is a filter instead...

vojtajina avatar Nov 15 '11 00:11 vojtajina

sure

Codier avatar Nov 15 '11 01:11 Codier

should be in master later today

mhevery avatar Nov 15 '11 04:11 mhevery

wow, you are still working

Codier avatar Nov 15 '11 04:11 Codier

To limit string size I use the following custom filter, you might be interested in:

    /**
     * Usage:
     *   {{some_text | cut:true:100:' ...'}}
     * Options:
     *   - wordwise (boolean) - if true, cut only by words bounds,
     *   - max (integer) - max length of the text, cut to this number of chars,
     *   - tail (string, default: ' …') - add this string to the input
     *     string if the string was cut.
     */
    angular.module('ng').filter('cut', function () {
        return function (value, wordwise, max, tail) {
            if (!value) return '';

            max = parseInt(max, 10);
            if (!max) return value;
            if (value.length <= max) return value;

            value = value.substr(0, max);
            if (wordwise) {
                var lastspace = value.lastIndexOf(' ');
                if (lastspace != -1) {
                    value = value.substr(0, lastspace);
                }
            }

            return value + (tail || ' …');
        };
    });

kstep avatar Jan 20 '12 00:01 kstep