mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Remove overloaded options param support for _.trunc.
This commit is contained in:
21
lodash.js
21
lodash.js
@@ -10409,20 +10409,16 @@
|
|||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category String
|
* @category String
|
||||||
* @param {string} [string=''] The string to truncate.
|
* @param {string} [string=''] The string to truncate.
|
||||||
* @param {Object|number} [options] The options object or maximum string length.
|
* @param {Object} [options] The options object.
|
||||||
* @param {number} [options.length=30] The maximum string length.
|
* @param {number} [options.length=30] The maximum string length.
|
||||||
* @param {string} [options.omission='...'] The string to indicate text is omitted.
|
* @param {string} [options.omission='...'] The string to indicate text is omitted.
|
||||||
* @param {RegExp|string} [options.separator] The separator pattern to truncate to.
|
* @param {RegExp|string} [options.separator] The separator pattern to truncate to.
|
||||||
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
|
|
||||||
* @returns {string} Returns the truncated string.
|
* @returns {string} Returns the truncated string.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* _.trunc('hi-diddly-ho there, neighborino');
|
* _.trunc('hi-diddly-ho there, neighborino');
|
||||||
* // => 'hi-diddly-ho there, neighbo...'
|
* // => 'hi-diddly-ho there, neighbo...'
|
||||||
*
|
*
|
||||||
* _.trunc('hi-diddly-ho there, neighborino', 24);
|
|
||||||
* // => 'hi-diddly-ho there, n...'
|
|
||||||
*
|
|
||||||
* _.trunc('hi-diddly-ho there, neighborino', {
|
* _.trunc('hi-diddly-ho there, neighborino', {
|
||||||
* 'length': 24,
|
* 'length': 24,
|
||||||
* 'separator': ' '
|
* 'separator': ' '
|
||||||
@@ -10441,20 +10437,13 @@
|
|||||||
* // => 'hi-diddly-ho there, neig [...]'
|
* // => 'hi-diddly-ho there, neig [...]'
|
||||||
*/
|
*/
|
||||||
function trunc(string, options, guard) {
|
function trunc(string, options, guard) {
|
||||||
if (guard && isIterateeCall(string, options, guard)) {
|
|
||||||
options = undefined;
|
|
||||||
}
|
|
||||||
var length = DEFAULT_TRUNC_LENGTH,
|
var length = DEFAULT_TRUNC_LENGTH,
|
||||||
omission = DEFAULT_TRUNC_OMISSION;
|
omission = DEFAULT_TRUNC_OMISSION;
|
||||||
|
|
||||||
if (options != null) {
|
if (isObject(options)) {
|
||||||
if (isObject(options)) {
|
var separator = 'separator' in options ? options.separator : separator;
|
||||||
var separator = 'separator' in options ? options.separator : separator;
|
length = 'length' in options ? (+options.length || 0) : length;
|
||||||
length = 'length' in options ? (+options.length || 0) : length;
|
omission = 'omission' in options ? baseToString(options.omission) : omission;
|
||||||
omission = 'omission' in options ? baseToString(options.omission) : omission;
|
|
||||||
} else {
|
|
||||||
length = +options || 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
string = baseToString(string);
|
string = baseToString(string);
|
||||||
if (length >= string.length) {
|
if (length >= string.length) {
|
||||||
|
|||||||
16
test/test.js
16
test/test.js
@@ -14821,12 +14821,12 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should not truncate if `string` is <= `length`', 2, function() {
|
test('should not truncate if `string` is <= `length`', 2, function() {
|
||||||
strictEqual(_.trunc(string, string.length), string);
|
strictEqual(_.trunc(string, { 'length': string.length }), string);
|
||||||
strictEqual(_.trunc(string, string.length + 2), string);
|
strictEqual(_.trunc(string, { 'length': string.length + 2 }), string);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should truncate string the given length', 1, function() {
|
test('should truncate string the given length', 1, function() {
|
||||||
strictEqual(_.trunc(string, 24), 'hi-diddly-ho there, n...');
|
strictEqual(_.trunc(string, { 'length': 24 }), 'hi-diddly-ho there, n...');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should support a `omission` option', 1, function() {
|
test('should support a `omission` option', 1, function() {
|
||||||
@@ -14842,24 +14842,22 @@
|
|||||||
strictEqual(_.trunc(string, { 'length': 24, 'separator': /,? +/ }), 'hi-diddly-ho there...');
|
strictEqual(_.trunc(string, { 'length': 24, 'separator': /,? +/ }), 'hi-diddly-ho there...');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should treat negative `length` as `0`', 4, function() {
|
test('should treat negative `length` as `0`', 2, function() {
|
||||||
_.each([0, -2], function(length) {
|
_.each([0, -2], function(length) {
|
||||||
strictEqual(_.trunc(string, length), '...');
|
|
||||||
strictEqual(_.trunc(string, { 'length': length }), '...');
|
strictEqual(_.trunc(string, { 'length': length }), '...');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should coerce `length` to an integer', 8, function() {
|
test('should coerce `length` to an integer', 4, function() {
|
||||||
_.each(['', NaN, 4.5, '4'], function(length, index) {
|
_.each(['', NaN, 4.5, '4'], function(length, index) {
|
||||||
var actual = index > 1 ? 'h...' : '...';
|
var actual = index > 1 ? 'h...' : '...';
|
||||||
strictEqual(_.trunc(string, length), actual);
|
|
||||||
strictEqual(_.trunc(string, { 'length': { 'valueOf': _.constant(length) } }), actual);
|
strictEqual(_.trunc(string, { 'length': { 'valueOf': _.constant(length) } }), actual);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should coerce `string` to a string', 2, function() {
|
test('should coerce `string` to a string', 2, function() {
|
||||||
strictEqual(_.trunc(Object(string), 4), 'h...');
|
strictEqual(_.trunc(Object(string), { 'length': 4 }), 'h...');
|
||||||
strictEqual(_.trunc({ 'toString': _.constant(string) }, 5), 'hi...');
|
strictEqual(_.trunc({ 'toString': _.constant(string) }, { 'length': 5 }), 'hi...');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work as an iteratee for methods like `_.map`', 1, function() {
|
test('should work as an iteratee for methods like `_.map`', 1, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user