Skip to content

Commit e207941

Browse files
Simplify logic
1 parent 1a9de7b commit e207941

File tree

1 file changed

+65
-96
lines changed

1 file changed

+65
-96
lines changed

dist/json-bufferify.js

Lines changed: 65 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@
88
*/
99

1010
(bufferify => {
11+
const types = {
12+
Uint8: {
13+
val: 0,
14+
offset: 1
15+
},
16+
Int8: {
17+
val: 1,
18+
offset: 1
19+
},
20+
Uint16: {
21+
val: 2,
22+
offset: 2
23+
},
24+
Int16: {
25+
val: 3,
26+
offset: 2
27+
},
28+
Uint32: {
29+
val: 4,
30+
offset: 4
31+
},
32+
Int32: {
33+
val: 5,
34+
offset: 4
35+
},
36+
Float32: {
37+
val: 6,
38+
offset: 4
39+
},
40+
Float64: {
41+
val: 7,
42+
offset: 8
43+
}
44+
};
45+
1146
/**
1247
* Extend anything.
1348
* @param {any} args = ([deep, ]target, source1[, ...sourceN])
@@ -24,7 +59,7 @@
2459
}
2560
val.forEach(obj => {
2661
if (obj instanceof Object) {
27-
Object.keys(obj).forEach(item => val[0][item] = deep ? extend(deep, obj[item] instanceof Array ? [] : {}, obj[item]) : obj[item]);
62+
Object.keys(obj).forEach(key => val[0][key] = deep ? extend(deep, obj[key] instanceof Array ? [] : {}, obj[key]) : obj[key]);
2863
} else {
2964
val[0] = obj;
3065
}
@@ -64,118 +99,52 @@
6499
});
65100
}
66101
if (data instanceof Array && data[0] instanceof Object) {
67-
data.forEach(item => offset = _encode(offset, arr, item));
102+
data.forEach(obj => offset = _encode(offset, arr, obj));
68103
} else {
69-
Object.keys(data).sort().forEach(item => {
70-
let current,
71-
flag = false;
72-
if (data[item] instanceof Object) {
73-
offset = _encode(offset, arr, data[item]);
74-
} else if (typeof data[item] === 'number') {
75-
arr.push({
76-
val: data[item]
77-
});
78-
current = arr[arr.length - 1];
79-
if (data[item] % 1 || (flag = data[item] < Math.pow(-2, 31) || data[item] > Math.pow(2, 32) - 1)) {
80-
if (!flag && data[item] >= -3.4e38 && data[item] <= 3.4e38) {
81-
arr.splice(arr.length - 1, 0, {
82-
val: 6,
83-
type: 'Uint8',
84-
offset: offset++
85-
});
86-
current.type = 'Float32';
87-
current.offset = offset;
88-
offset += 4;
89-
} else {
90-
arr.splice(arr.length - 1, 0, {
91-
val: 7,
92-
type: 'Uint8',
93-
offset: offset++
94-
});
95-
current.type = 'Float64';
96-
current.offset = offset;
97-
offset += 8;
98-
}
104+
Object.keys(data).sort().forEach(key => {
105+
if (data[key] instanceof Object) {
106+
offset = _encode(offset, arr, data[key]);
107+
} else if (typeof data[key] === 'number') {
108+
let flag,
109+
type;
110+
if (data[key] % 1 || (flag = data[key] < Math.pow(-2, 31) || data[key] > Math.pow(2, 32) - 1)) {
111+
type = flag || data[key] < -3.4e38 || data[key] > 3.4e38 ? 'Float64' : 'Float32';
99112
} else {
100-
if (data[item] >= 0) {
101-
if (data[item] < Math.pow(2, 8)) {
102-
arr.splice(arr.length - 1, 0, {
103-
val: 0,
104-
type: 'Uint8',
105-
offset: offset++
106-
});
107-
current.type = 'Uint8';
108-
current.offset = offset;
109-
offset += 1;
110-
} else if (data[item] < Math.pow(2, 16)) {
111-
arr.splice(arr.length - 1, 0, {
112-
val: 2,
113-
type: 'Uint8',
114-
offset: offset++
115-
});
116-
current.type = 'Uint16';
117-
current.offset = offset;
118-
offset += 2;
119-
} else if (data[item] < Math.pow(2, 32)) {
120-
arr.splice(arr.length - 1, 0, {
121-
val: 4,
122-
type: 'Uint8',
123-
offset: offset++
124-
});
125-
current.type = 'Uint32';
126-
current.offset = offset;
127-
offset += 4;
128-
}
113+
if (data[key] < 0) {
114+
type = data[key] > Math.pow(-2, 7) ? 'Int8' : data[key] > Math.pow(-2, 15) ? 'Int16' : 'Int32';
129115
} else {
130-
if (data[item] > Math.pow(-2, 7)) {
131-
arr.splice(arr.length - 1, 0, {
132-
val: 1,
133-
type: 'Uint8',
134-
offset: offset++
135-
});
136-
current.type = 'Int8';
137-
current.offset = offset;
138-
offset += 1;
139-
} else if (data[item] > Math.pow(-2, 15)) {
140-
arr.splice(arr.length - 1, 0, {
141-
val: 3,
142-
type: 'Uint8',
143-
offset: offset++
144-
});
145-
current.type = 'Int16';
146-
current.offset = offset;
147-
offset += 2;
148-
} else if (data[item] > Math.pow(-2, 31)) {
149-
arr.splice(arr.length - 1, 0, {
150-
val: 5,
151-
type: 'Uint8',
152-
offset: offset++
153-
});
154-
current.type = 'Int32';
155-
current.offset = offset;
156-
offset += 4;
157-
}
116+
type = data[key] < Math.pow(2, 8) ? 'Uint8' : data[key] < Math.pow(2, 16) ? 'Uint16' : 'Uint32';
158117
}
159118
}
160-
} else if (typeof data[item] === 'boolean') {
161119
arr.push({
162-
val: data[item] ? 1 : 0,
120+
val: types[type].val,
121+
type: 'Uint8',
122+
offset: offset++
123+
}, {
124+
val: data[key],
125+
type,
126+
offset
127+
});
128+
offset += types[type].offset;
129+
} else if (typeof data[key] === 'boolean') {
130+
arr.push({
131+
val: data[key] ? 1 : 0,
163132
type: 'Uint8',
164133
offset: offset++
165134
});
166135
} else {
167-
data[item] = data[item].split('').map(item => item.charCodeAt(0));
136+
data[key] = data[key].split('').map(char => char.charCodeAt(0));
168137
//Here is using a Uint8 to store the length of string, so the length of string can only be up to 255.
169138
arr.push({
170-
val: data[item].length,
139+
val: data[key].length,
171140
type: 'Uint8',
172141
offset: offset++
173142
});
174-
data[item].forEach(item => {
143+
data[key].forEach(val => {
175144
arr.push({
176-
val: item,
145+
val,
177146
type: 'Uint16',
178-
offset: offset
147+
offset
179148
});
180149
offset += 2;
181150
});

0 commit comments

Comments
 (0)