You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if(typeofdata[subKey]==='object'&&data[subKey]!==null&&typeofdata[subKey].length==='undefined'&&_.keys(data[subKey]).length>0){// If we have another nested document
16
-
subKeys[indx]=retrieveSubHeading(newKey,data[subKey]);// Recur on the subdocument to retrieve the full key name
45
+
if(_.isObject(data[subKey])&&!_.isNull(data[subKey])&&_.isUndefined(data[subKey].length)&&_.keys(data[subKey]).length>0){// If we have another nested document
46
+
returngenerateSubHeading(newKey,data[subKey]);// Recur on the sub-document to retrieve the full key name
17
47
}else{
18
-
subKeys[indx]=(options.DELIMITER.WRAP||'')+(newKey||'')+(options.DELIMITER.WRAP||'');// Set the key name since we don't have a sub document
48
+
returnnewKey;// Set the key name since we don't have a sub document
19
49
}
20
50
});
21
-
returnsubKeys.join(options.DELIMITER.FIELD);// Return the headings joined by our field delimiter
22
-
};
23
51
24
-
// Retrieve the headings for all documents and return it. This checks that all documents have the same schema.
25
-
varretrieveHeading=function(data){
26
-
returnfunction(cb){// Returns a function that takes a callback - the function is passed to async.parallel
27
-
varkeys=_.keys(data);// Retrieve the current data keys
28
-
_.each(keys,function(key,indx){// for each key
29
-
if(typeofdata[key]==='object'){
30
-
// if the data at the key is a document, then we retrieve the subHeading starting with an empty string heading and the doc
31
-
keys[indx]=retrieveSubHeading('',data[key]);
32
-
}
33
-
});
34
-
// Retrieve the unique array of headings (keys)
35
-
keys=_.uniq(keys);
36
-
// If we have more than 1 unique list, then not all docs have the same schema - report an error
37
-
if(keys.length>1){thrownewError('Not all documents have the same schema.',keys);}
38
-
returncb(null,_.flatten(keys).join(options.DELIMITER.FIELD));// Return headings back
39
-
};
52
+
returnsubKeys;// Return the headings joined by our field delimiter
40
53
};
41
54
42
55
// Convert the given data with the given keys
43
56
varconvertData=function(data,keys){
44
57
varoutput=[],// Array of CSV representing converted docs
45
58
value;// Temporary variable to store the current data
46
-
_.each(keys,function(key,indx){// For each key
47
-
value=data[key];// Set the current data that we are looking at
48
-
if(keys.indexOf(key)>-1){// If the keys contain the current key, then process the data
49
-
if(_.isArray(value)){// We have an array of values
0 commit comments