-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
61 lines (47 loc) · 1.3 KB
/
backend.js
File metadata and controls
61 lines (47 loc) · 1.3 KB
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
function Converter() {
this.flatten = function(input) {
var output = {};
function flattenator(current, property) {
if (Object(current) !== current) {
output[property] = current;
} else if (Array.isArray(current)) {
for (var i = 0, len = current.length; i < len; i++) {
flattenator(current[i], property + '/' + i);
}
if (0 == len) {
output[property] = [];
}
} else {
var isEmpty = true;
for (var p in current) {
isEmpty = false;
flattenator(current[p], property ? property + '/' + p : p);
}
if (isEmpty && property) {
output[property] = {};
}
}
}
flattenator(input);
return output;
};
this.expand = function(input) {
if (Object(input) !== input) {
return input;
}
var regexp = /([^\/]+)/g,
output = {};
for (var p in input) {
var current = output,
property = '',
match;
while (match = regexp.exec(p)) {
current = current[property] || (current[property] = (match[2] ? [] : {}));
property = match[2] || match[1];
}
current[property] = input[p];
}
return output[''] || output;
};
};
module.exports = Converter;