-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfileMappings.js
85 lines (74 loc) · 2.58 KB
/
fileMappings.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
const some = require("lodash/some");
const reduce = require("lodash/reduce");
const get = require("lodash/get");
const select = (values, value) => some(values, (val) => val === value);
const inGroups = (values, value) => some(values, (val) => value.startsWith(val));
/**
* Map layers to files. FileMappers is a map of file names and the criteria
* a layer has to fulfill with its id or source-layer property to be included
* in the file. The criteria can be either a function that returns a boolean
* to indicate whether or not the layer should be included, or an array of
* strings that the layer's id or source-layer prop should match.
*
* The order is important. Later file matchers override earlier ones. For a
* layer called "rail_tunnel", and the files "routes" and "base", the layer
* will be included in the "base" file if both criterias match.
*/
const fileMappers = {
base: (value) => {
const names = ["background", "admin_country"];
const groups = [
"landuse",
"water",
"aeroway",
"building",
"road",
"tunnel",
"bridge",
];
if (inGroups(groups, value)) {
return true;
}
return select(names, value);
},
"municipal-borders": ["municipal_border"],
routes: (value) => inGroups(["route"], value),
stops: (value) => inGroups(["stops"], value),
"ticket-zones": (value) => inGroups(["ticket-zones"], value),
"ticket-zone-labels": (value) => inGroups(["ticket-zone-label"], value),
text: (value) => inGroups(["label"], value),
"subway-entrance": (value) => inGroups(["subway-entrance"], value),
icon: (value) => inGroups(["icon"], value),
"park-and-ride": (value) => inGroups(["park-and-ride"], value),
"ticket-sales": (value) => inGroups(["ticket-sales"], value),
citybikes: (value) => inGroups(["citybike"], value),
};
function layerToFile(layer) {
const sourceLayer = get(layer, "source-layer", "base");
const layerId = get(layer, "id", "base");
return reduce(
fileMappers,
(groupName, match, layerGroupName) => {
const matchToGroup = (val) => {
if (typeof match === "function") {
return match(val);
}
if (Array.isArray(match)) {
return select(match, val);
}
return false;
};
// The matchers will be called with the sourceLayer first, and then the layerId.
let isInGroup = matchToGroup(sourceLayer);
if (!isInGroup) {
isInGroup = matchToGroup(layerId);
}
if (isInGroup) {
return layerGroupName;
}
return groupName;
},
sourceLayer
);
}
module.exports = layerToFile;