I have a service that formats strings in certain fields. Basically, when a user clicks out of the input box (on blur), the string is cleansed of illegal characters, and whitespace is replaced with commas. This is fine, but I would like to allow a user to add double quotes around grouped words. On blur, this should remove the quotes, but maintain the space in between the words, and then add a comma afterwards. I have tried everything but I can't get this to work. Here is how my service is currently set up:
angular.module('testApp')
.factory('formatStringService', [
function () {
return {
formatString: function (string) {
var styleStr = string;
if (styleStr === undefined) {
return;
}
styleStr = this.stringReplace(styleStr, '\n', ',');
styleStr = this.stringReplace(styleStr, '\t', ',');
styleStr = this.stringReplace(styleStr, ' ', ',');
styleStr = this.stringReplace(styleStr, ';', ',');
styleStr = this.newLine(styleStr);
styleStr = this.validated(styleStr);
for (var g = 0; g < 9; g++) {
styleStr = this.stringReplace(styleStr, ',,', ',');
}
if (styleStr.charAt(styleStr.length - 1) === ',') {
styleStr = styleStr.substr(0, (styleStr.length - 1));
}
if (styleStr.charAt(0) === '*') {
styleStr = styleStr.substr(1, (styleStr.length - 1));
}
if (styleStr.charAt(styleStr.length - 1) === '*') {
styleStr = styleStr.substr(0, (styleStr.length - 1));
}
return styleStr;
},
stringReplace: function (string, text, by) {
var strLength = string.length,
txtLength = text.length;
if ((strLength === 0) || (txtLength === 0)) {
return string;
}
var i = string.indexOf(text);
if ((!i) && (text !== string.substring(0, txtLength))) {
return string;
}
if (i === -1) {
return string;
}
var newstr = string.substring(0, i) + by;
if (i + txtLength < strLength) {
newstr += this.stringReplace(string.substring(i + txtLength, strLength), text, by);
}
return newstr;
},
validated: function (string) {
for (var i = 0, output = '', valid = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,~#+/\\*- '; i < string.length; i++) {
if (valid.indexOf(string.charAt(i)) !== -1) {
output += string.charAt(i);
}
}
return output;
},
newLine: function (string) {
for (var i = 0, output = ''; i < string.length; i++) {
if (string.charCodeAt(i) !== 10) {
output += string.charAt(i);
}
}
return output;
}
};
}
]);
Input string example: 1 2 3 "test test" 7 8
Should output: 1,2,3,test test,7,8