I wanted to pluralize the string. I have written the function which can accept the single string or array of strings and return the pluralized one.
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;
}
As in case if the input is not an array, i have to create one, which i am thinking is wrong. Can this code be optimized?