trunc.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var baseToString = require('../internal/baseToString'),
  2. isIterateeCall = require('../internal/isIterateeCall'),
  3. isObject = require('../lang/isObject'),
  4. isRegExp = require('../lang/isRegExp');
  5. /** Used as default options for `_.trunc`. */
  6. var DEFAULT_TRUNC_LENGTH = 30,
  7. DEFAULT_TRUNC_OMISSION = '...';
  8. /** Used to match `RegExp` flags from their coerced string values. */
  9. var reFlags = /\w*$/;
  10. /**
  11. * Truncates `string` if it's longer than the given maximum string length.
  12. * The last characters of the truncated string are replaced with the omission
  13. * string which defaults to "...".
  14. *
  15. * @static
  16. * @memberOf _
  17. * @category String
  18. * @param {string} [string=''] The string to truncate.
  19. * @param {Object|number} [options] The options object or maximum string length.
  20. * @param {number} [options.length=30] The maximum string length.
  21. * @param {string} [options.omission='...'] The string to indicate text is omitted.
  22. * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
  23. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  24. * @returns {string} Returns the truncated string.
  25. * @example
  26. *
  27. * _.trunc('hi-diddly-ho there, neighborino');
  28. * // => 'hi-diddly-ho there, neighbo...'
  29. *
  30. * _.trunc('hi-diddly-ho there, neighborino', 24);
  31. * // => 'hi-diddly-ho there, n...'
  32. *
  33. * _.trunc('hi-diddly-ho there, neighborino', {
  34. * 'length': 24,
  35. * 'separator': ' '
  36. * });
  37. * // => 'hi-diddly-ho there,...'
  38. *
  39. * _.trunc('hi-diddly-ho there, neighborino', {
  40. * 'length': 24,
  41. * 'separator': /,? +/
  42. * });
  43. * // => 'hi-diddly-ho there...'
  44. *
  45. * _.trunc('hi-diddly-ho there, neighborino', {
  46. * 'omission': ' [...]'
  47. * });
  48. * // => 'hi-diddly-ho there, neig [...]'
  49. */
  50. function trunc(string, options, guard) {
  51. if (guard && isIterateeCall(string, options, guard)) {
  52. options = undefined;
  53. }
  54. var length = DEFAULT_TRUNC_LENGTH,
  55. omission = DEFAULT_TRUNC_OMISSION;
  56. if (options != null) {
  57. if (isObject(options)) {
  58. var separator = 'separator' in options ? options.separator : separator;
  59. length = 'length' in options ? (+options.length || 0) : length;
  60. omission = 'omission' in options ? baseToString(options.omission) : omission;
  61. } else {
  62. length = +options || 0;
  63. }
  64. }
  65. string = baseToString(string);
  66. if (length >= string.length) {
  67. return string;
  68. }
  69. var end = length - omission.length;
  70. if (end < 1) {
  71. return omission;
  72. }
  73. var result = string.slice(0, end);
  74. if (separator == null) {
  75. return result + omission;
  76. }
  77. if (isRegExp(separator)) {
  78. if (string.slice(end).search(separator)) {
  79. var match,
  80. newEnd,
  81. substring = string.slice(0, end);
  82. if (!separator.global) {
  83. separator = RegExp(separator.source, (reFlags.exec(separator) || '') + 'g');
  84. }
  85. separator.lastIndex = 0;
  86. while ((match = separator.exec(substring))) {
  87. newEnd = match.index;
  88. }
  89. result = result.slice(0, newEnd == null ? end : newEnd);
  90. }
  91. } else if (string.indexOf(separator, end) != end) {
  92. var index = result.lastIndexOf(separator);
  93. if (index > -1) {
  94. result = result.slice(0, index);
  95. }
  96. }
  97. return result + omission;
  98. }
  99. module.exports = trunc;