Security update to Drupal 8.4.6
[yaffs-website] / node_modules / underscore.string / prune.js
1 /**
2  * _s.prune: a more elegant version of truncate
3  * prune extra chars, never leaving a half-chopped word.
4  * @author github.com/rwz
5  */
6 var makeString = require('./helper/makeString');
7 var rtrim = require('./rtrim');
8
9 module.exports = function prune(str, length, pruneStr) {
10   str = makeString(str);
11   length = ~~length;
12   pruneStr = pruneStr != null ? String(pruneStr) : '...';
13
14   if (str.length <= length) return str;
15
16   var tmpl = function(c) {
17     return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' ';
18   },
19     template = str.slice(0, length + 1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'
20
21   if (template.slice(template.length - 2).match(/\w\w/))
22     template = template.replace(/\s*\S+$/, '');
23   else
24     template = rtrim(template.slice(0, template.length - 1));
25
26   return (template + pruneStr).length > str.length ? str : str.slice(0, template.length) + pruneStr;
27 };