-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.js
55 lines (52 loc) · 1.32 KB
/
utils.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
function match(s, regex, patternNo) {
if (!s) {
return;
}
const m = String(s).match(regex);
if (m && patternNo !== undefined) {
return m[patternNo];
} else {
return m;
}
};
// what file extension the file should be saved to disk
function extensionForSource(source) {
if (source.zip) {
return 'zip';
} else {
const format = formatForSource(source)
return format === 'shp' ? 'zip' : format;
}
};
// what format the file is ultimately in
function formatForSource(source) {
const url = source.download;
let format = source.format;
if (!format) {
format = match(url, /\.([a-z]+)$/i, 1);
}
if (format === 'zip') {
format = 'shp';
}
if (!format) {
if (match(url, /(shp$|format=shp|\.shp|\.zip)/)) {
format = 'shp';
} else if (match(url, /(csv$|format=csv|\.csv)/)) {
format = 'csv';
} else if (match(url, /(json$|format=json|geojson$|format=geojson|\.geojson|\.json)/)) {
format = 'geojson';
}
}
if (format === 'json') {
format = 'geojson';
}
if (!format && source.keepExtension) {
format = url.match(/\.([^.]+)$/)[1];
}
return format;
}
module.exports = {
match,
formatForSource,
extensionForSource
};