dojox.data.CsvStore._getArrayOfArraysFromCsvFileContents
dojo.require("dojox.data.CsvStore");
defined in dojox/data/CsvStore.js
Usage
function (/*String*/ csvFileContents) (view source)
if(dojo.isString(csvFileContents)){ var lineEndingCharacters = new RegExp("\r\n|\n|\r"); var leadingWhiteSpaceCharacters = new RegExp("^\\s+",'g'); var trailingWhiteSpaceCharacters = new RegExp("\\s+$",'g'); var doubleQuotes = new RegExp('""','g'); var arrayOfOutputRecords = []; var arrayOfInputLines = csvFileContents.split(lineEndingCharacters); for(var i = 0; i < arrayOfInputLines.length; ++i){ var singleLine = arrayOfInputLines[i]; if(singleLine.length > 0){ var listOfFields = singleLine.split(','); var j = 0; while(j < listOfFields.length){ var space_field_space = listOfFields[j]; var field_space = space_field_space.replace(leadingWhiteSpaceCharacters, ''); // trim leading whitespace var field = field_space.replace(trailingWhiteSpaceCharacters, ''); // trim trailing whitespace var firstChar = field.charAt(0); var lastChar = field.charAt(field.length - 1); var secondToLastChar = field.charAt(field.length - 2); var thirdToLastChar = field.charAt(field.length - 3); if(field.length === 2 && field == "\"\""){ listOfFields[j] = ""; //Special case empty string field. }else if((firstChar == '"') && ((lastChar != '"') || ((lastChar == '"') && (secondToLastChar == '"') && (thirdToLastChar != '"')))){ if(j+1 === listOfFields.length){ // alert("The last field in record " + i + " is corrupted:\n" + field); return null; //null } var nextField = listOfFields[j+1]; listOfFields[j] = field_space + ',' + nextField; listOfFields.splice(j+1, 1); // delete element [j+1] from the list }else{ if((firstChar == '"') && (lastChar == '"')){ field = field.slice(1, (field.length - 1)); // trim the " characters off the ends field = field.replace(doubleQuotes, '"'); // replace "" with " } listOfFields[j] = field; j += 1; } } arrayOfOutputRecords.push(listOfFields); } } // The first item of the array must be the header row with attribute names. this._attributes = arrayOfOutputRecords.shift(); for(var i=0; i<this._attributes.length; i++){ // Store the index of each attribute this._attributeIndexes[this._attributes[i]] = i; } this._dataArray = arrayOfOutputRecords; //Array }
parameter | type | description |
---|---|---|
csvFileContents | String |
Examples
Example 1