Skip to content

Commit f1faa36

Browse files
committed
Add more test cases, fix bugs.
1 parent 973c6cc commit f1faa36

File tree

12 files changed

+637
-43
lines changed

12 files changed

+637
-43
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"dependencies": {
1616
"d3": "3.5.x",
17-
"topojson": "^1.6.18"
17+
"topojson": "^1.6.18",
18+
"request": "2.53.0"
1819
},
1920
"devDependencies": {
2021
"browser-sync": "^2.6.0",

src/util/tree.js src/import/formats/tree.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var TREE_KEY = '__tree_data__';
22

3-
function isTree(data) {
3+
function isTree(obj) {
44
return obj && obj[TREE_KEY];
55
}
66

src/import/formats/treejson.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var util = require('../../util');
2-
var tree = require('../../util/tree');
2+
var tree = require('./tree');
33
var json = require('./json');
44

55
module.exports = function(data, format) {

src/import/load.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ var fileProtocol = 'file://';
1111
// Returns cleaned up URL, or false if access is not allowed
1212
function sanitizeUrl(opt) {
1313
var url = opt.url;
14+
if (!url && opt.file) { return fileProtocol + opt.file; }
1415

1516
// In case this is a relative url (has no host), prepend opt.baseURL
1617
if (opt.baseURL && !protocol_re.test(url)) {
17-
if (!util.startsWith(url, '/') && opt.baseURL[baseURL.length-1] !== '/') {
18+
if (!util.startsWith(url, '/') && opt.baseURL[opt.baseURL.length-1] !== '/') {
1819
url = '/' + url; // Ensure that there is a slash between the baseURL (e.g. hostname) and url
1920
}
2021
url = opt.baseURL + url;
2122
}
2223
// relative protocol, starts with '//'
2324
if (util.isNode && util.startsWith(url, '//')) {
24-
url = opt.defaultProtocol + url;
25+
url = (opt.defaultProtocol || 'http') + ':' + url;
2526
}
2627
// If opt.domainWhiteList is set, only allows url, whose hostname
2728
// * Is the same as the origin (window.location.hostname)
@@ -32,10 +33,6 @@ function sanitizeUrl(opt) {
3233
if (util.isNode) {
3334
// relative protocol is broken: https://github.com/defunctzombie/node-url/issues/5
3435
var parts = require('url').parse(url);
35-
// In safe mode, make sure url begins with http:// or https://
36-
if (opt.safeMode && parts.protocol !== 'http:' && parts.protocol !== 'https:') {
37-
return false;
38-
}
3936
domain = parts.hostname;
4037
origin = null;
4138
} else {
@@ -59,18 +56,23 @@ function sanitizeUrl(opt) {
5956
(idx > 1 && domain[idx-1] === '.' && domain.lastIndexOf(d) === idx);
6057
});
6158
if (!whiteListed) {
62-
util.error('URL is not whitelisted: ' + url);
63-
url = false;
59+
throw 'URL is not whitelisted: ' + url;
6460
}
6561
}
6662
}
6763
return url;
6864
}
6965

7066
function load(opt, callback) {
71-
var url = load.sanitizeUrl(opt); // enable override
67+
try {
68+
var url = load.sanitizeUrl(opt); // enable override
69+
} catch (err) {
70+
callback(err, null);
71+
return;
72+
}
73+
7274
if (!url) {
73-
callback('Bad URL', null);
75+
callback('Invalid URL: ' + url, null);
7476
} else if (!util.isNode) {
7577
// in browser, use xhr
7678
xhr(url, callback);
@@ -91,8 +93,6 @@ function xhrHasResponse(request) {
9193
}
9294

9395
function xhr(url, callback) {
94-
util.log('LOAD XHR: ' + url);
95-
9696
var request = new XMLHttpRequest;
9797
// If IE does not support CORS, use XDomainRequest (from d3.xhr)
9898
if (this.XDomainRequest
@@ -117,12 +117,10 @@ function xhr(url, callback) {
117117
}
118118

119119
function file(file, callback) {
120-
util.log('LOAD FILE: ' + file);
121120
require('fs').readFile(file, callback);
122121
}
123122

124123
function http(url, callback) {
125-
util.log('LOAD HTTP: ' + url);
126124
var req = require('request')(url, function(error, response, body) {
127125
if (!error && response.statusCode === 200) {
128126
callback(null, body);

src/import/read.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var util = require('../util');
2-
var tree = require('../util/tree');
2+
var tree = require('./formats/tree');
33
var formats = require('./formats');
44

55
var parsers = {
@@ -17,18 +17,20 @@ function read(data, format) {
1717

1818
function parseValues(data, types) {
1919
var cols = util.keys(types),
20-
parse = cols.map(function(col) { return parsers[types[col]]; });
21-
parseArray(tree ? [data] : data, cols, parse, tree.isTree(data));
20+
parse = cols.map(function(col) { return parsers[types[col]]; }),
21+
isTree = tree.isTree(data);
22+
23+
parseArray(isTree ? [data] : data, cols, parse, isTree);
2224
}
2325

24-
function parseArray(data, cols, parse, tree) {
26+
function parseArray(data, cols, parse, isTree) {
2527
var d, i, j, len, clen;
2628
for (i=0, len=data.length; i<len; ++i) {
2729
d = data[i];
2830
for (j=0, clen=cols.length; j<clen; ++j) {
2931
d[cols[j]] = parse[j](d[cols[j]]);
3032
}
31-
if (tree && d.values) {
33+
if (isTree && d.values) {
3234
parseArray(d.values, cols, parse, true);
3335
}
3436
}

src/util/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ u.isBoolean = function(obj) {
3535

3636
// type coercion functions
3737

38-
u.number = function(s) { return s === null ? null : +s; };
38+
u.number = function(s) { return s == null ? null : +s; };
3939

40-
u.boolean = function(s) { return s === null ? null : !!s; };
40+
u.boolean = function(s) { return s == null ? null : s==='false' ? false : !!s; };
4141

42-
u.date = function(s) {return s === null ? null : Date.parse(s); }
42+
u.date = function(s) { return s == null ? null : Date.parse(s); }
4343

4444
u.array = function(x) { return x != null ? (u.isArray(x) ? x : [x]) : []; };
4545

@@ -217,7 +217,8 @@ u.stablesort = function(array, sortBy, keyFn) {
217217
u.bins = require('./bins');
218218

219219
var log = require('./log');
220-
u.log = function(msg) { log(msg, msg.LOG); };
220+
u.log = function(msg) { log(msg, log.LOG); };
221+
u.log.silent = log.silent;
221222
u.error = function(msg) { log(msg, log.ERR); };
222223

223224
u.truncate = require('./truncate');

src/util/log.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
var LOG = "LOG";
22
var ERR = "ERR";
3-
4-
var log;
5-
var has_stderr = typeof process !== 'undefined'
6-
&& typeof process.stderr !== 'undefined';
3+
var silent = false;
74

85
function prepare(msg, type) {
9-
return "[" + Date.now() + "] " + (type || LOG) + " " + msg;
6+
return '[' + [
7+
'"'+(type || LOG)+'"',
8+
Date.now(),
9+
'"'+msg+'"'
10+
].join(", ") + ']';
1011
}
1112

12-
if (has_stderr) {
13-
log = function(msg, type) {
14-
msg = prepare(msg, type);
15-
process.stderr.write(msg);
16-
};
17-
} else {
18-
log = function(msg, type) {
13+
function log(msg, type) {
14+
if (!silent) {
1915
msg = prepare(msg, type);
20-
if (type === ERR) {
21-
console.error(msg)
22-
} else {
23-
console.log(msg);
24-
}
16+
console.error(msg);
2517
}
2618
}
2719

20+
log.silent = function(val) { silent = !!val; };
21+
2822
log.LOG = LOG;
2923
log.ERR = ERR;
3024
module.exports = log;

0 commit comments

Comments
 (0)