我想复数字符串。我编写了可以接受单个字符串或字符串数组的函数,并返回复数字符串。
pluralizeTextArray: function(strArray) {
var isNotArray = !_.isArray(strArray),
temp = [];
if (isNotArray) {
temp[0] = strArray;
strArray = temp;
}
var length = strArray.length,
pluralizedArr = [],
vowels = ['a','e','i','o','u'],
lastChar = "",
suffix = "s",
str = "";
for (var i = 0; i < length; i++) {
str = strArray[i];
lastChar = str[str.length - 1];
switch (lastChar) {
case 's':
if (!str.endsWith('ies')) {
suffix = 'es';
}
break;
case 'y':
//ends in [consonant]y - add ies
//ends in [vowel]y - add 's' ,
//exception string ending with 'quy'
if ($.inArray(str[str.length - 2], vowels) === -1 ||
strObj.endsWith('quy')) {
str = str = _.initial(str).join(""); //removed last character
suffix = 'ies';
}
break;
}
pluralizedArr[i] = str+suffix;
}
retrun isNotArray ? pluralizedArr[0] : pluralizedArr;
}
如果输入不是数组,我必须创建一个,我认为这是错误的。这段代码可以优化吗?