Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Port of Kohana3's Text::limit_chars helper
- String.implement({
- limitChars: function(limit, options){
- var opts = {
- endChar: null,
- preserveWords: false
- };
- $extend(opts, options);
- var endChar = (opts.endChar == null) ? '…' : opts.endChar;
- if (this.trim() === '' || this.length <= limit) return this;
- if (limit <= 0) return endChar;
- if (!opts.preserveWords) return this.substr(0, limit) + endChar;
- var pattern = new RegExp('^.{' + (limit-1) + '}\\S*');
- var matches = this.match(pattern);
- return matches[0].trim() + ((matches[0].length == this.length) ? '' : endChar);
- }
- });
Add Comment
Please, Sign In to add comment