troubleshooting Question

JavaScript/jQuery comma separated lists to array

Avatar of nap0leon
nap0leon asked on
JavaScriptjQuery
8 Comments1 Solution3401 ViewsLast Modified:
I inherited a library that supports setting values for variables inside the library like this:
myLibrary.set('var1','var1_val')
A request has come along to update the .set() method so that it can support setting multiple variable-value pairs, like this:
myLibrary.set('var1,var2','var1_val,var2_val')
Easy enough... just split the "variables" and the "values" into arrays...

BUT sometimes a value is actually a comma-separated list of values, like this:
myLibrary.set('var1','var1_val1,var1_val2')
Which means the multiple variable-value pairs could look like this:
myLibrary.set('var1,var2','"var1_val1,var1_val2",var2_val1')
So splitting the values section on comma just got a bit more complicated...

Also, because different developers have different coding styles, the quotes in that line could be reversed, like this:
myLibrary.set("var1,var2","'var1_val1,var1_val2',var2_val1")
or even inconsistent between the variable list and the values lists, like this (double-quotes are the variable list but single-quotes around the values list):
myLibrary.set("var1,var2",'"var1_val1,var1_val2",var2_val1')
I have a function that seems to work, but I am not positive that it is 100%.

Here is the snippet of the .set() method where this new function is used to turn the variables and values into an array so that I can update the correct item in the JS database:
	variables = myLibrary.parseCommaList(variableList);
	values = myLibrary.parseCommaList(valueList);
	if (variables.length !== values.length) {
		throw myLibrary.error('', 'There must be an equal number of variables and values. # Variables = ' + variables.length + ', # Values = ' + values.length);
	}
	l = variables.length;
	for (i = 0; i < l; i++) {
		database[variables[i]] = values[i];			// use for obfuscated access
	}
For simplicity of seeking guidance, I've converted the method into a function:
function parseCommaList(list) {
	"use strict";
	var i = 0,				
		returnArray = [],
		j = 0,
		l = 0,
		quoteDelimiter = '',
		oldQuoteDelimiter = '',
		char = '';				
	if (typeof list !== 'string') {
		throw myLibrary.error('', 'Argument not a string in parseCommaList');
	}
	l = list.length;
	while (j < l) {
		char = list.substr(j,1);
		if (char === "'" || char === '"') {
			if (quoteDelimiter === char) {
				oldQuoteDelimiter = quoteDelimiter;
				quoteDelimiter = "";
				j ++;
			} else {
				oldQuoteDelimiter = "";
				quoteDelimiter = char;
				j ++;
				i = j;
			}
		} else if(char === ',') {
			if (quoteDelimiter === '') {
				returnArray.push(list.substring(i, j).replace(oldQuoteDelimiter, ''));
				i = j + 1;
			}
			j ++;
		} else {
			j ++;
		}
	}
	if (i != j) {
		returnArray.push(list.substring(i).replace("'","").replace('"',""));
	}
	return returnArray;	
};
Which can be tested like this:
For testing purposes, I'll use this to verify the values alerted are correct:
[code]
	var myList = '"a,b,c",d'
	var values = parseCommaList(myList)
	var l = values.length
	for (i = 0; i < l; i++) {
		alert(values[i]);
	}	

This version has an issue where the trailing quote is not removed, so I added a hack at the end of the function to remove any lingering quotes (line 38 above - the two replace()s).
e.g.,
For this line:
myLibrary.set('var1','a,b,c')
The function returns
a,b,c'
If anyone has an approach to doing this that works 100%, I'd love to see it.  The solution can use any combination of JavaScript and jQuery - as long as the function returns an array of items so I can perform my length verification and loop through them to assign values in the database.

A couple simple tests that the function should be able to pass:
list = "a,b,c"
list = '"a,b,c",d'
list = '"a,b,c","d"'
list = "'a,b,c',d"
list = "'a,b,c','d'"
list = "d,'a,b,c'"
list = "'d','a,b,c'"
list = 'd,"a,b,c"'
list = '"d","a,b,c"'

and one with a couple single-value values and a couple list-values:
list = "a,'b,c,d',e,'f,g,h'"
ASKER CERTIFIED SOLUTION
Jon Norman
Information Systems Manager
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros