forked from bengarvey/lineage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvToJson.coffee
99 lines (86 loc) · 2.44 KB
/
csvToJson.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
fs = require 'fs'
csvParse = require 'csv-parse'
async = require 'async'
console.log "Converting family..."
CSV_FILE = 'data/tree2.csv'
getPeople = (callback) ->
fs.readFile CSV_FILE, 'utf-8', (err, data) ->
console.log "Loaded csv data..."
csvParse data, {delimiter: ','}, (err, result) ->
people = convertArrayToObjectList(result)
people = addLastNames(people)
callback null, people
getNodeById = (nodes, id) ->
for node in nodes when node.id is id
return node
return -1
parseLinks = (nodes) ->
links = []
for node in nodes
console.log node
if node.spouseId? and node.spouseId isnt ''
links.push(getLink('spouse', node, getNodeById(nodes, +node.spouseId)))
if node.motherId? and node.motherId isnt ''
links.push(getLink('mother', node, getNodeById(nodes, +node.motherId)))
if node.fatherId? and node.fatherId isnt ''
links.push(getLink('father', node, getNodeById(nodes, +node.fatherId)))
console.log links[links.length-1]
return links
getLink = (type, source, target) ->
link =
source: source.id
target: target.id
type: type
color: getLinkColor(type)
getLinkColor = (type) ->
switch type
when 'spouse' then '#666'
when 'mother' then '#933'
when 'father' then '#39F'
else '#111'
addLastNames = (nodes) ->
for node in nodes
names = node.name.split(" ")
node.lastName = names[0].replace(/,/g,'')
return nodes
convertArrayToObjectList = (arr) ->
header = arr[0]
list = []
for index in [1..arr.length-1]
ob = {}
for headerIndex in [0..header.length-1]
ob[header[headerIndex]] = convertType(header[headerIndex], arr[index][headerIndex])
list.push ob
return list
convertType = (name, value) ->
if name is 'id' then +value
else if name is 'birthDate'
dates = value.split(":")
unless dates[1]?
dates[1] = 1
unless dates[2]?
dates[2] = 0
return new Date(dates[0], dates[1]-1, dates[2])
else if name is 'deadthDate' then new Date("#{value} 00")
else value
writeFile = (file, data) ->
fs.writeFile(file, data, (err) ->
if err
return console.log err
else
console.log "Done."
)
async.series([
getPeople
],
(err, result) ->
people = result[0]
links = parseLinks(people)
if err
console.log "Something went wrong: #{err}"
output =
nodes: people
links: links
writeFile('data/converted.json', JSON.stringify(output))
)
console.log "Complete!"