-
Notifications
You must be signed in to change notification settings - Fork 8
/
fromId.js
118 lines (108 loc) · 2.38 KB
/
fromId.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
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
// Imports
const fs = require('fs');
const yaml = require('js-yaml');
const validator = require('./lib/schema');
// Constants
const IN_FOLDER = 'id_presets';
const OUT_FOLDER = 'id_presets_out';
// Input data from iD presets
const fields = JSON.parse(fs.readFileSync(IN_FOLDER+'/fields.json')).fields;
const presets = JSON.parse(fs.readFileSync(IN_FOLDER+'/presets.json')).presets;
// Converter
for(let k of Object.keys(presets)) {
const preset = presets[k];
if(preset.fields && (k.match(/\//g) || []).length === 1) {
const out = {
name: {
default: preset.name+" preset",
en: preset.name+" preset"
},
version: "0.1",
author: "iD developers",
description: {
default: preset.name,
en: preset.name
},
groups: [{
name: {
default: preset.name+" group",
en: preset.name+" group"
},
items: [{
name: {
default: preset.name,
en: preset.name
},
tags: []
}]
}]
};
for(let t of Object.keys(preset.tags)) {
out.groups[0].items[0].tags.push({
type: 'CONSTANT',
key: t,
value: preset.tags[t]
});
}
for(let fk of preset.fields) {
const field = fields[fk];
const val = {
key: field.key
};
let ok = true;
switch(field.type) {
case 'check':
val.type = 'SINGLE_CHOICE';
val.values = [ 'yes', 'no' ];
break;
case 'number':
case 'maxspeed':
val.type = 'NUMBER';
break;
case 'localized':
case 'text':
case 'textarea':
case 'tel':
val.type = 'TEXT';
break;
case 'url':
val.type = 'TEXT';
val.format = 'url';
break;
case 'structureRadio':
if(field.keys && field.keys.length > 0) {
val.type = 'SINGLE_CHOICE';
val.values = field.keys;
}
else {
console.log(k);
ok = false;
}
break;
case 'combo':
case 'radio':
if(field.options) {
val.type = 'SINGLE_CHOICE';
val.values = field.options;
}
else {
ok = false;
}
break;
default:
console.log("unrecognized:", field.type);
// console.log(field);
ok = false;
}
if(ok) {
out.groups[0].items[0].tags.push(val);
}
}
if(out.groups[0].items[0].tags.length > 0 && validator.validate(out)) {
fs.writeFileSync(OUT_FOLDER+'/'+k.replace("/", "_")+'.yaml', yaml.dump(out));
}
else {
console.log("Empty or invalid preset", k);
}
}
}