-
Notifications
You must be signed in to change notification settings - Fork 3
/
map.js
141 lines (121 loc) · 3.84 KB
/
map.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
var fs = require('fs')
, download = require('./download')
, forEachAsync = require('foreachasync').forEachAsync
//
, cities = { map: {}, arr: [] }
, states = { map: {}, arr: [] }
, sts = { map: {}, arr: [] }
, companies = { map: {}, arr: [] }
, links = { map: {}, arr: [] }
, carriers = { map: {}, arr: [] }
, types = { map: {}, arr: [] }
, carrierNames = { map: {}, arr: [] }
, s = ' '
, s2 = ' '
//, s = ''
//, s2 = ''
;
// NOTE these are not ideas, they will change with each update
function addThing(str, t) {
// match www. and no www.
if (!str || 'number' === typeof t.map[str]) {
str = str;
} else if ('number' === typeof t.map[str.replace(/\/\/www\./, '//')]) {
str = str.replace(/\/www\./, '/');
} else if ('number' === typeof t.map[str.replace(/\/\//, '//www.')]) {
str = str.replace(/\/\//, '//www.');
}
// account for falsey 0
if ('number' !== typeof t.map[str]) {
t.map[str] = t.arr.length;
t.arr.push(str);
}
return t.map[str];
}
function mapTheData(bigData) {
var mapData = {}
;
bigData.forEach(function (bigData) {
var area = bigData[0]
;
if (!mapData[area]) {
mapData[area] = [];
}
mapData[area].push(bigData.slice(1));
//mapData[area].push(bigData);
//bigData.shift();
});
Object.keys(mapData).forEach(function (areaCode) {
var list = mapData[areaCode]
;
list.forEach(function (d) {
d[0] = parseInt(d[0], 10); // prefix
d[1] = addThing(d[1], cities); // city name
d[2] = addThing(d[2], states); // state name
d[3] = addThing(d[3], sts); // state abbrev
d[4] = addThing(d[4], companies); // teclo registered
d[5] = addThing(d[5], types); // telco type
d[6] = addThing(d[6], links); // carrier link
d[7] = addThing(d[7], carriers); // carrier
d[8] = addThing(d[8], carrierNames); // carrier
});
});
writeDataFile(mapData);
}
function writeDataFile(mapData) {
var acWs
, mWs
, spacer = ' '
;
acWs = fs.createWriteStream('data.json');
mWs = fs.createWriteStream('meta.json');
console.log(Object.keys(mapData), 'area codes to write...');
acWs.write('{' + s + '"list":' + s + '{\n');
forEachAsync(Object.keys(mapData), function (next, areaCode, i) {
var list = mapData[areaCode]
, strs = []
, sp = ' '
;
acWs.write(spacer + '"' + areaCode + '":' + s + '[\n');
list.forEach(function (d, j) {
strs.push(sp + JSON.stringify(d) + '\n');
if (0 === j) {
sp = s2 + ',' + s;
}
});
acWs.write(strs.join('') + s2 + ']\n', function () {
console.log('wrote ' + strs.length + '...');
next();
});
if (0 === i) {
spacer = ',' + s;
}
}).then(function () {
acWs.on('close', function () {
console.log('wrote data.json');
});
mWs.on('close', function () {
console.log('wrote meta.json');
});
acWs.write(s2 + '}\n');
acWs.write('}\n', function () {
acWs.close();
});
mWs.write('{\n');
mWs.write( s2 + '"types":' + s + JSON.stringify(types.arr, null, s2) + '\n');
mWs.write(',' + s + '"states":' + s + JSON.stringify(states.arr, null, s2) + '\n');
mWs.write(',' + s + '"sts":' + s + JSON.stringify(sts.arr, null, s2) + '\n');
mWs.write(',' + s + '"cities":' + s + JSON.stringify(cities.arr, null, s2) + '\n');
mWs.write(',' + s + '"companies":' + s + JSON.stringify(companies.arr, null, s2) + '\n');
mWs.write(',' + s + '"links":' + s + JSON.stringify(links.arr, null, s2) + '\n');
mWs.write(',' + s + '"carriers":' + s + JSON.stringify(carriers.arr, null, s2) + '\n');
mWs.write(',' + s + '"carrierNames":' + s + JSON.stringify(carrierNames.arr, null, s2) + '\n');
mWs.write('}\n', function () {
mWs.close();
});
});
}
if (require.main === module) {
download.getSheet(mapTheData);
}