Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/protocol/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
KityMinder.registerProtocol('csv', function(minder) {

return {
fileDescription: 'KityMinder',
fileExtension: '.csv',
dataType: 'text',
mineType: 'text/csv',

encode: function(json) {
const SEP=','
const QUOTE='"'
const NEWLINE='\n'

var rootData = json.root
/*
* Recursive function to dig into JSON tree and append
* new columns every iteration, and copying the parents in
* previous columns.
* It calls itself every time children exists in JSON and retrieves the title.
*/
var recFlattenJSON = function (json,prefix){
if(json.data.text === undefined){
throw "Error in .KM Structure: missing Title!"
}
var newPrefix = prefix + (prefix ? SEP : "") + QUOTE + json.data.text.replace(/"/g, '\"') + QUOTE;
var result = ""
if(json.children === undefined || json.children.length == 0){
result = newPrefix + NEWLINE
}
else{
json.children.forEach(function (child){
result += recFlattenJSON(child, newPrefix)
})
}
return result
}
var output = recFlattenJSON(rootData, "")
return output
}
};
});