-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils-behavior.html
More file actions
115 lines (104 loc) · 3.34 KB
/
utils-behavior.html
File metadata and controls
115 lines (104 loc) · 3.34 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
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
<script>
window.PolymerApigility = window.PolymerApigility || {};
PolymerApigility.UtilsBehavior = {
inflateKeys: function(data, breakOnNonObject) {
var output = {};
var _process = function (key, val, output) {
var k;
if (typeof val === 'object') {
for (k in val) {
if (breakOnNonObject && typeof val[k] !== 'object') {
output[key] = val;
} else {
_process(key + '[' + k + ']', val[k], output);
}
}
} else if (typeof val !== 'function') {
output[key] = val;
} else {
throw new Error('There was an error processing for inflateKeys().');
}
};
for (var key in data) {
_process(key, data[key], output);
}
return output;
},
deflateKeyAndSet: function(key, value, data, useArrayWhenFirstKeyIsNumeric) {
// Avoid indirect modifications
if (value instanceof Array) {
// slices() clones the array and returns the reference to the new array
value = value.slice();
}
var j, ct, p, lastObj, obj, lastIter, undef, chr,
postLeftBracketPos, keys, keysLen;
while (key.charAt(0) === ' ') {
key = key.slice(1);
}
if (key.indexOf('\x00') > -1) {
key = key.slice(0, key.indexOf('\x00'));
}
if (key && key.charAt(0) !== '[') {
keys = [];
postLeftBracketPos = 0;
for (j = 0; j < key.length; j++) {
if (key.charAt(j) === '[' && !postLeftBracketPos) {
postLeftBracketPos = j + 1;
} else if (key.charAt(j) === ']') {
if (postLeftBracketPos) {
if (!keys.length) {
keys.push(key.slice(0, postLeftBracketPos - 1));
}
keys.push(key.substr(postLeftBracketPos, j - postLeftBracketPos));
postLeftBracketPos = 0;
if (key.charAt(j + 1) !== '[') {
break;
}
}
}
}
if (!keys.length) {
keys = [key];
}
for (j = 0; j < keys[0].length; j++) {
chr = keys[0].charAt(j);
if (chr === ' ' || chr === '.' || chr === '[') {
keys[0] = keys[0].substr(0, j) + '_' + keys[0].substr(j + 1);
}
if (chr === '[') {
break;
}
}
obj = data;
for (j = 0, keysLen = keys.length; j < keysLen; j++) {
key = keys[j].replace(/^['"]/, '')
.replace(/['"]$/, '');
lastIter = j !== keys.length - 1;
lastObj = obj;
if ((key !== '' && key !== ' ') || j === 0) {
if (obj[key] === undef) {
if (useArrayWhenFirstKeyIsNumeric && (j+1) < keysLen && !isNaN(parseInt(keys[j+1].replace(/^['"]/, '').replace(/['"]$/, '')))) {
obj[key] = [];
} else {
obj[key] = {};
}
}
obj = obj[key];
} else {
// To insert new dimension
ct = -1;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
if (+p > ct && p.match(/^\d+$/g)) {
ct = +p;
}
}
}
key = ct + 1;
}
}
lastObj[key] = value;
}
}
};
</script>