-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
138 lines (127 loc) · 3.77 KB
/
index.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
const fetch = require('unfetch').default;
const iniialJson = getHash() || `{
"author": "shakyShane",
"profile": {
"links": [
{
"name": "twitter",
"url": "https://twitter.com/shaneOsbourne"
},
{
"name": "Medium",
"url": "https://medium.com/@shakyShane"
}
]
},
"location": null,
"email": null,
"bio": null,
"public_repos": 145,
"public_gists": 69,
"followers": 298,
"following": 1,
"created_at": "2012-04-14T17:34:37Z",
"updated_at": "2017-04-26T12:43:35Z"
}`;
const $ = document.querySelector.bind(document);
const statusElem = $('.status');
const statusText = $('.debug');
const flow = $('#flow');
const rootName = $('#root-name');
const prefix = $('#prefix');
const namespace = $('#namespace');
const examplesElem = $('#examples');
const examples = [
['json/nested.json', 'Nested Objects & Arrays'],
['json/optional.json', 'Optional Fields'],
['json/recursive.json', 'Recursive data structures'],
['json/invalid-keys.json', 'Invalid property names']
];
examples.forEach(([json, title]) => {
const opt = document.createElement('option');
opt.value = json;
opt.textContent = title;
examplesElem.appendChild(opt);
});
examplesElem.addEventListener('change', function () {
if (examplesElem.value !== 'Select an example') {
examplesElem.parentNode.classList.add('loading');
const resp = fetch(examplesElem.value).then(x => x.text());
resp.then(x => jsonInput.setValue(x));
}
});
let defaults = {
flow: false,
namespace: '',
prefix: 'I',
rootName: 'RootObject'
};
flow.addEventListener('change', function () {
setOutput(jsonInput.getValue(), options({flow: flow.checked}));
});
namespace.addEventListener('input', function () {
setOutput(jsonInput.getValue(), options({namespace: namespace.value}));
});
rootName.addEventListener('input', function () {
setOutput(jsonInput.getValue(), options({rootName: rootName.value}));
});
rootName.addEventListener('input', function () {
setOutput(jsonInput.getValue(), options({rootName: rootName.value}));
});
prefix.addEventListener('input', function () {
setOutput(jsonInput.getValue(), options({prefix: prefix.value}));
});
const ts = json2ts(iniialJson, defaults);
var tsOutput = CodeMirror($('#ts'), {
value: ts,
mode: {name: "javascript", typescript: true}
});
var jsonInput = CodeMirror($('#app'), {
value: iniialJson,
mode: {name: "javascript", json: true}
});
function setOutput(json, options) {
if (json && json.length > 3) {
JSON.parse(json);
const ts = json2ts(json, options);
tsOutput.setValue(ts);
status('success', 'JSON: All good!');
}
}
jsonInput.on('change', function (obj) {
try {
setOutput(jsonInput.getValue());
} catch (e) {
status('error', `Error parsing JSON: ${e.message}`);
console.log('Some JSON error?', e.message);
}
});
function getHash() {
if (window.location.hash) {
if (window.location.hash.slice(0, 5) === '#src=') {
const maybe = window.location.hash.slice(5);
if (maybe) {
return decodeURIComponent(maybe);
}
}
}
return "";
}
const share = $('a[href="#share"]');
share.addEventListener('click', function (e) {
e.preventDefault();
window.location.hash = `src=${encodeURIComponent(jsonInput.getValue().trim())}`;
});
function status(type, text) {
statusElem.classList.remove('success');
statusElem.classList.remove('error');
statusElem.classList.add(type);
statusText.textContent = text;
}
function options(incoming) {
defaults = Object.assign({},
defaults,
incoming
);
return defaults;
}