-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2json.ts
74 lines (67 loc) · 2.12 KB
/
convert2json.ts
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
import csv from 'csv-parser'
import fs from 'fs'
const enResults = []
const mmResults = []
fs.createReadStream('csv/Myanmar_Locations_Postal_Code_EN.csv')
.pipe(csv())
.on('data', (data) => {
const codes = data.postal_code.split('')
const postalcodeObject = {
...data,
...{
region_code: `${codes[0]}${codes[1]}`,
town_ship_code: `${codes[2]}${codes[3]}`,
qv_code: codes.length > 6 ? `${codes[4]}${codes[5]}${codes[6]}` : `${codes[4]}${codes[5]}`,
},
}
enResults.push(postalcodeObject)
})
.on('end', () => {})
fs.createReadStream('csv/Myanmar_Locations_Postal_Code_MM.csv')
.pipe(csv())
.on('data', (data) => {
const codes = data.postal_code.split('')
const postalcodeObject = {
...data,
...{
region_code: `${codes[0]}${codes[1]}`,
town_ship_code: `${codes[2]}${codes[3]}`,
qv_code: codes.length > 6 ? `${codes[4]}${codes[5]}${codes[6]}` : `${codes[4]}${codes[5]}`,
},
}
mmResults.push(postalcodeObject)
})
.on('end', () => {
const data = enResults.map((item, index) => {
return {
...{
en: {
region: item['region_name'],
town_township: item.town_township,
qv_tract: item.quarter_village_tract,
},
mm: {
region: mmResults[index]['region_name'],
town_township: mmResults[index].town_township,
qv_tract: mmResults[index].quarter_village_tract,
},
},
tsp_code: item.town_ship_code,
region_code: item.region_code,
postal_code: item.postal_code,
qv_code: item.qv_code,
}
})
const regionGrouped = groupBy(data, 'region_code')
Object.entries(regionGrouped).forEach((item) => {
regionGrouped[item[0]] = groupBy(regionGrouped[item[0]], 'tsp_code')
})
fs.writeFileSync('src/data.json', JSON.stringify(regionGrouped))
fs.writeFileSync('src/flat.json', JSON.stringify({ data: data }))
})
var groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
;(rv[x[key]] = rv[x[key]] || []).push(x)
return rv
}, {})
}