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