Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatadata.js
More file actions
647 lines (581 loc) · 18.6 KB
/
Copy pathdatadata.js
File metadata and controls
647 lines (581 loc) · 18.6 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
},{}],2:[function(require,module,exports){
/*! datadata.js © 2014-2015 Florian Ledermann
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
// test whether in a browser environment
if (typeof window === 'undefined') {
// node
var d3dsv = require('d3-dsv');
var fs = require('fs');
var fileparser = function(func) {
return function(path, row, callback) {
if (dd.isUndefined(callback)) {
callback = row;
row = null;
}
fs.readFile(path, 'utf8', function(error, data) {
if (error) return callback(error);
data = func(data, row);
callback(null,data);
});
};
};
var d3 = {
csv: fileparser(d3dsv.csv.parse),
tsv: fileparser(d3dsv.tsv.parse),
json: fileparser(JSON.parse)
};
} else {
// browser
// we expect global d3 to be available
var d3 = window.d3;
}
function rowFileHandler(loader) {
// TODO: file handler API should not need to be passed map, reduce functions but be wrapped externally
return function(path, map, reduce, options) {
options = dd.merge({
// default accessor function tries to convert number-like strings to numbers
accessor: function(d) {
var keys = Object.keys(d);
for (var i=0; i<keys.length; i++) {
var key = keys[i],
val = d[key];
// CSV doesn't support specification of null values
// interpret empty field values as missing
if (val === "") {
d[key] = null;
}
else if (dd.isNumeric(val)) {
// unary + converts both ints and floats correctly
d[key] = +val;
}
}
return d;
}
}, options);
return new Promise(function(resolve, reject) {
loader(path, options.accessor,
function(error, data) {
if (error) {
reject(error);
return;
}
resolve(dd.mapreduce(data, map, reduce));
}
);
});
};
}
function jsonFileHandler(path, map, reduce) {
return new Promise(function(resolve, reject) {
d3.json(path, function(error, data) {
if (error) {
reject(error);
return;
}
if (dd.isArray(data)) {
resolve(dd.mapreduce(data, map, reduce));
}
else {
// object - treat entries as keys by default
var keys = Object.keys(data);
var map_func;
if (!map) {
// use keys as data to emit key/data pairs in map step!
map_func = dd.map.dict(data);
}
else {
map_func = function(k, emit) {
// put original key into object
var v = data[k];
v.__key__ = k;
// call user-provided map funtion with object
map(v, emit);
};
}
resolve(dd.mapreduce(keys, map_func, reduce));
}
});
});
}
var fileHandlers = {
'csv': rowFileHandler(d3.csv),
'tsv': rowFileHandler(d3.tsv),
'json': jsonFileHandler
};
var getFileHandler = function(pathOrExt) {
// guess type
var ext = pathOrExt.split('.').pop().toLowerCase();
return fileHandlers[ext] || null;
};
var registerFileHandler = function(ext, handler) {
fileHandlers[ext] = handler;
};
// TODO: register .topojson, .geojson in mapmap.js
/**
Datadata - a module for loading and processing data.
You can call the module as a function to create a promise for data from a URL, Function or Array.
Returns a promise for data for everything.
@param {(string|function|Array)} spec - A String (URL), Function or Array of data.
@param {(function|string)} [map={@link datadata.map.dict}] - The map function for map/reduce.
@param {(string)} [reduce=datadata.emit.last] - The reduce function for map/reduce.
@exports module:datadata
*/
var dd = function(spec, map, reduce, options) {
// options
// type: override file extension, e.g. for API urls (e.g. 'csv')
// fileHandler: manually specify file handler to be used to load & parse file
options = options || {};
if (spec == null) throw new Error("datadata.js: No data specification.");
if (map && !dd.isFunction(map)) {
// map is string -> map to attribute value
map = dd.map.key(map);
}
if (dd.isString(spec)) {
// consider spec to be a URL/file to load
var handler = options.fileHandler || getFileHandler(options.type || spec);
if (handler) {
return handler(spec, map, reduce, options);
}
else {
throw new Error("datadata.js: Unknown file type for: " + spec);
}
}
if (dd.isArray(spec)) {
return new Promise(function(resolve, reject) {
resolve(dd.mapreduce(spec, map, reduce));
});
}
throw new Error("datadata.js: Unknown data specification.");
};
// expose registration method & rowFileHandler helper
dd.registerFileHandler = registerFileHandler;
dd.rowFileHandler = rowFileHandler;
// simple load function, returns a promise for data without map/reduce-ing
// DO NOT USE - present only for mapmap.js legacy reasons
dd.load = function(spec, key) {
if (spec.then && typeof spec.then === 'function') {
// already a thenable / promise
return spec;
}
else if (dd.isString(spec)) {
// consider spec to be a URL to load
// guess type
var ext = spec.split('.').pop();
if (ext == 'json' || ext == 'topojson' || ext == 'geojson') {
return new Promise(function(resolve, reject) {
d3.json(spec, function(error, data) {
if (error) {
reject(error);
return;
}
resolve(data);
});
});
}
else {
console.warn("Unknown extension: " + ext);
}
}
};
// Type checking
/**
Return true if argument is a string.
@param {any} val - The value to check.
*/
dd.isString = function (val) {
return Object.prototype.toString.call(val) == '[object String]';
};
/**
Return true if argument is a function.
@param {any} val - The value to check.
*/
dd.isFunction = function(obj) {
return (typeof obj === 'function');
};
/**
Return true if argument is an Array.
@param {any} val - The value to check.
*/
dd.isArray = function(obj) {
return (obj instanceof Array);
};
/**
Return true if argument is an Object, but not an Array, String or anything created with a custom constructor.
@param {any} val - The value to check.
*/
dd.isDictionary = function(obj) {
return (obj && obj.constructor && obj.constructor === Object);
};
/**
Return true if argument is undefined.
@param {any} val - The value to check.
*/
dd.isUndefined = function(obj) {
return (typeof obj == 'undefined');
};
/**
Return true if argument is a number or a string that strictly looks like a number.
This method is stricter than +val or parseInt(val) as it doesn't validate the empty
string or strings contining any non-numeric characters.
@param {any} val - The value to check.
*/
dd.isNumeric = function(val) {
// check if string looks like a number
// +"" => 0
// parseInt("") => NaN
// parseInt("123OK") => 123
// +"123OK" => NaN
// so we need to pass both to be strict
return !isNaN(+val) && !isNaN(parseFloat(val));
}
// Type conversion / utilities
/**
If the argument is already an Array, return a copy of the Array.
Else, return a single-element Array containing the argument.
*/
dd.toArray = function(val) {
if (!val) return [];
// return a copy if aready array, else single-element array
return dd.isArray(val) ? val.slice() : [val];
};
/**
Shallow object merging, mainly for options. Returns a new object.
*/
dd.merge = function() {
var obj = {};
for (var i = 0; i < arguments.length; i++) {
var src = arguments[i];
for (var key in src) {
if (src.hasOwnProperty(key)) {
obj[key] = src[key];
}
}
}
return obj;
};
/**
Return an {@link module:datadata.OrderedHash|OrderedHash} object.
@exports module:datadata.OrderedHash
*/
dd.OrderedHash = function() {
// ordered hash implementation
var keys = [];
var vals = {};
return {
/**
Add a key/value pair to the end of the OrderedHash.
@param {String} k - Key
@param v - Value
*/
push: function(k,v) {
if (!vals[k]) keys.push(k);
vals[k] = v;
},
/**
Insert a key/value pair at the specified position.
@param {Number} i - Index to insert value at
@param {String} k - Key
@param v - Value
*/
insert: function(i,k,v) {
if (!vals[k]) {
keys.splice(i,0,k);
vals[k] = v;
}
},
/**
Return the value for specified key.
@param {String} k - Key
*/
get: function(k) {
// string -> key
return vals[k];
},
/**
Return the value at specified index position.
@param {String} i - Index
*/
at: function(i) {
// number -> nth object
return vals[keys[i]];
},
length: function(){return keys.length;},
keys: function(){return keys;},
key: function(i) {return keys[i];},
values: function() {
return keys.map(function(key){return vals[key];});
},
map: function(func) {
return keys.map(function(k){return func(k, vals[k]);});
},
unsorted_dict: function() {
return vals;
}
};
};
// Utility functions for map/reduce
dd.map = {
key: function(attr, remap) {
return function(d, emit) {
var key = d[attr];
if (remap && remap[key] !== undefined) {
key = remap[key];
}
emit(key, d);
};
},
dict: function(dict) {
return function(d, emit) {
emit(d, dict[d]);
};
}
};
dd.emit = {
ident: function() {
return function(key, values, emit) {
emit(key, values);
};
},
first: function() {
return function(key, values, emit) {
emit(key, values[0]);
};
},
last: function() {
return function(key, values, emit) {
emit(key, values[values.length - 1]);
};
},
merge: function() {
return function(key, values, emit) {
var obj = values.reduce(function(prev, curr) {
var keys = Object.keys(curr);
for (var i=0; i<keys.length; i++) {
var k = keys[i];
prev[k] = curr[k];
}
return prev;
});
emit(key, obj);
};
},
toAttr: function(attr, func) {
func = func || dd.emit.last();
return function(key, values, emit) {
func(key, values, function(k, v) {
var obj = {};
obj[attr] = v;
emit(k, obj);
});
};
},
sum: function(include, exclude) {
include = wildcards(include || '*');
exclude = wildcards(exclude);
return function(key, values, emit) {
var obj = values.reduce(function(prev, curr) {
var keys = Object.keys(curr);
for (var i=0; i<keys.length; i++) {
var key = keys[i],
doAdd = false,
j;
for (j=0; j<include.length; j++) {
if (key.search(include[i]) > -1) {
doAdd = true;
break;
}
}
for (j=0; j<exclude.length; j++) {
if (key.search(include[j]) > -1) {
doAdd = false;
break;
}
}
if (doAdd && prev[key] && curr[key] && !isNaN(prev[key]) && !isNaN(curr[key])) {
prev[key] = prev[key] + curr[key];
}
else {
prev[key] = curr[key];
if (doAdd) {
console.warn("datadata.emit.sum(): Cannot add keys " + key + "!");
}
}
}
return prev;
});
emit(key, obj);
};
}
};
dd.map.geo = {
point: function(latProp, lonProp, keyProp) {
var id = 0;
return function(d, emit) {
var key = keyProp ? d[keyProp] : id++;
emit(key, dd.geo.Point(d[lonProp], d[latProp], d));
};
}
};
dd.emit.geo = {
segments: function() {
return function(key, data, emit) {
var prev = null, cur = null;
for (var i=0; i<data.length; i++) {
cur = data[i];
if (prev) {
emit(key + '-' + i, dd.geo.LineString([[prev.lon,prev.lat],[cur.lon,cur.lat]], prev));
}
prev = cur;
}
};
}
};
// constructors for GeoJSON objects
dd.geo = {
Point: function(lon, lat, properties) {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [lon, lat]
},
properties: properties
};
},
LineString: function(coordinates, properties) {
return {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: coordinates
},
properties: properties
};
}
};
function wildcards(spec) {
spec = dd.toArray(spec);
for (var i=0; i<spec.length; i++) {
if (!(spec[i] instanceof RegExp)) {
spec[i] = new RegExp('^' + spec[i].replace('*','.*').replace('?','.'));
}
}
return spec;
}
// https://code.google.com/p/mapreduce-js/
// Mozilla Public License
dd.mapreduce = function (data, map, reduce) {
var mapResult = [],
reduceResult = dd.OrderedHash(),
reduceKey;
reduce = reduce || dd.emit.last(); // default
var mapEmit = function(key, value) {
if (key == null) return; // do not emit if key is null or undefined
if(!mapResult[key]) {
mapResult[key] = [];
}
mapResult[key].push(value);
};
var reduceEmit = function(key, value) {
reduceResult.push(key, value);
};
for(var i = 0; i < data.length; i++) {
map(data[i], mapEmit);
}
for(reduceKey in mapResult) {
reduce(reduceKey, mapResult[reduceKey], reduceEmit);
}
return reduceResult;
};
dd.mapreducer = function(map, reduce) {
return function(data) {
dd.mapreduce(data, map, reduce);
};
};
// Helper functions for map etc.
// put 'd' in another object using the attribute 'key'
// optional 'pull' is the name of a key to leave on the top level
dd.envelope = function(key, pull, func) {
return function(d) {
if (pull && typeof pull == 'function') {
// envelope(key, func) case
func = pull;
pull = null;
}
if (func) d = func(d);
var val = {};
val[key] = d;
if (pull) {
val[pull] = d[pull];
delete d[pull];
}
return val;
};
};
dd.prefix = function(prefix, func) {
return function(d) {
if (func) d = func(d);
var val = {},
keys = Object.keys(d);
for (var i=0; i<keys.length; i++) {
val[prefix + keys[i]] = d[keys[i]];
}
return val;
};
};
dd.prefix_attr = function(attr, func) {
return function(d) {
if (func) d = func(d);
var val = {},
keys = Object.keys(d),
prefix = d[attr] ? d[attr] + '_' : '';
for (var i=0; i<keys.length; i++) {
val[prefix + keys[i]] = d[keys[i]];
}
return val;
};
};
dd.map_attr = function(map, func) {
return function(d) {
if (func) d = func(d);
if (typeof map == 'function') {
d = map(d);
}
else {
var keys = Object.keys(map);
for (var i=0; i<keys.length; i++) {
var key = keys[i];
var val = map[key];
if (typeof val == 'function') {
d[key] = val(d);
}
else if (d[val]) {
d[key] = d[val];
delete d[val];
}
}
}
return d;
};
};
dd.reverse = function(data) {
if (data.slice && typeof data.slice == 'function') {
// slice() = copy
return data.slice().reverse();
}
return data;
};
module.exports = dd;
},{"d3-dsv":1,"fs":1}]},{},[2]);
You can’t perform that action at this time.
