parseStyles.js
941 Bytes
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
/**
Transforms an array of style objects into an array of pipe separated strings
input = [
{
'feature': 'road',
'element': 'all',
'rules': {
'hue': '0x00ff00'
}
},
{
'feature': 'landscape',
'element': 'all',
'rules': {
'visibility': 'off'
}
}
]
output = [
"feature:road|element:all|hue:0x00ff00",
"feature:landscape|element:all|visibility:off"
]
**/
module.exports = function(styles) {
if (!Array.isArray(styles)) {
throw new Error('styles must be an array');
}
return styles.map(function(style){
var i, len, s = [], keys = ['feature', 'element'];
for (i = 0, len = keys.length; i < len; i++) {
if (style[keys[i]] != null) {
s.push(keys[i] + ':' + style[keys[i]]);
}
}
if (style.rules != null) {
var k;
for (k in style.rules) {
s.push(k + ':' + style.rules[k]);
}
}
return s.join('|');
});
}