Sha256: 1c4ffd3a57e20e0d00b9aa2412f65cb7f177dd168156635e9732e746de6bd2be

Contents?: true

Size: 908 Bytes

Versions: 19

Compression:

Stored size: 908 Bytes

Contents

/**
 * _s.prune: a more elegant version of truncate
 * prune extra chars, never leaving a half-chopped word.
 * @author github.com/rwz
 */
var makeString = require('./helper/makeString');
var rtrim = require('./rtrim');

module.exports = function prune(str, length, pruneStr) {
  str = makeString(str);
  length = ~~length;
  pruneStr = pruneStr != null ? String(pruneStr) : '...';

  if (str.length <= length) return str;

  var tmpl = function(c) {
      return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' ';
    },
    template = str.slice(0, length + 1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'

  if (template.slice(template.length - 2).match(/\w\w/))
    template = template.replace(/\s*\S+$/, '');
  else
    template = rtrim(template.slice(0, template.length - 1));

  return (template + pruneStr).length > str.length ? str : str.slice(0, template.length) + pruneStr;
};

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/underscore.string/prune.js
ela-4.1.5 node_modules/underscore.string/prune.js
ela-4.1.4 node_modules/underscore.string/prune.js
ela-4.1.3 node_modules/underscore.string/prune.js
ela-4.1.2 node_modules/underscore.string/prune.js
ela-4.1.1 node_modules/underscore.string/prune.js
ela-4.1.0 node_modules/underscore.string/prune.js
ela-4.0.0 node_modules/underscore.string/prune.js
ela-3.4.3 node_modules/underscore.string/prune.js
ela-3.4.2 node_modules/underscore.string/prune.js
ela-3.4.0 node_modules/underscore.string/prune.js
ela-3.3.1 node_modules/underscore.string/prune.js
ela-3.3.0 node_modules/underscore.string/prune.js
ela-3.2.0 node_modules/underscore.string/prune.js
ela-3.1.1 node_modules/underscore.string/prune.js
ela-3.1.0 node_modules/underscore.string/prune.js
ela-3.0.0 node_modules/underscore.string/prune.js
ela-2.0.0 node_modules/underscore.string/prune.js
ela-1.1.0 node_modules/underscore.string/prune.js