-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathoutput_as_hack.js
executable file
·166 lines (151 loc) · 4.18 KB
/
output_as_hack.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node --harmony
var program = require('commander');
var info = require('../package.json');
program
.version(info.version)
.option('--ariaprops', 'output ARIA property definitions')
.option('--dom', 'output DOM')
.option('--elementrole', 'output element to role relationships')
.option('--roleelement', 'output role to element relationships')
.option('--roles', 'output roles')
.parse(process.argv);
const output = Object.create(null);
if (program.ariaprops) {
output['ariaprops'] = require('../src/ariaPropsMap.js').default;
}
if (program.dom) {
output['dom'] = require('../src/domMap.js').default;
}
if (program.elementrole) {
output['elementrole'] = require('../src/elementRoleMap.js').default;
}
if (program.roleelement) {
output['roleelement'] = require('../src/roleElementMap.js').default;
}
if (program.roles) {
output['roles'] = require('../src/rolesMap.js').default;
}
function getTypeof(value) {
return Array.isArray(value)
? 'array'
: (value == null)
? 'null'
: typeof value;
}
function stringifyBoolean(value) {
return [(value) ? 'true' : 'false'];
}
function stringifyArray(arr, depth) {
const output = [];
if (arr.length === 0) {
output.push('Vector {}');
} else {
output.push('Vector {');
let tmp = [];
(new Set(arr)).forEach(
value => {
if (value != null) {
tmp.push(`${constructIndent(depth)}${triageValue(value, (depth))},`);
}
}
);
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)}}`);
}
return output.join('\n');
}
function stringifyObject(value, depth) {
let output = [];
const keys = Object.keys(value);
if (keys.length === 0) {
output.push('shape ()');
} else {
output.push(`shape (`);
const tmp = [];
keys.forEach(key => {
tmp.push(`${constructIndent(depth)}${triageValue(key, (depth - 1), false)} => ${triageValue(value[key], depth)},`);
});
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)})`);
}
return output.join('\n');
}
function stringifyMap(value, depth) {
let output = [];
if (value.size === 0) {
output.push('shape ()');
} else {
output.push(`shape (`);
const tmp = [];
value.forEach(value, key => {
tmp.push(`${constructIndent(depth)}${triageValue(key, (depth - 1), false)} => ${triageValue(value, depth)},`);
});
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)})`);
}
return output.join('\n');
}
function stringifySet(value, depth) {
const output = [];
if (value.size === 0) {
output.push('Vector {}');
} else {
output.push('Vector {');
let tmp = [];
value.forEach(
value => {
if (value != null) {
tmp.push(`${constructIndent(depth)}${triageValue(value, (depth))},`);
}
}
);
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)}}`);
}
return output.join('\n');
}
function constructIndent(depth) {
return Array(depth).fill(' ').join('');
}
function triageValue(value, depth = 0) {
let output = [];
switch(getTypeof(value)) {
case 'object':
if (value instanceof Map) {
output = output.concat(stringifyMap(value, (depth + 1)));
} else if (value instanceof Set) {
output = output.concat(stringifySet(value, (depth + 1)));
} else {
output = output.concat(stringifyObject(value, (depth + 1)));
}
break;
case 'array':
output = output.concat(stringifyArray(value, (depth + 1)));
break;
case 'boolean':
output = output.concat(stringifyBoolean(value, (depth + 1)));
break;
case 'string':
output = output.concat(`'${value}'`);
break;
default:
output.push('null');
}
return output;
}
let hack = [];
Object.keys(output).forEach(type => {
hack = hack.concat(
`$${type} = Map {`,
...[...output[type].keys()]
.sort()
.map(prop => {
const depth = 2;
return `${constructIndent(depth)}${triageValue(prop, depth, false)} => ${triageValue(output[type].get(prop), depth).join('\n')},`;
}),
`${constructIndent(1)}};`
);
});
if (hack.length) {
process.stdout.write(hack.join('\n'));
}