mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
Fix string methods to handle empty values (#4442)
* Enable strings category methods tests * Ensure escape, pad, padEnd, padStart, trim, trimEnd, trimStart, unescape return an empty string for falsey values * Coerce value to string using toString in truncate, capitalize and case methods * Ensure createCaseFirst returns an empty string for falsey values
This commit is contained in:
committed by
John-David Dalton
parent
abb54cc49a
commit
e51a424513
@@ -1,43 +0,0 @@
|
||||
import assert from 'assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { _, stubString } from './utils.js';
|
||||
|
||||
describe('"Strings" category methods', function() {
|
||||
var stringMethods = [
|
||||
'camelCase',
|
||||
'capitalize',
|
||||
'escape',
|
||||
'kebabCase',
|
||||
'lowerCase',
|
||||
'lowerFirst',
|
||||
'pad',
|
||||
'padEnd',
|
||||
'padStart',
|
||||
'repeat',
|
||||
'snakeCase',
|
||||
'toLower',
|
||||
'toUpper',
|
||||
'trim',
|
||||
'trimEnd',
|
||||
'trimStart',
|
||||
'truncate',
|
||||
'unescape',
|
||||
'upperCase',
|
||||
'upperFirst'
|
||||
];
|
||||
|
||||
lodashStable.each(stringMethods, function(methodName) {
|
||||
var func = _[methodName];
|
||||
|
||||
it('`_.' + methodName + '` should return an empty string for empty values', function() {
|
||||
var values = [, null, undefined, ''],
|
||||
expected = lodashStable.map(values, stubString);
|
||||
|
||||
var actual = lodashStable.map(values, function(value, index) {
|
||||
return index ? func(value) : func();
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
83
test/Strings-category-methods.test.js
Normal file
83
test/Strings-category-methods.test.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import assert from 'assert';
|
||||
import lodashStable from 'lodash';
|
||||
import { stubString } from './utils.js';
|
||||
|
||||
import camelCase from '../camelCase.js';
|
||||
import capitalize from '../capitalize.js';
|
||||
import escape from '../escape.js';
|
||||
import kebabCase from '../kebabCase.js';
|
||||
import lowerCase from '../lowerCase.js';
|
||||
import lowerFirst from '../lowerFirst.js';
|
||||
import pad from '../pad.js';
|
||||
import padEnd from '../padEnd.js';
|
||||
import padStart from '../padStart.js';
|
||||
import repeat from '../repeat.js';
|
||||
import snakeCase from '../snakeCase.js';
|
||||
import trim from '../trim.js';
|
||||
import trimStart from '../trimStart.js';
|
||||
import trimEnd from '../trimEnd.js';
|
||||
import truncate from '../truncate.js';
|
||||
import unescape from '../unescape.js';
|
||||
import upperCase from '../upperCase.js';
|
||||
import upperFirst from '../upperFirst';
|
||||
|
||||
|
||||
const methods = {
|
||||
camelCase,
|
||||
capitalize,
|
||||
escape,
|
||||
kebabCase,
|
||||
lowerCase,
|
||||
lowerFirst,
|
||||
pad,
|
||||
padEnd,
|
||||
padStart,
|
||||
repeat,
|
||||
snakeCase,
|
||||
trim,
|
||||
trimStart,
|
||||
trimEnd,
|
||||
truncate,
|
||||
unescape,
|
||||
upperCase,
|
||||
upperFirst
|
||||
}
|
||||
|
||||
|
||||
describe('"Strings" category methods', function() {
|
||||
var stringMethods = [
|
||||
'camelCase',
|
||||
'capitalize',
|
||||
'escape',
|
||||
'kebabCase',
|
||||
'lowerCase',
|
||||
'lowerFirst',
|
||||
'pad',
|
||||
'padEnd',
|
||||
'padStart',
|
||||
'repeat',
|
||||
'snakeCase',
|
||||
'trim',
|
||||
'trimEnd',
|
||||
'trimStart',
|
||||
'truncate',
|
||||
'unescape',
|
||||
'upperCase',
|
||||
'upperFirst'
|
||||
];
|
||||
|
||||
lodashStable.each(stringMethods, function(methodName) {
|
||||
var func = methods[methodName];
|
||||
|
||||
it('`_.' + methodName + '` should return an empty string for empty values', function() {
|
||||
var values = [, null, undefined, ''],
|
||||
expected = lodashStable.map(values, stubString);
|
||||
|
||||
var actual = lodashStable.map(values, function(value, index) {
|
||||
return index ? func(value) : func();
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user